Reputation: 45
I have ansible playbook with 10 tasks. order of the tasks differs based on use case so I have to create extraVar.yml file for each use case and extraVar.yml will have order of the tasks defined. how to do that.
Example:
playbook:
tasks:
- name task1
- name task2
- name task3
extravar.yml
#order of tasks
task3
task1
task2
Upvotes: 1
Views: 371
Reputation: 68229
Create the generic playbook. For example,
shell> cat pb_generic.yml
- hosts: localhost
tasks:
- debug:
msg: 1
- debug:
msg: 2
- debug:
msg: 3
Create the file with the order of the tasks. For example,
shell> cat extravar.yml
1
2
0
The playbook below reads the generic playbook, creates the list of tasks, and writes a new playbook pb_order.yml
- hosts: localhost
vars:
order_of_tasks: "{{ lookup('file', 'extravar.yml').splitlines() }}"
pb_generic: "{{ lookup('file', 'pb_generic.yml')|from_yaml }}"
list_of_tasks: |
[
{% for i in order_of_tasks %}
{{ pb_generic.0.tasks[i|int] }},
{% endfor %}
]
tasks:
- copy:
dest: "{{ playbook_dir }}/pb_order.yml"
content: |
- {{ pb_generic.0|combine({'tasks': list_of_tasks})|
to_nice_yaml(indent=2)|indent(2) }}
gives
shell> cat pb_order.yml
- hosts: localhost
tasks:
- debug:
msg: 2
- debug:
msg: 3
- debug:
msg: 1
The created playbook will run the tasks in the required order.
Upvotes: 0
Reputation: 12189
This feels like a total antipattern put you could but your tasks in to seperate task files and then iterate over the list of tasks and include those task files. Since your iterating over the list they will be imported in the order of the list.
If you change the order of the vars in the tasks variable, the order of the tasks will change
VAR FILE
---
tasks:
- task2
- task3
- task1
Sample task file
---
- debug:
msg: this is task1
Playbook
---
- name: Creating the multi users with a new approach
hosts: localhost
vars_files:
- extravars.yml
tasks:
- name: Run task
include_tasks: "{{ item }}.yml"
loop: "{{ tasks }}"
OUTPUT
TASK [Run task] ***************************************************************************************************************************************************************************
Sunday 23 October 2022 14:31:13 +0100 (0:00:01.094) 0:00:01.205 ********
included: /local/task2.yml for localhost => (item=task2)
included: /local/task3.yml for localhost => (item=task3)
included: /local/task1.yml for localhost => (item=task1)
TASK [debug] ******************************************************************************************************************************************************************************
Sunday 23 October 2022 14:31:14 +0100 (0:00:00.159) 0:00:01.365 ********
ok: [localhost] => {
"msg": "this is task2"
}
TASK [debug] ******************************************************************************************************************************************************************************
Sunday 23 October 2022 14:31:14 +0100 (0:00:00.047) 0:00:01.412 ********
ok: [localhost] => {
"msg": "this is task3"
}
TASK [debug] ******************************************************************************************************************************************************************************
Sunday 23 October 2022 14:31:14 +0100 (0:00:00.045) 0:00:01.458 ********
ok: [localhost] => {
"msg": "this is task1"
}
PLAY RECAP ********************************************************************************************************************************************************************************
localhost : ok=7 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Sunday 23 October 2022 14:31:14 +0100 (0:00:00.068) 0:00:01.527 ********
===============================================================================
gather_facts ------------------------------------------------------------ 1.09s
debug ------------------------------------------------------------------- 0.16s
include_tasks ----------------------------------------------------------- 0.16s
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
total ------------------------------------------------------------------- 1.42s
for an extra bonus point you could even pass the task order in at runtime and it will override any order that was in the vars file
ansible-playbook main.yml -e '{"tasks": ["task3", "task1", "task2"]}'
Upvotes: 2