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

2,147 views
Skip to first unread message

Olivier Lauret

unread,
Mar 23, 2015, 9:49:18 AM3/23/15
to ansible...@googlegroups.com
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:
   ---
   install_path = '/opt/install1'

group_vars/grp2:
   ---
   install_path = '/opt/install2'

The problem is that my playbook always get "install_path = '/opt/install1'" not matter if I only include only grp2.

Any idea to solve this issue?

Regards,
Olivier

George Boobyer

unread,
Mar 23, 2015, 2:17:49 PM3/23/15
to ansible...@googlegroups.com
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.

George Boobyer

unread,
Mar 23, 2015, 2:19:04 PM3/23/15
to ansible...@googlegroups.com
Darn auto correct - should read dictionary var not car in the above! ; )

George

Jonathan Davila

unread,
Mar 23, 2015, 3:18:49 PM3/23/15
to ansible...@googlegroups.com
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' }

Olivier Lauret

unread,
Mar 23, 2015, 7:03:22 PM3/23/15
to ansible...@googlegroups.com
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 :)

Regards,
Olivier

Dan Vaida

unread,
Mar 28, 2015, 1:53:23 PM3/28/15
to ansible...@googlegroups.com
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' ] }
Reply all
Reply to author
Forward
0 new messages