Pass conditional value to environment variable based on host in Ansible playbook

121 views
Skip to first unread message

Parth Gandhi

unread,
Sep 27, 2017, 6:00:18 AM9/27/17
to Ansible Project
Hi,
I need to pass conditional value based on the host in the same group to environment variable in playbook. IS there a way to do it? 
Here is the use case. I have a host file with a group having 2 servers

hosts:
[rancher]
hostA
hostB

rancher_create.yaml:
- hosts: xyz
  docker_container:
    image: xyz
    env:
      IP: {{ hostA_IP }}

the IP environment variable should take host A ip for HostA  and hostB ip for HostB. Both the servers are used for same service in cluster and for cluster to setup there is a need to pass the individual IP to the docker container cmd.

Thanks,
Parth

Mauricio Tavares

unread,
Sep 27, 2017, 6:10:57 AM9/27/17
to ansible...@googlegroups.com
http://docs.ansible.com/ansible/latest/intro_inventory.html#host-variables


> Thanks,
> Parth
>
> --
> 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/6509f789-02d5-4380-9a90-13dea10338be%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Parth Gandhi

unread,
Sep 27, 2017, 7:34:38 AM9/27/17
to Ansible Project
Thanks, I was able to solve it using {{ ansible_eth0.ipv4.address }}. However your link was very helpful in understanding how the variables work.
Reply all
Reply to author
Forward
0 new messages