Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible: How to use variables defined in inventory file (hosts) in my playbook?

As the subject says. I have some host variables defined in my hosts inventory file. How do I access them in my playbook?

Here is an example. Based on all my research I was expecting foo and bar to be part of hostvars. I can put host specific variables in separate var files, but I would love to keep them in my inventory file "attached" to a host. I don't want to use it in templates. ansible version: 1.3.2, ansible_distribution_version: 6.4

bash $
bash $ ansible --version
ansible 1.3.2
bash $
bash $ cat test_inv.ini

[foobar]
someHost foo="some string" bar=123
someOtherHost foo="some other string" bar=456


bash $
bash $ cat test.yml

---

- name: test variables...
  hosts: all
  vars:
    - some_junk: "1"
#  gather_facts: no # foo and bar are unavailable whether I gather facts or not.
  tasks:
    - debug: msg="hostvars={{hostvars}}"
    - debug: msg="vars={{vars}}"
    - debug: msg="groups={{groups}}"
    - debug: msg="some_junk={{some_junk}}"
#    - debug: msg="???? HOW DO I PRINT values of host specific variables foo and bar defined in inventory file ???"

bash $
bash $
bash $ ansible-playbook -i test_inv.ini test.yml

PLAY [test variables...] ******************************************************

GATHERING FACTS ***************************************************************
ok: [someHost]

TASK: [debug msg="hostvars={{hostvars}}"] *************************************
ok: [someHost] => {"msg": "hostvars={'someHost': {u'facter_operatingsystem': u'RedHat', u'facter_selinux_current_mode': u'enforcing', u'facter_hostname': u'someHost', 'module_setup': True, u'facter_memoryfree_mb': u'1792.70', u'ansible_distribution_version': u'6.4' // ...........snip...........// u'VMware IDE CDR10'}}"}

TASK: [debug msg="vars={{vars}}"] *********************************************
ok: [someHost] => {"msg": "vars={'some_junk': '1', 'delegate_to': None, 'changed_when': None, 'register': None, 'inventory_dir': '/login/sg219898/PPP/automation/ansible', 'always_run': False, 'ignore_errors': False}"}

TASK: [debug msg="groups={{groups}}"] *****************************************
ok: [someHost] => {"msg": "groups={'ungrouped': [], 'foobar': ['someHost'], 'all': ['someHost']}"}

TASK: [debug msg="some_junk=1"] ***********************************************
ok: [someHost] => {"msg": "some_junk=1"}

PLAY RECAP ********************************************************************
someHost                   : ok=5    changed=0    unreachable=0    failed=0  

bash $ 
like image 307
Kashyap Avatar asked Nov 19 '13 14:11

Kashyap


People also ask

How do you define host vars in Ansible?

Ansible uses a combination of a hosts file and a group_vars directory to pull variables per host group and run Ansible plays/tasks against hosts. group_vars/all is used to set variables that will be used for every host that Ansible is ran against.

How do you use vars in Ansible playbook?

Playbook variables are quite easy and straightforward. To define a variable in a playbook, simply use the keyword vars before writing your variables with indentation. To access the value of the variable, place it between the double curly braces enclosed with quotation marks.


1 Answers

Doing the following should work:
debug: msg="foo={{foo}}"

The foo variable will be evaluated in the context of the current host. Tested locally with ansible 1.3.4.

like image 143
Jake Kreider Avatar answered Oct 10 '22 16:10

Jake Kreider