ansible变量的使用方法
1.主机变量:直接在/etc/ansible/hosts文件中,主机的后边设置key=value的格式
注:inventory_hostname是ansible自带的变量,代表组中的每个主机
#在配置文件中定义主机变量 [root@nfs-server playbook]# cat /etc/ansible/hosts [webservers] 192.168.2.101 key=101 192.168.2.111 key=111 #写playbook,测试主机变量 [root@nfs-server playbook]# cat hosts_vars.yml --- - hosts: webservers remote_user: root tasks: - name: 显示主机变量 debug: msg="The server ip is {{ inventory_hostname }},The key is {{ key }}" #执行playbook,查看主机变量是否正常显示 [root@nfs-server playbook]# ansible-playbook hosts_vars.yml PLAY [webservers] ************************************************************************************************************************************** TASK [Gathering Facts] ********************************************************************************************************************************* ok: [192.168.2.111] ok: [192.168.2.101] TASK [显示主机变量] ************************************************************************************************************************************ ok: [192.168.2.101] => { "msg": "The server ip is 192.168.2.101,The key is 101" } ok: [192.168.2.111] => { "msg": "The server ip is 192.168.2.111,The key is 111" } PLAY RECAP ********************************************************************************************************************************************* 192.168.2.101 : ok=2 changed=0 unreachable=0 failed=0 192.168.2.111 : ok=2 changed=0 unreachable=0 failed=0
2.主机组变量
#在配置文件中定义主机组变量 [root@nfs-server playbook]# cat /etc/ansible/hosts [webservers] 192.168.2.101 192.168.2.111 [webservers:vars] ansiber_version=2.3 key=nginx #写playbook,测试主机组变量 [root@nfs-server playbook]# cat hosts_group_vars.yml --- - hosts: webservers remote_user: root tasks: - name: "显示主机组变量" debug: msg="The server ip is {{ inventory_hostname }},The key is {{ key }}" #执行playbook,查看主机组变量是否正常显示 [root@nfs-server playbook]# ansible-playbook hosts_group_vars.yml PLAY [webservers] ************************************************************************************************************************************** TASK [Gathering Facts] ********************************************************************************************************************************* ok: [192.168.2.111] ok: [192.168.2.101] TASK [显示主机组变量] ***************************************************************************************************************************************** ok: [192.168.2.101] => { "msg": "The server ip is 192.168.2.101,The key is nginx" } ok: [192.168.2.111] => { "msg": "The server ip is 192.168.2.111,The key is nginx" } PLAY RECAP ********************************************************************************************************************************************* 192.168.2.101 : ok=2 changed=0 unreachable=0 failed=0 192.168.2.111 : ok=2 changed=0 unreachable=0 failed=0
3.通过/etc/ansible下的文件定义主机以及主机组变量
主机变量目录:host_vars(注:需要在此目录下为每个主机创建一个文件,在此文件中定义变量)
组变量目录:group_vars(注:需要在此目录下创建一个以主机组名命名的文件,在此文件中定义变量)
注:以上两个目录均不存在,需要自己手动创建;mkdir /etc/ansible/{host_vars,group_vars}
注:当需要为所有主机或者所有主机组创建一样的变量时,在host_vars和group_vars目录下,创建all文件并写入变量即可~
#在配置文件中定义主机组变量 [root@nfs-server playbook]# cat /etc/ansible/hosts [webservers] 192.168.2.101 192.168.2.111 #在host_vars目录下创建以主机名为名字的文件,并在此文件中配置变量,变量格式为: key: value [root@nfs-server ansible]# head host_vars/* ==> host_vars/192.168.2.101 <== key: 101 ==> host_vars/192.168.2.111 <== key: 111 #在group_vars目录创建以主机组为名字的文件,并在此文件中配置变量,变量格式为:key: value [root@nfs-server ansible]# head group_vars/* name: nginx #测试主机变量:利用上面写的主机变量playbook进行测试 [root@nfs-server playbook]# ansible-playbook hosts_vars.yml PLAY [webservers] ************************************************************************************************************************************** TASK [Gathering Facts] ********************************************************************************************************************************* ok: [192.168.2.111] ok: [192.168.2.101] TASK [显示主机变量和主机组变量] ************************************************************************************************************************************ ok: [192.168.2.101] => { "msg": "The server ip is 192.168.2.101,The key is 101" } ok: [192.168.2.111] => { "msg": "The server ip is 192.168.2.111,The key is 111" } PLAY RECAP ********************************************************************************************************************************************* 192.168.2.101 : ok=2 changed=0 unreachable=0 failed=0 192.168.2.111 : ok=2 changed=0 unreachable=0 failed=0 #测试主机组变量:利用上面写的主机组变量playbook进行测试(把key名字改为了name) [root@nfs-server playbook]# ansible-playbook hosts_group_vars.yml PLAY [webservers] ************************************************************************************************************************************** TASK [Gathering Facts] ********************************************************************************************************************************* ok: [192.168.2.101] ok: [192.168.2.111] TASK [显示主机组变量] ***************************************************************************************************************************************** ok: [192.168.2.101] => { "msg": "The server ip is 192.168.2.101,The application is nginx" } ok: [192.168.2.111] => { "msg": "The server ip is 192.168.2.111,The application is nginx" } PLAY RECAP ********************************************************************************************************************************************* 192.168.2.101 : ok=2 changed=0 unreachable=0 failed=0 192.168.2.111 : ok=2 changed=0 unreachable=0 failed=0
4.通过命令行传入变量,或者变量文件
-e,--extra-vars:此参数表示从命令行传入变量
-e "key=value":直接传递变量的格式
-e "@vars.yml":传递变量文件的格式
#在playbook中引用一个key为YOURNAME,此key通过命令行传入 [root@nfs-server playbook]# cat hosts_vars.yml --- - hosts: webservers remote_user: root tasks: - name: 显示主机变量和主机组变量 debug: msg="The server ip is {{ inventory_hostname }},The key is {{ YOURNAME }}" [root@nfs-server playbook]# ansible-playbook hosts_vars.yml -e "YOURNAME=liuzhengwei" PLAY [webservers] ************************************************************************************************************************************** TASK [Gathering Facts] ********************************************************************************************************************************* ok: [192.168.2.101] ok: [192.168.2.111] TASK [显示主机变量和主机组变量] ************************************************************************************************************************************ ok: [192.168.2.101] => { "msg": "The server ip is 192.168.2.101,The key is liuzhengwei" } ok: [192.168.2.111] => { "msg": "The server ip is 192.168.2.111,The key is liuzhengwei" } PLAY RECAP ********************************************************************************************************************************************* 192.168.2.101 : ok=2 changed=0 unreachable=0 failed=0 192.168.2.111 : ok=2 changed=0 unreachable=0 failed=0 #在playbook中引用一个key为YOURNAME,此key通过提前定义的变量文件传入 #下面文件为变量文件 [root@nfs-server playbook]# cat vars/vars.yml abc: LIUZHENGWEI #通过命令行将此变量文件传入 [root@nfs-server playbook]# ansible-playbook hosts_vars.yml -e "@vars/vars.yml" PLAY [webservers] ************************************************************************************************************************************** TASK [Gathering Facts] ********************************************************************************************************************************* ok: [192.168.2.111] ok: [192.168.2.101] TASK [显示主机变量和主机组变量] ************************************************************************************************************************************ ok: [192.168.2.101] => { "msg": "The server ip is 192.168.2.101,The key is LIUZHENGWEI" } ok: [192.168.2.111] => { "msg": "The server ip is 192.168.2.111,The key is LIUZHENGWEI" } PLAY RECAP ********************************************************************************************************************************************* 192.168.2.101 : ok=2 changed=0 unreachable=0 failed=0 192.168.2.111 : ok=2 changed=0 unreachable=0 failed=0
5.在playbook文件中定义变量:格式为key: value
#在playbook文件中定义变量abc [root@nfs-server playbook]# cat hosts_vars.yml --- - hosts: webservers remote_user: root vars: abc: liuzhengwei tasks: - name: 显示主机变量和主机组变量 debug: msg="The server ip is {{ inventory_hostname }},The key is {{ abc }}" #执行playbook测试变量是否生效 [root@nfs-server playbook]# ansible-playbook hosts_vars.yml PLAY [webservers] ************************************************************************************************************************************** TASK [Gathering Facts] ********************************************************************************************************************************* ok: [192.168.2.111] ok: [192.168.2.101] TASK [显示主机变量和主机组变量] ************************************************************************************************************************************ ok: [192.168.2.101] => { "msg": "The server ip is 192.168.2.101,The key is liuzhengwei" } ok: [192.168.2.111] => { "msg": "The server ip is 192.168.2.111,The key is liuzhengwei" } PLAY RECAP ********************************************************************************************************************************************* 192.168.2.101 : ok=2 changed=0 unreachable=0 failed=0 192.168.2.111 : ok=2 changed=0 unreachable=0 failed=0
6.在playbook文件中引用定义了变量的文件:vars_files
#在playbook文件中引用定义了变量的文件vars/vars.yml [root@nfs-server playbook]# cat vars/vars.yml abc: LIUZHENGWEI [root@nfs-server playbook]# cat hosts_vars.yml --- - hosts: webservers remote_user: root vars_files: - vars/vars.yml tasks: - name: 显示主机变量和主机组变量 debug: msg="The server ip is {{ inventory_hostname }},The key is {{ abc }}" #执行playbook测试变量是否生效 [root@nfs-server playbook]# ansible-playbook hosts_vars.yml PLAY [webservers] ************************************************************************************************************************************** TASK [Gathering Facts] ********************************************************************************************************************************* ok: [192.168.2.101] ok: [192.168.2.111] TASK [显示主机变量和主机组变量] ************************************************************************************************************************************ ok: [192.168.2.101] => { "msg": "The server ip is 192.168.2.101,The key is LIUZHENGWEI" } ok: [192.168.2.111] => { "msg": "The server ip is 192.168.2.111,The key is LIUZHENGWEI" } PLAY RECAP ********************************************************************************************************************************************* 192.168.2.101 : ok=2 changed=0 unreachable=0 failed=0 192.168.2.111 : ok=2 changed=0 unreachable=0 failed=0
7.使用register内的变量
ansible playbook内task之间可以互相传递变量,比如我们总共有2个tasks,其中第2个task是否执行需要判断第一个task运行后的结果,这个时候我们就得在task之间传递数据,需要把第一个task执行的结果传递给第2个task.Ansible task之间传递数据使用register方式。
#注:这是第1个task把执行hostname的结果register给info这个变量 [root@nfs-server playbook]# cat register_var.yml --- - hosts: webservers remote_user: root tasks: - name: register variables shell: hostname register: info - name: display variables #debug: msg="The variable is {{ info['stdout'] }}" debug: msg="The variable is {{ info.stdout }}" #注:register返回结果是字典的方式,所以用info.stdout或info['stdout']的方式返回结果 [root@nfs-server playbook]# ansible-playbook register_var.yml PLAY [webservers] ************************************************************************************************************************************** TASK [Gathering Facts] ********************************************************************************************************************************* ok: [192.168.2.101] ok: [192.168.2.111] TASK [register variables] ****************************************************************************************************************************** changed: [192.168.2.101] changed: [192.168.2.111] TASK [display variables] ******************************************************************************************************************************* ok: [192.168.2.101] => { "msg": "The variable is lamp1" } ok: [192.168.2.111] => { "msg": "The variable is lamp2" } PLAY RECAP ********************************************************************************************************************************************* 192.168.2.101 : ok=3 changed=1 unreachable=0 failed=0 192.168.2.111 : ok=3 changed=1 unreachable=0 failed=0
8.使用vars_prompt传入变量(vars_prompt是以交互式的方式给定义好的参数传入变量值)
注:交互式传入的变量值可以是加密的也可以是不加密的,用private进行定义
#交互式输入变量值,传递给tasks,private=no表示输入的变量值不加密,private=yes表示加密 [root@nfs-server playbook]# cat vars_prompt.yml --- - hosts: all remote_user: root vars_prompt: - name: "one" prompt: "please input one value" private: no - name: "two" prompt: "please input two value" private: yes tasks: - name: display one value debug: msg="one value is {{ one }}" - name: display two value debug: msg="two value is {{ two }}" [root@nfs-server playbook]# ansible-playbook vars_prompt.yml please input one value: Liu Zhengwei please input two value: PLAY [webservers] ************************************************************************************************************************************** TASK [Gathering Facts] ********************************************************************************************************************************* ok: [192.168.2.101] ok: [192.168.2.111] TASK [display one value] ******************************************************************************************************************************* ok: [192.168.2.101] => { "msg": "one value is Liu Zhengwei" } ok: [192.168.2.111] => { "msg": "one value is Liu Zhengwei" } TASK [display two value] ******************************************************************************************************************************* ok: [192.168.2.101] => { "msg": "two value is age is 28" } ok: [192.168.2.111] => { "msg": "two value is age is 28" } PLAY RECAP ********************************************************************************************************************************************* 192.168.2.101 : ok=3 changed=0 unreachable=0 failed=0 192.168.2.111 : ok=3 changed=0 unreachable=0 failed=0