您的位置:首页 > 运维架构

Ansible中內建变量的使用

2017-11-26 22:33 381 查看

Ansible中內建变量的使用

Ansible
是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。在
Ansible
中,我们也可以使用变量以提高
Ansible
针对不同场景下使用的灵活性。

Ansible
使用的变量包括两种,內建变量和自定义变量。本文针对
Ansible
的內建变量的使用进行简单介绍。

Ansible
的內建变量通常需要
Ansible
获取远程节点信息后自动构建而成,且该变量的值只对当前远程节点有效。我们可以使用
Ansible
setup
模块查看指定节点的內建变量信息:

[root@Centos7T ~]#ansible 192.168.25.51 -m setup
192.168.25.51 | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"172.18.42.51",
"192.168.25.51"
],
"ansible_all_ipv6_addresses": [
"fe80::304e:2290:cb61:914a",
"fe80::250:56ff:fe2e:8595"
],
"ansible_apparmor": {
"status": "disabled"
},
"ansible_architecture": "x86_64",
"ansible_bios_date": "07/02/2015",
"ansible_bios_version": "6.00",
"ansible_cmdline": {
"BOOT_IMAGE": "/vmlinuz-3.10.0-514.el7.x86_64",
"LANG": "en_US.UTF-8",
"crashkernel": "auto",
"quiet": true,
"rhgb": true,
"ro": true,
"root": "UUID=02a69ee0-ceeb-43a3-b28a-49f042d51373"
},
…… ……


我们可以配置
grep
命令查找我们需要的內建变量:

[root@Centos7T ~]#ansible 192.168.25.51 -m setup | grep "mem"
"ansible_memfree_mb": 295,
"ansible_memory_mb": {
"ansible_memtotal_mb": 976,
[root@Centos7T ~]#ansible 192.168.25.51 -m setup | grep "memto"
"ansible_memtotal_mb": 976,
[root@Centos7T ~]#ansible 192.168.25.51 -m setup | grep "fqdn"
"ansible_fqdn": "centos7.example.com",


现在我们可以引用查寻到的内建变量了。变量的引用过有不同于shell脚本的特殊格式,其引用格式为
{{ Vars }}
。变量名在两对花括号里面,且变量名两侧都有一个空格。现在通过一个例子来演示。

Host Inwentory
中定义了一组远程节点,信息如下:

[websvr]
192.168.25.51
192.168.25.80


编写
YAML
格式
playbook
文件如下:

[root@Centos7T ~]#cat blog.yml
- hosts: [websvr]
user: root
tasks:
- name: create new file
shell: echo {{ ansible_fqdn }} > /root/newfile.txt


playbook
定义了一个
task
,调用
ansible_fqdn
的值,将其写入各个节点的newfile.txt文件。由于使用了重定向,这个任务使用
shell
模块。执行过程如下:

[root@Centos7T ~]#ansible-playbook blog.yml

PLAY [websvr] ******************************************************************

TASK [Gathering Facts] *********************************************************
ok: [192.168.25.51]
ok: [192.168.25.80]

TASK [create new file] *********************************************************
changed: [192.168.25.51]
changed: [192.168.25.80]

PLAY RECAP *********************************************************************
192.168.25.51              : ok=2    changed=1    unreachable=0    failed=0
192.168.25.80              : ok=2    changed=1    unreachable=0    failed=0


查看两个节点的newfile.txt文件:

节点1:
[root@centos7 R1 ~]#hostname
centos7.example.com
[root@centos7 R1 ~]#cat newfile.txt
节点2:
[root@centos7 ~]#hostname
centos7.test.com
[root@centos7 ~]#cat newfile.txt
centos7.test.com


注意:內建变量的值是通过任务
TASK [Gathering Facts]
获取到的,该任务在
ansible-play
命令中自动执行。如果没有这一步,将直接导致內建变量不可用。

[root@Centos7T ~]#ansible 192.168.25.51 -m shell -a "echo {{ ansible_fqdn }} > /root/newfile1.txt"
192.168.25.51 | FAILED | rc=-1 >>
The task includes an option with an undefined variable. The error was: 'ansible_fqdn' is undefined
exception type: <class 'ansible.errors.AnsibleUndefinedVariable'>
exception: 'ansible_fqdn' is undefined


当然,仔细观察
setup
模块的输出信息不难发现,所有的内建变量都是键值对的形式,有些多个键值对在一个花括号里面,这样的文本组织形式是基于
JSON
格式。我们查看其中一段关于ipv4的信息:

"ansible_default_ipv4": {
"address": "172.18.42.51",
"alias": "ens37",
"broadcast": "172.18.255.255",
"gateway": "172.18.0.1",
"interface": "ens37",
"macaddress": "00:50:56:3e:48:5b",
"mtu": 1500,
"netmask": "255.255.0.0",
"network": "172.18.0.0",
"type": "ether"
},


在变量
ansible_default_ipv4
下还有多个子变量,我们可以称之为二级变量或者子键,分别对应着不同的值,这些子键的调用需要上级主键的配合使用才能完成变量调用,其格式为
{{ main_var[ "sub_var" ] }}


编写
YAML
格式
playbook
文件如下:

[root@Centos7T ~]#cat blog.yml
- hosts: [websvr]
user: root
tasks:
- name: create new file
shell: echo {{ ansible_fqdn }} > /root/newfile.txt
- name: get ip_addr
shell: echo {{ ansible_default_ipv4[ "address" ] }} >> /root/newfile.txt
- name: get mac_addr
shell: echo {{ ansible_default_ipv4[ "macaddress" ] }} >> /root/newfile.txt


运行过程如下:

[root@Centos7T ~]#ansible-playbook blog.yml

PLAY [websvr] ******************************************************************

TASK [Gathering Facts] *********************************************************
ok: [192.168.25.51]
ok: [192.168.25.80]

TASK [create new file] *********************************************************
changed: [192.168.25.51]
changed: [192.168.25.80]

TASK [get ip_addr] *************************************************************
changed: [192.168.25.51]
changed: [192.168.25.80]

TASK [get mac_addr] ************************************************************
changed: [192.168.25.51]
changed: [192.168.25.80]

PLAY RECAP *********************************************************************
192.168.25.51              : ok=4    changed=3    unreachable=0    failed=0
192.168.25.80              : ok=4    changed=3    unreachable=0    failed=0


运行结果如下:

节点1:
[root@centos7 R1 ~]#cat newfile.txt
centos7.example.com
172.18.42.51
00:50:56:3e:48:5b
节点2:
[root@centos7 ~]#cat newfile.txt
centos7.test.com
172.18.42.80
00:0c:29:1b:23:8d


子键的调用还有另外一种调用格式
{{ main_var.sub_var }}
。运行结果也是一样的。 如下所示:

- name: get ip_addr
shell: echo {{ ansible_default_ipv4.address }} >> /root/newfile.txt
- name: get mac_addr
shell: echo {{ ansible_default_ipv4.macaddress }} >> /root/newfile.txt


关于
Ansibel
的内建变量的使用就简单介绍到这里了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息