# How to use --extra-vars option to pass host variables in ansible-playbook

**URL:** <https://forum.ansible.com/t/how-to-use-extra-vars-option-to-pass-host-variables-in-ansible-playbook/20678>\
**Category:** Ansible Project\
**Created:** [February 25, 2016, 3:00pm UTC](https://forum.ansible.com/t/how-to-use-extra-vars-option-to-pass-host-variables-in-ansible-playbook/20678 "2016-02-25T15:00:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![DINESH\_S](https://avatars.discourse-cdn.com/v4/letter/d/e19b73/32.png) [@DINESH\_S](https://forum.ansible.com/u/DINESH_S)\
**Post date:** [February 25, 2016, 3:00pm UTC](https://forum.ansible.com/t/how-to-use-extra-vars-option-to-pass-host-variables-in-ansible-playbook/20678/1 "2016-02-25T15:00:19Z")

</div>

We have an requirement to pass the host variables defined in a file under host\_vars directory as an argument to the ansible playbook. So we have planned to use **–extra-vars** option to pass additional variables as key value pair. But we couldn’t able to pass the host variables as argument and I couldn’t find any proper documentation for this use case.

Could anyone give me a solution for this problem.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [February 25, 2016, 7:02pm UTC](https://forum.ansible.com/t/how-to-use-extra-vars-option-to-pass-host-variables-in-ansible-playbook/20678/2 "2016-02-25T19:02:01Z")

</div>

host\_vars do not need to be passed, they are picked up automatically, you would be ‘double passing them’ by adding them to the command line.

---

<div class="post-metadata">

**Author:** ![DINESH\_S](https://avatars.discourse-cdn.com/v4/letter/d/e19b73/32.png) [@DINESH\_S](https://forum.ansible.com/u/DINESH_S)\
**Post date:** [February 29, 2016, 5:56am UTC](https://forum.ansible.com/t/how-to-use-extra-vars-option-to-pass-host-variables-in-ansible-playbook/20678/3 "2016-02-29T05:56:02Z")

</div>

Thanks Brain for your reply.

We are not planning to have the yaml or json file under host\_vars directory. Instead we are passing as arguments in the command line.

Could you please share me some examples or documentation to pass host variables.

---

<div class="post-metadata">

**Author:** ![Alex\_Leonhardt](https://avatars.discourse-cdn.com/v4/letter/a/ec9cab/32.png) [@Alex\_Leonhardt](https://forum.ansible.com/u/Alex_Leonhardt)\
**Post date:** [February 29, 2016, 7:07am UTC](https://forum.ansible.com/t/how-to-use-extra-vars-option-to-pass-host-variables-in-ansible-playbook/20678/4 "2016-02-29T07:07:45Z")

</div>

Then don’t they just become normal variables that you’d use in your playbooks?

Alex

---

<div class="post-metadata">

**Author:** ![DINESH\_S](https://avatars.discourse-cdn.com/v4/letter/d/e19b73/32.png) [@DINESH\_S](https://forum.ansible.com/u/DINESH_S)\
**Post date:** [February 29, 2016, 7:41am UTC](https://forum.ansible.com/t/how-to-use-extra-vars-option-to-pass-host-variables-in-ansible-playbook/20678/5 "2016-02-29T07:41:01Z")

</div>

Yes. If I give the arguments as below, then it would be normal variables or global variables.

ansible-playbook -i hosts xxxx.yaml --extra-vars ‘{“management\_interface”:{“int”: “em0”,“ip”: “192.168.56.3”}}’

But I’m looking for an option in ansible-playbook where we could pass the host variables specific to the particular host as follows.

ansible-playbook -i hosts xxxx.yaml --extra-vars ‘{“hostvars[‘host1’]”:{“management\_interface”:{“int”: “em0”,“ip”: “192.168.56.3”}}}’

The content in hosts file is as below:

[vdc1-host1]  
host1

[vdc1-host2]  
host2

[vdc1:children]  
vdc1-host1  
vdc1-host2

[sites:children]  
vdc1

[all:children]  
sites

Please let me know if you have any queries.

---

<div class="post-metadata">

**Author:** ![Tom\_Bamford1](https://avatars.discourse-cdn.com/v4/letter/t/43a26b/32.png) [@Tom\_Bamford1](https://forum.ansible.com/u/Tom_Bamford1)\
**Post date:** [February 29, 2016, 3:00pm UTC](https://forum.ansible.com/t/how-to-use-extra-vars-option-to-pass-host-variables-in-ansible-playbook/20678/6 "2016-02-29T15:00:43Z")

</div>

Have you considered other ways of specifying that information, such as a dynamic inventory script, or a custom facts module?

Tom

---

<div class="post-metadata">

**Author:** ![DINESH\_S](https://avatars.discourse-cdn.com/v4/letter/d/e19b73/32.png) [@DINESH\_S](https://forum.ansible.com/u/DINESH_S)\
**Post date:** [February 29, 2016, 4:14pm UTC](https://forum.ansible.com/t/how-to-use-extra-vars-option-to-pass-host-variables-in-ansible-playbook/20678/7 "2016-02-29T16:14:22Z")

</div>

No. I don’t have idea in specifying it as dynamic inventory script or custom facts module.

Could you please provide me an examples or documentation.
