如何使用Ansible查看hostvars等配置值
首先
可以通过以下方法轻松检查在执行Ansible playbook时的hostvars和其他设置值。
此外,如果在playbook内将gather_facts: no注释掉或者设置为yes,还可以输出setup信息(目标配置的信息)。
环境
ansible --version
ansible 2.1.0 (devel c600ab81ee) last updated 2016/04/20 11:11:25 (GMT +900)
lib/ansible/modules/core: (detached HEAD 98322a777f) last updated 2016/04/20 11:11:54 (GMT +900)
lib/ansible/modules/extras: (detached HEAD 1aecfc1e19) last updated 2016/04/20 11:11:55 (GMT +900)
config file =
configured module search path = Default w/o overrides
样本的库存文件
主持人
[win]
win2012r2-01 ansible_host=192.168.33.201 my_host_var=win2012r2-01
[centos]
centos6-01 ansible_host=192.168.33.202 my_host_var=centos6-01
[win:vars]
ansible_connection=winrm
ansible_port=5985
ansible_user=administrator
ansible_password=PASSWORD
my_grp_var=win
[centos:vars]
ansible_user=vagrant
ansible_private_key_file=~/.ssh/id_rsa
my_grp_var=centos
游戏攻略手册
---
- hosts: all
gather_facts: no
tasks:
- name: "dump variables hostvars"
debug: var=hostvars[inventory_hostname]
- name: "dump variables vars"
debug: var=vars
执行结果
$ansible-playbook -i hosts debug_hostvars_vars.yml
PLAY [all] *********************************************************************
TASK [dump variables hostvars] *************************************************
ok: [win2012r2-01] => {
"hostvars[inventory_hostname]": {
"ansible_check_mode": false,
"ansible_connection": "winrm",
"ansible_host": "192.168.33.201",
"ansible_password": "PASSWORD",
"ansible_port": 5985,
"ansible_user": "administrator",
"ansible_version": {
"full": "2.1.0",
"major": 2,
"minor": 1,
"revision": 0,
"string": "2.1.0"
},
"group_names": [
"win"
],
"groups": {
"all": [
"win2012r2-01",
"centos6-01"
],
"centos": [
"centos6-01"
],
"ungrouped": [],
"win": [
"win2012r2-01"
]
},
"inventory_dir": "/Users/hoge/work/qiita/hots_vars_vars",
"inventory_file": "hosts",
"inventory_hostname": "win2012r2-01",
"inventory_hostname_short": "win2012r2-01",
"my_grp_var": "win",
"my_host_var": "win2012r2-01",
"omit": "__omit_place_holder__e7c09830a34f39579c77b6f6f94085c738f1c62b",
"playbook_dir": "/Users/hoge/work/qiita/hots_vars_vars"
}
}
ok: [centos6-01] => {
"hostvars[inventory_hostname]": {
"ansible_check_mode": false,
"ansible_host": "192.168.33.202",
"ansible_private_key_file": "~/.ssh/id_rsa",
"ansible_user": "vagrant",
"ansible_version": {
"full": "2.1.0",
"major": 2,
"minor": 1,
"revision": 0,
"string": "2.1.0"
},
"group_names": [
"centos"
],
"groups": {
"all": [
"win2012r2-01",
"centos6-01"
],
"centos": [
"centos6-01"
],
"ungrouped": [],
"win": [
"win2012r2-01"
]
},
"inventory_dir": "/Users/hoge/work/qiita/hots_vars_vars",
"inventory_file": "hosts",
"inventory_hostname": "centos6-01",
"inventory_hostname_short": "centos6-01",
"my_grp_var": "centos",
"my_host_var": "centos6-01",
"omit": "__omit_place_holder__e7c09830a34f39579c77b6f6f94085c738f1c62b",
"playbook_dir": "/Users/hoge/work/qiita/hots_vars_vars"
}
}
TASK [dump variables vars] *****************************************************
ok: [win2012r2-01] => {
"vars": "{'inventory_file': 'hosts', 'group_names': [u'win'], 'role_names': [], u'ansible_port': 5985, u'ansible_user': u'administrator', 'ansible_play_hosts': [u'win2012r2-01', u'centos6-01'], u'ansible_connection': u'winrm', 'groups': {'ungrouped': [], u'win': [u'win2012r2-01'], 'all': [u'win2012r2-01', u'centos6-01'], u'centos': [u'centos6-01']}, u'ansible_host': u'192.168.33.201', 'inventory_hostname': u'win2012r2-01', 'inventory_hostname_short': u'win2012r2-01', 'playbook_dir': u'/Users/hoge/work/qiita/hots_vars_vars', 'omit': '__omit_place_holder__e7c09830a34f39579c77b6f6f94085c738f1c62b', 'inventory_dir': '/Users/hoge/work/qiita/hots_vars_vars', u'my_host_var': u'win2012r2-01', 'environment': [], 'hostvars': <ansible.vars.hostvars.HostVars object at 0x10463a890>, u'my_grp_var': u'win', 'ansible_check_mode': False, 'play_hosts': [u'win2012r2-01', u'centos6-01'], u'ansible_password': u'PASSWORD', 'ansible_version': {'major': 2, 'full': '2.1.0', 'string': '2.1.0', 'minor': 1, 'revision': 0}}"
}
ok: [centos6-01] => {
"vars": "{'inventory_file': 'hosts', 'group_names': [u'centos'], u'ansible_user': u'vagrant', 'ansible_play_hosts': [u'win2012r2-01', u'centos6-01'], u'ansible_private_key_file': u'~/.ssh/id_rsa', 'groups': {'ungrouped': [], u'win': [u'win2012r2-01'], 'all': [u'win2012r2-01', u'centos6-01'], u'centos': [u'centos6-01']}, u'ansible_host': u'192.168.33.202', 'role_names': [], 'inventory_hostname': u'centos6-01', 'inventory_hostname_short': u'centos6-01', 'playbook_dir': u'/Users/hoge/work/qiita/hots_vars_vars', 'omit': '__omit_place_holder__e7c09830a34f39579c77b6f6f94085c738f1c62b', 'inventory_dir': '/Users/hoge/work/qiita/hots_vars_vars', u'my_host_var': u'centos6-01', 'environment': [], 'hostvars': <ansible.vars.hostvars.HostVars object at 0x10463a890>, u'my_grp_var': u'centos', 'ansible_check_mode': False, 'play_hosts': [u'win2012r2-01', u'centos6-01'], 'ansible_version': {'major': 2, 'full': '2.1.0', 'string': '2.1.0', 'minor': 1, 'revision': 0}}"
}
PLAY RECAP *********************************************************************
centos6-01 : ok=2 changed=0 unreachable=0 failed=0
win2012r2-01 : ok=2 changed=0 unreachable=0 failed=0
$
其他
在本文中,我们使用debug模块将执行结果直接输出到屏幕上。但是,如果使用template模块,也可以将其作为Yaml格式的文件输出,就像应用在本文中一样。