内置变量ansible_version获取到ansible的版本号
ansible test70 -m debug -a "msg={{ansible_version}}"
内置变量hostvars操作当前主机时获取到其他主机中的信息
- name: "play 1: Gather facts of test71"
hosts: test71 remote_user: root - name: "play 2: Get facts of test71 when operating on test70" hosts: test70 remote_user: root tasks: - debug: msg: "{{hostvars.test71.ansible_ens35.ipv4}}"########################################################
- hosts: test71
remote_user: root gather_facts: no tasks: - set_fact: testvar: "testvar_in_71" - debug: msg: "{{testvar}}" - hosts: test70 remote_user: root gather_facts: no tasks: - debug: msg: "{{hostvars.test71.testvar}}"通过"set_fact"结合"hostvars"的方式,实现了跨play获取其他主机中的变量信息的功能内置变量inventory_hostname对应主机在清单中配置的名称
all:
children:PA:hosts:jack6_2:ansible_host: 192.168.179.137ansible_ssh_port: 22jack6_1:ansible_host: 192.168.179.136ansible_ssh_port: 22PB: hosts:jack7_1:ansible_host: 192.168.179.149ansible_ssh_port: 22PC:hosts:jack7:ansible_host: 192.168.179.135ansible_ssh_port: 22[[email protected] work]# ansible jack7_1 -m debug -a "msg={{inventory_hostname}}"jack7_1 | SUCCESS => {"msg": "jack7_1"}内置变量inventory_hostname_short 简短名称
[[email protected] work]# ansible jack7_1 -m debug -a "msg={{inventory_hostname_short}}"
jack7_1 | SUCCESS => {"msg": "jack7_1"}内置变量play_hosts获取到当前play所操作的所有主机的主机名列表
[[email protected] work]# ansible PA -m debug -a "msg={{play_hosts}}"
jack6_2 | SUCCESS => {"msg": ["jack6_2", "jack6_1"]}jack6_1 | SUCCESS => {"msg": ["jack6_2", "jack6_1"]}内置变量groups获取到清单中"所有分组"的"分组信息"
[[email protected] work]# ansible jack7_1 -m debug -a "msg={{groups}}"
jack7_1 | SUCCESS => {"msg": {"PA": ["jack6_2", "jack6_1"], "PB": ["jack7_1"], "PC": ["jack7"], "all": ["jack7_1", "jack7", "jack6_2", "jack6_1"], "ungrouped": []}}内置变量group_names获取到当前主机所在分组的组名
[[email protected] work]# ansible jack7_1 -m debug -a "msg={{group_names}}"
jack7_1 | SUCCESS => {"msg": ["PB"]}内置变量inventory_dir获取到ansible主机中清单文件的存放路径
[[email protected] work]# ansible jack7_1 -m debug -a "msg={{inventory_dir}}"
jack7_1 | SUCCESS => {"msg": "/etc/ansible"}
精彩评论