At the top level directory there is an executive shell script that calls a playbook, the particular playbook called is based on inputs to the script. For each playbook there is a corresponding role subdirectory, and each role subdirectory is further divided into a tasks subdirectory and a vars subdirectory. There is one vars file corresponding to each task file. Each role contains a set of unique tasks. If a task is used in more than one playbook then the tasks and vars files are located under the common subdirectory.
The structure of my current ansible hierarchy is shown below. Note that the common subdirectory and the compA and compB subdirectories look similar to the compN subdirectory, but only the compN subdirectory is shown in detail for simplification.
top-level directory:
compA_playbook.yaml
compB_playbook.yaml
…
compN_playbook.yaml
executive.sh
roles
|- common
|- compA
|- compB
…
|- compN
|- tasks
| task1.yaml
| task2.yaml
| …
| taskm.yaml
|- vars
task1.yaml
task2.yaml
…
taskm.yaml
A typical playbook looks like this:
---
- hosts: localhost
vars_files:
- roles/common/vars/fxn_1.yaml
tasks:
- name: function 1
import_tasks: roles/common/tasks/fxn_1.yaml
…
A typical vars file looked like this:
---
fxn_1_bool: true
fxn_1_id: ddtnn35
fxn_1_cfgfile: '"/xyz/home/user2/.functionX.cfg"'
fxn_1_starttime: "2020-09-30 07:30:00"
fxn_1_pauseinterval: 1
and a typical task file looked like this:
---
- name: set downtime
shell: >
/opt/bin/set_downtime.py {{ item }} downtime add
-t {{fxn_1_starttime}} -u {{fxn_1_id}};
loop: "{{ query('inventory_hostnames', '{{ HPC }}-spares') }}"
loop_control:
pause: "{{ fxn_1_pauseinterval }}"
when: fxn_1_bool
register: set_fxn_1
tags: [ FXN_1 ]
The code is written so that the user may specify that an entire playbook be executed (by default) OR that one or more tasks within a playbook could be executed by specifying the appropriate tags (each task has a unique tag).
This setup worked fine until I needed to add conditionals to choose between var files for certain tasks. I could only get the conditionals to work by replacing “vars_files:” with “include_vars:”; so now a typical playbook looks like this:
---
- hosts: localhost
tasks:
- include_vars: roles/common/vars/fxn_1.yaml
when: newVar1 == true or newVar2 == true
- include_vars: /some/other/dir/fxn_1.yaml
when: newVar1 == false and newVar2 == false
- name: function 1
import_tasks: roles/common/tasks/fxn_1.yaml
The conditional clauses work fine – the correct var file is chosen based on the values on newVar1 and newVar2. The problem comes when I use tags. When I specify a tag the tag is only associated with the task, but NOT associated with the include_vars, so when the task executes it fails because the variables are undefined.
I need some way to attach the tag to the variable files as well as the tasks files. I have tried using ansible block, but couldn’t get that to work. Any input appreciated!
misterT