Ansible中內建变量的使用
来源:互联网 发布:管家婆数据恢复 编辑:程序博客网 时间:2024/05/22 14:10
Ansible中內建变量的使用
Ansible
是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。在Ansible
中,我们也可以使用变量以提高Ansible
针对不同场景下使用的灵活性。 Ansible
使用的变量包括两种,內建变量和自定义变量。本文针对Ansible
的內建变量的使用进行简单介绍。
Ansible
的內建变量通常需要Ansible
获取远程节点信息后自动构建而成,且该变量的值只对当前远程节点有效。我们可以使用Ansible
的setup
模块查看指定节点的內建变量信息:
[root@Centos7T ~]#ansible 192.168.25.51 -m setup192.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.51192.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.ymlPLAY [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 ~]#hostnamecentos7.example.com[root@centos7 R1 ~]#cat newfile.txt节点2:[root@centos7 ~]#hostnamecentos7.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 undefinedexception 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.txtcentos7.example.com172.18.42.5100:50:56:3e:48:5b节点2:[root@centos7 ~]#cat newfile.txtcentos7.test.com172.18.42.8000: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
的内建变量的使用就简单介绍到这里了。
- Ansible中內建变量的使用
- Ansible中自定义变量的使用
- Ansible的使用
- ANSIBLE的简单使用
- Ansible的安装与使用
- ansible的安装与使用
- ansible使用
- ansible使用
- 实例学习ansible系列(14)playbook变量使用
- ansible 变量优先级
- ansible 配置变量
- ansible 内嵌变量
- Ansible 的 hostvars/group_names/groups 的使用
- 关于ansible的学习和使用--1
- ansible第一次使用它的配置
- 使用ansible添加zabbix的主机监控
- Ansible的安装与使用初探
- Ansible的使用(一)---安装
- 自顶向下,逐步求精 一些理解
- HTML && CSS 学习笔记(2)超文本(关键词:HTML/超文本/超链接/锚/anchor)
- Java 中 modifer 'public' is reduntant for interface methods
- Miller_Rabin算法
- 小程序的一些小错误的解决办法
- Ansible中內建变量的使用
- GFS Google Storage System/HDFS
- 深入理解Java:注解(Annotation)自定义注解入门
- 二叉树的建立
- 文章标题
- 谈谈架构
- Java开发工程师的面试该用什么姿势?做到这些让你多拿5K
- java单例【转自java团长】
- Javase—数组