how to use ansible tags with conditional includes?

1,388 views
Skip to first unread message

misterT1958

unread,
May 29, 2020, 11:24:28 AM5/29/20
to Ansible Project
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

misterT1958

unread,
Jun 2, 2020, 12:46:19 PM6/2/20
to Ansible Project
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?

Stefan Hornburg (Racke)

unread,
Jun 2, 2020, 3:02:48 PM6/2/20
to ansible...@googlegroups.com
On 6/2/20 6:46 PM, misterT1958 wrote:
> 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?

Why don't you use the inventory for these variables? It is possible to use multiple inventories which coincide with
your current variable files.

Regards
Racke
> fxn_1_starttime:"2020-09-3007:30:00"
>
> fxn_1_pauseinterval: 1
>
>  
>
>  
>
> and a typical task file looked like this:
>
> ---
>
>   -name:set downtime
> --
> 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
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/6b850a6d-6a5e-4dd1-8ff0-6f204f603f5e%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/6b850a6d-6a5e-4dd1-8ff0-6f204f603f5e%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc
Reply all
Reply to author
Forward
0 new messages