On the same server, how to run a role twice with different arguments

Hello all,

I am new with ansible and trying to understand how, on the same server, to run a role twice with different arguments.

What I am trying to do is to install twice the same software but using a different path.

I have tried to use the host file like:

[grp1]
192.168.1.10

[grp2]
192.168.1.10

and then set the group_vars like:

group_vars/grp1:

The group vars will overwrite each other - if a host is in two groups that share vars.
Try putting a dictionary car in the host vars for the host:

install_paths:
  - '/opt/install1'
  - '/opt/install2'

Then in the task use with_items: install_paths (not sure what task you are using so here using the copy module for example)

- copy
   src: localsrc
   dest: "{item}"
   with_items: install_paths

That should do it as item is replaces with each element in the with_items.

Darn auto correct - should read dictionary var not car in the above! ; )

George

Another method you could use is passing the variable value directly into the role when you call it as such

---
- name: SomePlaybook
hosts: yourgroup
roles:

  • {role: yourrole, install_path: ‘/opt/install1’ }
  • {role: yourrole, install_path: ‘/opt/install2’ }

Thank you George and Jonathan for your feedback,

I like the idea of the roles in the playbook as the installation is a bit more complex than explained in the email. Thank you for the tip, I will try it write now :slight_smile:

Regards,
Olivier

Obvious for some, helpful for others, a more condensed version could look like:

- name: SomePlaybook hosts: yourgroup roles:`

  • { role: yourrole, install_path: [ ‘/opt/install1’, ‘/opt/install2’ ] }

`