Bootstrap

Ansible—— 31. playbook 常用的内置变量

1. ansible_version
ansible master -m debug -a "msg={{ansible_version}}"
2. hostvars
---
- name: "play 1: Gather facts of test71"
  hosts: test71
  remote_user: root
 
- name: "play 2: Get facts of test71 when operating on master"
  hosts: master
  remote_user: root
  tasks:
  - debug:
      msg: "{{hostvars['test71'].ansible_ens35.ipv4}}"
           #"{{hostvars.test71.ansible_ens35.ipv4}}"
---
- hosts: test71
  remote_user: root
  gather_facts: no
  tasks:
  - shell: "echo register_var_in_play1"
    register: shellreturn
 
- hosts: master
  remote_user: root
  gather_facts: no
  tasks:
  - debug:
      msg: "{{hostvars.test71.shellreturn.stdout}}"

报错,vars不能跨主机引用

---
- hosts: test71
  remote_user: root
  gather_facts: no
  vars:
    testvar: testvar_in_71
  tasks:
  - debug:
      msg: "{{testvar}}"
 
- hosts: master
  remote_user: root
  gather_facts: no
  tasks:
  - debug:
      msg: "{{hostvars.test71.testvar}}"
---
- hosts: test71
  remote_user: root
  gather_facts: no
  tasks:
  - set_fact:
      testvar: "testvar_in_71"
  - debug:
      msg: "{{testvar}}"
 
- hosts: master
  remote_user: root
  gather_facts: no
  tasks:
  - debug:
      msg: "{{hostvars.test71.testvar}}"
3. inventory_hostname
[test_group]
10.1.1.60
master.zsythink.net ansible_host=10.1.1.70
test71 anisble_host=10.1.1.71
ansible test_group -m debug -a "msg={{inventory_hostname}}"
4 inventory_hostname_short
5. play_hosts

获取当前操作的所有主机名

6. groups
ansible master -m debug -a "msg={{groups.test}}"
ansible master -m debug -a "msg={{groups['test']}}"
ansible master -m debug -a "msg={{groups.ungrouped}}"
7. group_names

获取主机所在组的名称

8. inventory_dir

获取主机清单目录

————Blueicex 2020/3/27 19:06 [email protected]

;