はじめに
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
サンプルのインベントリファイル
hosts
[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
playbook
debug_hostvars_vars.yml
---
- 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形式のファイルとして出力することも可能です。