Ansible变量介绍 vars变量 inventory针对主机设置变量

张开发
2026/4/5 21:18:59 15 分钟阅读

分享文章

Ansible变量介绍 vars变量 inventory针对主机设置变量
Ansible变量介绍什么是变量一个动态获取数据的对象在ansible中提供指定名称定义然后预存数据当指令或者yml操作 的时候可以调用里面的数据变量的定义方式在Ansible内定义变量有三种方式 - 通过命令行传递变量参数定义 - 在playbook文件定义变量 - 通过vars:定义变量 - 通过vars_files 定义变量 - 通过Inventory在主机组或者单个主机中设置变量 - 通过hosts_vars:对主机进行定义 - 通过group_vars:对主机组进行定义优先级playbook内可以定义多种变量但是传递和执行的时候是有优先级的关系的vars变量[rootansible ~]# vi an-3.yml [rootansible ~]# cat an-3.yml #vars定义变量 #关键字:vars:变量名称 #通过vars定义的变量只能在该文件内使用 #变量名也有要求:变量的首字母必须是英文或者是下划线 #案例:定义变量并且调用变量 - hosts: webservers vars: id: 666 age: 21 uname: yun tasks: - name: 通过debug模块内的msg参数输出变量 debug: msg: 用户id: {{ id }}, 用户年纪: {{ age }}, 用户名: {{ uname }} [rootansible ~]# ansible-playbook an-3.yml PLAY [webservers] *************************************************************************************************************************************** TASK [Gathering Facts] ********************************************************************************************************************************** ok: [192.168.92.20] TASK [通过debug模块内的msg参数输出变量] ***************************************************************************************************************** ok: [192.168.92.20] { msg: 用户id: 666, 用户年纪: 21, 用户名: yun } PLAY RECAP ********************************************************************************************************************************************** 192.168.92.20 : ok2 changed0 unreachable0 failed0 skipped0 rescued0 ignored0 [rootansible ~]#inventory针对主机设置变量[rootansible ansible]# vi hosts [rootansible ansible]# vi an-5.yml [rootansible ansible]# cat hosts # This is the default ansible hosts file. # # It should live in /etc/ansible/hosts # # - Comments begin with the # character # - Blank lines are ignored # - Groups of hosts are delimited by [header] elements # - You can enter hostnames or ip addresses # - A hostname/ip can be a member of multiple groups # Ex 1: Ungrouped hosts, specify before any group headers: ## green.example.com ## blue.example.com ## 192.168.100.1 ## 192.168.100.10 # Ex 2: A collection of hosts belonging to the webservers group: ## [webservers] ## alpha.example.org ## beta.example.org ## 192.168.1.100 ## 192.168.1.110 [webservers] 192.168.92.20 unameyun urlhttp://192.168.92.20:6100 # If you have multiple hosts following a pattern, you can specify # them like this: ## www[001:006].example.com # You can also use ranges for multiple hosts: ## db-[99:101]-node.example.com # Ex 3: A collection of database servers in the dbservers group: ## [dbservers] ## ## db01.intranet.mydomain.net ## db02.intranet.mydomain.net ## 10.25.1.56 ## 10.25.1.57 # Ex4: Multiple hosts arranged into groups such as Debian and openSUSE: ## [Debian] ## alpha.example.org ## beta.example.org ## [openSUSE] ## green.example.com ## blue.example.com [rootansible ansible]# cat an-5.yml #验证inventory变量 - hosts: webservers tasks: - name: 利用debug输出变量 debug: msg: 用户名: {{ uname }}, 网址: {{ url }} [rootansible ansible]# ansible-playbook an-5.yml PLAY [webservers] *************************************************************************************************************************************** TASK [Gathering Facts] ********************************************************************************************************************************** ok: [192.168.92.20] TASK [利用debug输出变量] ******************************************************************************************************************************** ok: [192.168.92.20] { msg: 用户名: yun, 网址: http://192.168.92.20:6100 } PLAY RECAP ********************************************************************************************************************************************** 192.168.92.20 : ok2 changed0 unreachable0 failed0 skipped0 rescued0 ignored0 [rootansible ansible]#

更多文章