Thursday, February 14, 2019

Ansible tags

Lately I got a little confused about properly using Ansible tags so I thought I spend a couple of minutes to create a simple example to show how to use them and also explain the pitfalls (at least how I see them).

Note: this was tested with Ansible 2.5.2 and python 2.7.15

Scenario

My example scenario is simple:
  • I have a small playbook with 4 tasks: one task without tag, two tasks with a different tag each and the 4th task with both tags
    - name: Debug Playbook
      hosts: localhost
      tasks:
        - name: Debug No Tag
          debug:
            msg: "No tag"
    
        - name: Debug Tag A
          debug:
            msg: "Tag A"
          tags:
            - tagA
    
        - name: Debug Tag B
          debug:
            msg: "Tag B"
          tags:
            - tagB
    
        - name: Debug Tag A and B
          debug:
            msg: "Tag A and B"
          tags:
            - tagA
            - tagB
    
  • I am running 4 invocations of the playbook with a mix of --tags and --skip-tags to show the effects on the possible outcomes

Results

  • No tag arguments:
    ansible-playbook playbook.yml
    All tasks are executed.
    TASK [Debug No Tag] *****************************
    ok: [localhost] => {
        "msg": "No tag"
    }
    
    TASK [Debug Tag A] ******************************
    ok: [localhost] => {
        "msg": "Tag A"
    }
    
    TASK [Debug Tag B] ******************************
    ok: [localhost] => {
        "msg": "Tag B"
    }
    
    TASK [Debug Tag A and B] ************************
    ok: [localhost] => {
        "msg": "Tag A and B"
    }
    
  • Supply --tags tagA:
    ansible-playbook --tags tagA playbook.yml
    All tasks are executed where tagA is listed. This works as expected: --tags acts as a filter.
    TASK [Debug Tag A] ******************************
    ok: [localhost] => {
        "msg": "Tag A"
    }
    
    TASK [Debug Tag A and B] ************************
    ok: [localhost] => {
        "msg": "Tag A and B"
    }
    
  • Use both --tags and --skip-tags:
    ansible-playbook --tags tagA --skip-tags tagB playbook.yml
    Only the task tagged tagA is executed. The task with both tags is skipped.
    This actually got me confused. Now when writing this blog it seems obvious: --skip-tags acts as a negative filter i.e. it will skip all tasks where tagB is mentioned.
    TASK [Debug Tag A] ******************************
    ok: [localhost] => {
        "msg": "Tag A"
    }
    
  • Supply --skip-tags tagB:
    ansible-playbook --skip-tags tagB playbook.yml
    All tasks are skipped where tagB is listed, in particular the task without tags is also executed.
    TASK [Debug No Tag] *****************************
    ok: [localhost] => {
        "msg": "No tag"
    }
    
    TASK [Debug Tag A] ******************************
    ok: [localhost] => {
        "msg": "Tag A"
    }
    

To remember

  • As soon as you switch to using tags via --tags none of the untagged tasks will be executed any longer. If you want the untagged task to be executed in any case you need to supply all possible tags of your playbooks.
  • When mixing --tags and --skip-tags in one call both filters work in conjunction: --tags selects all tasks with the given tags(s), --skip-tags reduces this set by all tasks with the given skip tags.

The scenario can get a bit trickier when invoking include sections. I will explain this in another blog.

No comments:

Post a Comment