well I guess the number of replies says it all: I must be trying to do 
something the language does not support.  Ansible conditions operate on 
"include_vars" - include* statements in ansible are dynamic.  Similarly, 
import* statements in ansible are static.  Tags are applicable to 
statically imported constructs; tags are not applicable to dynamic 
inclusions such as include_roles or include_tasks.

Therefore, since ansible conditionals are applicable to dynamic constructs, 
and ansible tags are applicable to static constructs; the two are mutually 
exclusive.  The language does not support the ability to tag variables that 
are included via include_vars.  Bummer.

This begs the question: how does one conditionally include vars files in 
tagged tasks in ansible??  Anybody?

On Friday, May 29, 2020 at 11:24:28 AM UTC-4, misterT1958 wrote:
>
> Hey folks,
>
> I have run into a problem that I have not been able to solve -- I hope 
> somebody out there has had better luck than I have.  Here's the setup:
>
> 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
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/6b850a6d-6a5e-4dd1-8ff0-6f204f603f5e%40googlegroups.com.

Reply via email to