Running roles for every host mentioned in inventory file

18 views
Skip to first unread message

Vishal Bobade

unread,
Apr 30, 2020, 4:01:48 AM4/30/20
to Ansible Project
HI , 

I have my inventory file like below , where I am using the variables primaryNode, secondaryNode in the roles so that particular role will run on the host specified infront of ansible_host values.

Now, how can i pass multiple hosts in the inventory under secondaryNode so that role2 will run on all those nodes one by one. and I will not need to create role3 or role 4 for those.


inventory file 


all:
 children:
  ha:
   hosts:
    primaryNode:
      ansible_host: domain.net1
    secondaryNode:
      ansible_host: domain.net2


my yaml looks liike elow :


---- name: Installing primary node  hosts: primaryNode  become: True  roles:
- role: role1
- name: Installing secondary node hosts: secondaryNode become: True roles:
- role: role2

Neha Pithadiya

unread,
May 2, 2020, 6:20:49 AM5/2/20
to Ansible Project
I think its not possible to pass multiple values to "ansible_host" variable . You can create "group" in inventory file with all the hosts listed under it.

Thanks
Neha Pithadiya.

David Foley

unread,
May 2, 2020, 8:42:48 AM5/2/20
to Ansible Project
Not sure if it's possible with ansbile but with Salt you can set Grains and do if else statements based on those grains

{% if role[host1] }
- role: host1
{% elif role[hoat2] %}
-role: host 2
{%endif%}

I would look up condition s for ansible

Stefan Hornburg (Racke)

unread,
May 2, 2020, 9:36:50 AM5/2/20
to ansible...@googlegroups.com
Don't know about virtual grains, but you can apply when: conditions

- role: role1
when: "'role1' in group_names"

Regards
Racke

--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc
Reply all
Reply to author
Forward
0 new messages