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.
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.
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.
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).
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.
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 }
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With