Help us understand the problem. What is going on with this article?

Ansibleでhostvars等の設定値を確認する方法

More than 3 years have passed since last update.

はじめに

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形式のファイルとして出力することも可能です。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした