ansible 变量

原创 2018年04月15日 16:07:39

用一个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官方文档翻译之变量

ansible官方文档翻译之变量,主要是将官方文档进行翻译,后续会给出相关的练习...
  • kellyseeme
  • kellyseeme
  • 2016-01-26 04:13:41
  • 6595

4-1·Ansible进阶、变量与事实、包含

四·Variables and Facts 1)变量简介: ● ansible支持利用变量储存整个ansible项目文件中可重复使用的值 ● 变量的名称由字符串组成,必须以字母开头,并且...
  • qq_40663092
  • qq_40663092
  • 2018-01-31 22:11:37
  • 99

实例学习ansible系列(14)playbook变量使用

ansible的playbook中的变量引用使用{{ }}。而且Ansbile还内嵌了七个很有用的变量hostvars变量 groups变量 group_names变量 inventory_hostn...
  • liumiaocn
  • liumiaocn
  • 2016-07-31 17:04:21
  • 5840

ansible 变量优先级

ansible中变量的优先级 extra vars (-e 选项指定的变量)最高 inventory 主机清单中定义的变量(ansible_ssh_user等) play剧本中vars、vars_f...
  • hawkerou
  • hawkerou
  • 2017-04-05 21:38:17
  • 445

Ansible主机与组的定义Inventory

Inventory Ansible的Inventory文件,可以理解为saltstack中的salt-key中的所有minion的列表以及用户自定义的nodegroup的概念,默认情况下这个文件是/...
  • wmj2004
  • wmj2004
  • 2016-11-18 14:25:32
  • 3400

ansible register 之用法

ansible register 这个功能非常有用。当我们需要判断对执行了某个操作或者某个命令后,如何做相应的响应处理(执行其他 ansible 语句),则一般会用到register 。举个例子:我们...
  • qianggezhishen
  • qianggezhishen
  • 2016-12-30 09:13:28
  • 6252

向yml文件传递参数,ansible-playbook命令

用命令行传递参数定义命令行变量在release.yml文件里,hosts和user都定义为变量,需要从命令行传递变量值。 hosts: ‘{{ hosts }}’ remote_user: ‘{{ ...
  • angle_sun
  • angle_sun
  • 2016-10-03 14:58:16
  • 10162

Ansible中自定义变量的使用

Ansible自定义变量的定义方式有以下四种: 1,通过命令行定义变量 2,在playbook中定义变量 3,在主机清单文件中定义变量 4,通过roles角色定义变量 本文将主要针对Ansible中定...
  • geoffreychan
  • geoffreychan
  • 2017-12-02 22:44:23
  • 226

ansible playbook中的变量定义与引用

Ansible playbook中变量的定义与引用有很多种方法,这里在本地环境测试验证,并整理如下。测试环境:使用自己的Mac笔记本上安装ansible作为控制服务器,VMware Fusion虚拟机...
  • felix_yujing
  • felix_yujing
  • 2017-08-06 20:33:17
  • 626

ansible小结(三)Inventory与Patterns

Ansible的Inventory文件,可以理解为saltstack中的salt-key中的所有minion的列表以及用户自定义的nodegroup的概念,默认情况下这个文件是/etc/ansible...
  • weiguang1017
  • weiguang1017
  • 2016-11-01 18:51:33
  • 2231
收藏助手
不良信息举报
您举报文章:ansible 变量
举报原因:
原因补充:

(最多只允许输入30个字)