opticyclic
opticyclic

Reputation: 8116

How Do I Make An Ansible Playbook Skip A Role?

I am trying to skip the upgrade part of my playbook. The key part looks like this:

hosts: linux_group
name: Upgrade the third-party application
roles:
  - role: "upgradeEnv"
    when: ENV == inventory_hostname
vars:
  logdir: "/home/appuser/external/logs"
become_user: "{{ sudoUser }}"
become_method: sudo
become: yes
tags:
  - upgrade

And the key part of the role looks like this:

- name: Upgrade database
  shell: "upgradeDB.sh {{ env }}"
  vars:
    DBURL: "{{ user }}@{{ host }}"
  no_log: True
  register: register_appupgrade
  tags:
    - upgrade

- fail:
    msg: "Upgrade errors:"
  when: register_appupgrade.stderr !=""
  tags:
    - upgrade 

I run the playbook with --skip-tags=upgrade but ansible still goes into the role and runs the tasks so I end up with tags: upgrade specified on each task.

The Upgrade database gets skipped but the fail ends the run due to the when condition.

Why is the role being run from the playbook even when the tags are supposed to be skipped?

Why is the fail task not being skipped?

Upvotes: 2

Views: 735

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 68034

Given the project for testing

shell> tree .
.
├── ansible.cfg
├── hosts
├── pb.yml
└── roles
    └── upgradeEnv
        └── tasks
            └── main.yml

3 directories, 4 files
shell> cat ansible.cfg 
[defaults]
inventory = $PWD/hosts
roles_path = $PWD/roles
remote_tmp = ~/.ansible/tmp
retry_files_enabled = false
stdout_callback = yaml
shell> cat hosts
[linux_group]
test_11
test_13

ansible [core 2.14.1]

The tags keyword means the tags are applied to all tasks at the indentation level.

  1. If you apply tags at the play level
shell> cat pb.yml
- hosts: linux_group
  roles:
    - role: upgradeEnv
      when: ENV == inventory_hostname
  tags: upgrade

everything will be skipped

shell> ansible-playbook pb.yml --skip-tags=upgrade

PLAY [linux_group] ***************************************************************************

PLAY RECAP ***********************************************************************************

  1. If you apply tags at the role level
shell> cat pb.yml
- hosts: linux_group
  roles:
    - role: upgradeEnv
      when: ENV == inventory_hostname
      tags: upgrade

the role will be skipped

shell> ansible-playbook pb.yml --skip-tags=upgrade

PLAY [linux_group] ***************************************************************************

TASK [Gathering Facts] ***********************************************************************
ok: [test_13]
ok: [test_11]

PLAY RECAP ***********************************************************************************
test_11: ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test_13: ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=
  1. You'll see the same result if you omit tags at role level and apply tags at each task in the role
shell> cat pb.yml
- hosts: linux_group
  roles:
    - role: upgradeEnv
      when: ENV == inventory_hostname
shell> cat roles/upgradeEnv/tasks/main.yml
- name: Upgrade database
  command: "echo {{ env }}"
  register: register_appupgrade
  tags: upgrade

- debug:
    msg: |
      register_appupgrade.stdout: {{ register_appupgrade.stdout }}
      register_appupgrade.stderr: {{ register_appupgrade.stderr }}
  tags: upgrade

- fail:
    msg: Upgrade errors
  when: register_appupgrade.stderr != ""
  tags: upgrade
  1. If you don't skip tags the play works as expected
shell> ansible-playbook pb.yml -e ENV=test_11 -e env=test

PLAY [linux_group] ***************************************************************************

TASK [Gathering Facts] ***********************************************************************
ok: [test_11]
ok: [test_13]

TASK [upgradeEnv : Upgrade database] *********************************************************
skipping: [test_13]
changed: [test_11]

TASK [upgradeEnv : debug] ********************************************************************
skipping: [test_13]
ok: [test_11] => 
  msg: |-
    register_appupgrade.stdout: test
    register_appupgrade.stderr:

TASK [upgradeEnv : fail] *********************************************************************
skipping: [test_11]
skipping: [test_13]

PLAY RECAP ***********************************************************************************
test_11: ok=3    changed=1    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
test_13: ok=1    changed=0    unreachable=0    failed=0    skipped=3    rescued=0    ignored=0

Upvotes: 4

Related Questions