一、Ansible内置了一些变量以方便主机之间相互调用各自的变量。这些变量包括:shell
* hostvars容许你访问另外一个主机的变量,固然前提是ansible已经收集到这个主机的变量了:ide
group_names:是当前主机所在的group列表测试
groups:是全部inventory的group列表spa
inventory_hostname:是在inventory里定义的主机名(ip或主机名称)debug
play_hosts是当前的playbook范围内的主机列表ip
inventory_dir和inventory_file是定义inventory的目录和文件it
二、测试内置变量ansible-playbook:class
---变量
- hosts: '` hosts `'file
user: ansible
sudo: yes
sudo_user:root
tasks:
- name: Getthe all vars
shell:echo "` group_names `"
#shell:echo "` play_hosts `"
#shell:echo "` inventory_hostname `"
register:res
- debug:var=res.stdout_lines
三、以上tasks中能够去掉对应#分别测试,执行查看结果:
$ ansible-playbook -i ./hosts playhosts.yml -e 'hosts=all'