ansible 变量

用一个playbook例子直接说明:
1. 在playbook中直接定义变量
2. 在inventory中定义变量 - 在此未体现
3. 在/etc/ansible/host_vars和/etc/ansible/host_groups/文件夹下定义变量
在变量很多时,不建议直接在Inventory中直接定义变量,而是考虑在/etc/ansible/host_vars和/etc/ansible/host_groups中定义;
在执行ansible命令时,ansible默认会从/etc/ansible/host_vars和/etc/ansbile/host_group/两个目录下读取变量定义;
4. 内置变量:ansible_eth0;
ansible_eth0内置变量是用来保存远程主机上的eth0接口信息,包括ip/netmask等
5. 数组变量,sport数组变量

var_demo.yml
(env27)wfq@ubuntu:~/playbook$ vim var_demo.yml 
---
- hosts: asb
  vars:
      foo: bar
  vars_files:
      - vars.yml
  tasks:
      - debug: msg="Variable 'foo' is set to {{ foo }}, vars_files name is {{ name }}"
      - debug: msg="host_vars hobby {{ hobby }}, home {{ home }}, job {{ job }}"
      - debug: msg="group_vars fllower {{ fllower }} sport array {{ sport[0] }} {{ sport[1] }} {{ sport[2] }}"
      - debug: msg="ansible_eth0.ipv4.addres {{ ansible_eth0.ipv4.address }}; ansible_eth0.ipv4.netmask {{ ansible_eth0.ipv4.netmask }}; ansible_eth0.ipv4.network {{ a
nsible_eth0.ipv4.network }}"
(env27)wfq@ubuntu:~/playbook$ 

/etc/ansible/hosts,在hosts文件中定义了asb的组,asb means ansible,4台服务器(131被玩坏去掉了)
wfq@ubuntu:/etc/ansible$ ls   
ansible.cfg  group_vars  host_vars  hosts
wfq@ubuntu:/etc/ansible$ cat hosts
[asb]
192.168.174.130
192.168.174.132
192.168.174.133
192.168.174.134
wfq@ubuntu:/etc/ansible$ 
/etc/ansible/host_vars/目录下的主机变量定义,因为主机变量的文件名和主机名要一致,我的定义如下
wfq@ubuntu:/etc/ansible/host_vars$ ls 
192.168.174.130.yml  192.168.174.131.yml  192.168.174.132.yml  192.168.174.133.yml  192.168.174.134.yml  
wfq@ubuntu:/etc/ansible/host_vars$ cat 192.168.174.130.yml
---
hobby: running
job: devops
home: huixinloft
wfq@ubuntu:/etc/ansible/host_vars$ pwd
/etc/ansible/host_vars
wfq@ubuntu:/etc/ansible/host_vars$

/etc/ansible/group_vars目录,因为文件名要和Inventory的主机组名一致,所以定义如下:

wfq@ubuntu:/etc/ansible/group_vars$ ls
asb.yml
wfq@ubuntu:/etc/ansible/group_vars$ cat asb.yml 
---
fllower: luhui
sport:
   - basketball
   - running
   - badminton
wfq@ubuntu:/etc/ansible/group_vars$

执行ansible-playbook

(env27)wfq@ubuntu:~/playbook$ ansible-playbook var_demo.yml

结果如下:

(env27)wfq@ubuntu:~/playbook$ ansible-playbook var_demo.yml 

PLAY [asb] ******************************************************************** 

GATHERING FACTS *************************************************************** 
ok: [192.168.174.133]
ok: [192.168.174.134]
ok: [192.168.174.130]
ok: [192.168.174.132]

TASK: [debug msg="Variable 'foo' is set to bar, vars_files name is Merry"] **** 
ok: [192.168.174.130] => {
    "msg": "Variable 'foo' is set to bar, vars_files name is Merry"
}
ok: [192.168.174.132] => {
    "msg": "Variable 'foo' is set to bar, vars_files name is Merry"
}
ok: [192.168.174.133] => {
    "msg": "Variable 'foo' is set to bar, vars_files name is Merry"
}
ok: [192.168.174.134] => {
    "msg": "Variable 'foo' is set to bar, vars_files name is Merry"
}

TASK: [debug msg="host_vars hobby {{hobby}}, home {{home}}, job {{job}}"] ***** 
ok: [192.168.174.133] => {
    "msg": "host_vars hobby running, home huixinloft, job devops"
}
ok: [192.168.174.130] => {
    "msg": "host_vars hobby running, home huixinloft, job devops"
}
ok: [192.168.174.134] => {
    "msg": "host_vars hobby running, home huixinloft, job devops"
}
ok: [192.168.174.132] => {
    "msg": "host_vars hobby running, home huixinloft, job devops"
}

TASK: [debug msg="group_vars fllower {{fllower}} sport array {{sport[0]}} {{sport[1]}} {{sport[2]}}"] *** 
ok: [192.168.174.130] => {
    "msg": "group_vars fllower luhui sport array basketball running badminton"
}
ok: [192.168.174.134] => {
    "msg": "group_vars fllower luhui sport array basketball running badminton"
}
ok: [192.168.174.133] => {
    "msg": "group_vars fllower luhui sport array basketball running badminton"
}
ok: [192.168.174.132] => {
    "msg": "group_vars fllower luhui sport array basketball running badminton"
}

TASK: [debug msg="ansible_eth0.ipv4.addres {{ansible_eth0.ipv4.address}}; ansible_eth0.ipv4.netmask {{ansible_eth0.ipv4.netmask}}; ansible_eth0.ipv4.network {{ansible_eth0.ipv4.network}}"] *** 
ok: [192.168.174.130] => {
    "msg": "ansible_eth0.ipv4.addres 192.168.174.130; ansible_eth0.ipv4.netmask 255.255.255.0; ansible_eth0.ipv4.network 192.168.174.0"
}
ok: [192.168.174.132] => {
    "msg": "ansible_eth0.ipv4.addres 192.168.174.132; ansible_eth0.ipv4.netmask 255.255.255.0; ansible_eth0.ipv4.network 192.168.174.0"
}
ok: [192.168.174.134] => {
    "msg": "ansible_eth0.ipv4.addres 192.168.174.134; ansible_eth0.ipv4.netmask 255.255.255.0; ansible_eth0.ipv4.network 192.168.174.0"
}
ok: [192.168.174.133] => {
    "msg": "ansible_eth0.ipv4.addres 192.168.174.133; ansible_eth0.ipv4.netmask 255.255.255.0; ansible_eth0.ipv4.network 192.168.174.0"
}

PLAY RECAP ******************************************************************** 
192.168.174.130            : ok=5    changed=0    unreachable=0    failed=0   
192.168.174.132            : ok=5    changed=0    unreachable=0    failed=0   
192.168.174.133            : ok=5    changed=0    unreachable=0    failed=0   
192.168.174.134            : ok=5    changed=0    unreachable=0    failed=0   

(env27)wfq@ubuntu:~/playbook$ 
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wang725/article/details/79950142
文章标签: ansible playbook vars
个人分类: ansible
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭