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

2,539 views
Skip to first unread message

DINESH S

unread,
Feb 25, 2016, 12:37:18 PM2/25/16
to Ansible Project
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.

Brian Coca

unread,
Feb 25, 2016, 2:02:10 PM2/25/16
to ansible...@googlegroups.com
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.


----------
Brian Coca

DINESH S

unread,
Feb 29, 2016, 12:56:02 AM2/29/16
to Ansible Project
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.

Alex Leonhardt

unread,
Feb 29, 2016, 2:07:57 AM2/29/16
to ansible...@googlegroups.com
Then don't they just become normal variables that you'd use in your playbooks?

Alex
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/58630062-7d6f-4499-af1b-90415b070ad6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dinesh Sekar

unread,
Feb 29, 2016, 2:41:01 AM2/29/16
to Ansible Project
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.



On Monday, February 29, 2016 at 12:37:57 PM UTC+5:30, Alex Leonhardt wrote:
Then don't they just become normal variables that you'd use in your playbooks?

Alex

On Monday, 29 February 2016, DINESH S <msdin...@gmail.com> wrote:
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.

On Friday, February 26, 2016 at 12:32:10 AM UTC+5:30, Brian Coca wrote:
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.


----------
Brian Coca

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.

Tom Bamford

unread,
Feb 29, 2016, 10:01:00 AM2/29/16
to ansible-project@googlegroups com
On 29 Feb 2016, at 9:41 AM, Dinesh Sekar <msdin...@gmail.com> wrote:
>
> 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.

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

Tom

Dinesh Sekar

unread,
Feb 29, 2016, 11:14:23 AM2/29/16
to Ansible Project
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.
Reply all
Reply to author
Forward
0 new messages