Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible, running role multiple times with different parameter sets

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.

like image 303
vuliad Avatar asked Sep 26 '15 23:09

vuliad


People also ask

Can Ansible roles be reused by playbooks in a different directory?

Ansible will look here when we want to use our new role in a playbook later in this tutorial. Within this directory we will define roles that can be reused across multiple playbooks and different servers. Each role that we will create requires its own directory.

Does Ansible run sequentially?

They are like a to-do list for Ansible that contains a list of tasks. Playbooks contain the steps which the user wants to execute on a particular machine. Playbooks are run sequentially.

How do I run multiple tasks in Ansible?

If you want to run multiple tasks in a playbook concurrently, use async with poll set to 0. When you set poll: 0 , Ansible starts the task and immediately moves on to the next task without waiting for a result. Each async task runs until it either completes, fails or times out (runs longer than its async value).

How do you use vars in Ansible roles?

When you add a tag to the role option, Ansible applies the tag to ALL tasks within the role. When using vars: within the roles: section of a playbook, the variables are added to the play variables, making them available to all tasks within the play before and after the role.


2 Answers

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 }
like image 199
nikobelia Avatar answered Oct 15 '22 15:10

nikobelia


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
like image 45
Andrew Avatar answered Oct 15 '22 14:10

Andrew