Ansible, running role multiple times with different parameter sets

ansible roles
ansible with_items
include_role ansible

What is the best practice for running one role with different set of parameters?

I need to run one application(docker container) multiple times on one server with different environment variables for each.

There's limitations in the Ansible docs when it comes to this kind of thing - if there's an official best practice, I haven't come across it.

One good way that keeps your playbooks nice and readable is running several different plays against the host and calling the role with different parameters in each.

The role: foo, var: blah syntax shown a little way into this description is a good way to pass parameters in, and keeps it clear at a glance what is going on. For example:

- name: Run the docker role with docker_container_state=foo
  hosts: docker-host
  roles:
  - { role: docker_container, docker_container_state: foo }

- name: Run the docker role with docker_container_state=bar
  hosts: docker-host
  roles:
  - { role: docker_container, docker_container_state: bar }

Roles, Ansible will only allow a role to execute once, even if defined multiple times, if the parameters defined on the role are not different for each definition. Role default variables allow you to set default variables for included or dependent roles  Given the above, the role foo will only be run once. To make roles run more than once, there are two options: Pass different parameters in each role definition. Add allow_duplicates: true to the meta/main.yml file for the role. Example 1 - passing different parameters:

I usually use includes to run part of the role (or a whole role!) multiple times, if i have a decent layout of variables. See the example playbook below, with role apply_state which has print_state.yml inside roles/apply_state/tasks folder. The trick is to pass item inside include, after that it's a piece of cake.

playbook.yml

- hosts: localhost
  roles:
    - { role: apply_state, states: [ state_one, state_two, state_three ] }

roles/apply_state/tasks/main.yml

- name: print all states!
  include: print_state.yml state="{{ item }}"
  with_items: "{{ states }}" 

roles/apply_state/tasks/print_state.yml

- name: echo state
  debug: msg="{{ state }}"

See the output of ansible-playbook -i localhost, playbook.yml below:

PLAY [localhost] ***************************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [apply_state : print all states!] *****************************************
included: /home/user/roles/apply_state/tasks/print_state.yml for localhost
included: /home/user/roles/apply_state/tasks/print_state.yml for localhost
included: /home/user/roles/apply_state/tasks/print_state.yml for localhost

TASK [apply_state : echo state] ************************************************
ok: [localhost] => {
    "msg": "state_one"                                                                                                                 
}                                                                                                                                      

TASK [apply_state : echo state] ************************************************
ok: [localhost] => {
    "msg": "state_two"                                                                                                                 
}                                                                                                                                      

TASK [apply_state : echo state] ************************************************
ok: [localhost] => {
    "msg": "state_three"                                                                                                               
}                                                                                                                                      

PLAY RECAP *********************************************************************
localhost                  : ok=7    changed=0    unreachable=0    failed=0

Ansible variable for roles used several times, Running a role multiple times in one playbook; Using role dependencies and can be easily overridden by any other variable, including inventory variables. If you store your roles in a different location, set the roles_path configuration option​  I'm running Ansible 1.9.3 and have been unable to get the role dependency wheel/tire example from the Ansible documentation working. $ ansible --version ansible 1.9.3 configured module search path = None Here are my files:

In case you need the following information,

Sometimes, passing arguments to an Ansible role is an artificial way to run it multiple times effectively.

A typical use case is to restart an application several times, in the same playbook, in the process of installing it, with a different configuration each time. By Default, Ansible will consider that the restarting role has already been played and will not replay it. This must have something to do with idempotence.

The solution is to add the following property to the meta/main.yml of the role to be executed multiple times:

allow_duplicates: true

and you're good to go!

Roles are ran multiple times if both referenced as dependency and , This is expected with roles defined in the roles section. Use include_role or import_role module in tasks (or pre_tasks ) to avoid the problem: As a workaround you can add allow_duplicates: false to prevent Ansible from running the same role twice with the same parameters. Clearly the module is looped twice: once with hosts, the other time with the specified items.

On the same server, how to run a role twice with different arguments , #20169 - Role dependency is run multiple times This causes a state "loop" each time I run my ansible playbook, flip flopping if defined multiple times, if the parameters defined on the role are not different for each definition. Every role must set a fact at the end of its execution, this way we can handle  Create and Run Your First Network Ansible Playbook ¶ If you want to run this command every day, you can save it in a playbook and run it with ansible-playbook instead of ansible. The playbook can store a lot of the parameters you provided with flags at the command line, leaving less to type at the command line.

Playbook Roles and Include Statements, On the same server, how to run a role twice with different arguments I am new with ansible and trying to understand how, on the same server, to run a role twice with different arguments. and then set the group_vars like: Variables are set on a host-by-host basis just like facts discovered by the setup module. These variables will be available to subsequent plays during an ansible-playbook run. Set cacheable to yes to save variables across executions using a fact cache. Variables created with set_fact have different precedence depending on whether they are or

How to Include Tasks in Ansible + Examples, Playbooks can also include plays from other playbook files. (In addition to the explicitly passed-in parameters, all variables from the vars section are also See Configuration file for details about how to set this up in ansible.cfg. of the role at different times, you should consider just splitting that role into multiple roles. Running the p4.yml playbook on two hosts and interpreting the output. The same command can be run with the –check parameter for a dry-run. In case you want to use password authentication, use -k parameter. Explanation: Ansible-playbook command that runs p4.yml; Playbook skipsSELinux role because it is already enabled. Ansible found that httpd

Comments
  • Not sure if the above syntax is valid anymore. To pass single/multiple variables to different executions of a role, check Example 1 - passing different parameters: in this official doc reference: docs.ansible.com/ansible/latest/user_guide/….