Search code examples
ansibleansible-role

Ansilbe Playbook Tasks Running Twice


I've spent an obscene amount of time trying to get this playbook/role to work correctly. When I use the tags I've assigned the tasks in the playbook it runs the tagged task and then all the tasks. I added the "never" tag and it still runs all the tasks in the role but if I don't use any tags it only runs the task I had assigned "always" for testing. I just don't know what I'm missing here since I have other roles/playbooks with tags that run as expected. I should also mention that I'm fairly new to using roles...I have a couple of playbooks I converted to a role with no issues..This one is just giving me fits.

The tasks in the role

---
- setup:
    filter: "{{ item }}"
  loop:
    - 'ansible_distribution*'
    - 'ansible_hostname'
  tags:
    - always

- name: Register to Satellite
  command: "subscription-manager register --org={{ org }} --activationkey={{ reg_key }}"
  tags: [ never, register ]

- name: Unregister to Satellite
  command: "subscription-manager unregister"
  tags: [ never, unregister ]

The playbook I'm using to call the role

---
- hosts: all
  gather_facts: false
  become: true
  roles:
    - role: roles/satellite_tasks
      tags: register
    - role: roles/satellite_tasks
      tags: unregister

The command I'm using to run everything below, using the register and unregister tag

ansible-playbook -i inventory/homelab-inventory playbooks/satellite-tasks.yml --limit all_lab --extra-vars "org=99999999" --extra-vars "reg_key=notmykey"

The output using the register tag

PLAY [all] *************************************************************************************************************************************************************************************************

TASK [satellite_tasks : setup] *****************************************************************************************************************************************************************************
ok: [rhel9-02] => (item=ansible_distribution*)
ok: [rhel9-01] => (item=ansible_distribution*)
ok: [rhel7-02] => (item=ansible_distribution*)
ok: [rhel8-01] => (item=ansible_distribution*)
ok: [rhel7-01] => (item=ansible_distribution*)
ok: [rhel8-02] => (item=ansible_distribution*)
ok: [rhel9-02] => (item=ansible_hostname)
ok: [rhel9-01] => (item=ansible_hostname)
ok: [rhel7-02] => (item=ansible_hostname)
ok: [rhel7-01] => (item=ansible_hostname)
ok: [rhel8-01] => (item=ansible_hostname)
ok: [rhel8-02] => (item=ansible_hostname)

TASK [satellite_tasks : Register to Satellite] *************************************************************************************************************************************************************
changed: [rhel9-02]
changed: [rhel9-01]
changed: [rhel8-02]
changed: [rhel7-02]
changed: [rhel7-01]
changed: [rhel8-01]

TASK [satellite_tasks : Unregister to Satellite] ***********************************************************************************************************************************************************
changed: [rhel9-02]
changed: [rhel9-01]
changed: [rhel7-02]
changed: [rhel8-02]
changed: [rhel7-01]
changed: [rhel8-01]

TASK [satellite_tasks : Register to Satellite] *************************************************************************************************************************************************************
changed: [rhel9-02]
changed: [rhel9-01]
changed: [rhel7-01]
changed: [rhel8-01]
changed: [rhel7-02]
changed: [rhel8-02]

PLAY RECAP *************************************************************************************************************************************************************************************************
rhel7-01                   : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
rhel7-02                   : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
rhel8-01                   : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
rhel8-02                   : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
rhel9-01                   : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
rhel9-02                   : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

Output running the unregister tag

PLAY [all] *************************************************************************************************************************************************************************************************

TASK [satellite_tasks : Unregister to Satellite] ***********************************************************************************************************************************************************
changed: [rhel9-02]
changed: [rhel9-01]
changed: [rhel7-02]
changed: [rhel8-02]
changed: [rhel8-01]
changed: [rhel7-01]

TASK [satellite_tasks : setup] *****************************************************************************************************************************************************************************
ok: [rhel7-01] => (item=ansible_distribution*)
ok: [rhel7-02] => (item=ansible_distribution*)
ok: [rhel8-01] => (item=ansible_distribution*)
ok: [rhel9-01] => (item=ansible_distribution*)
ok: [rhel9-02] => (item=ansible_distribution*)
ok: [rhel7-02] => (item=ansible_hostname)
ok: [rhel7-01] => (item=ansible_hostname)
ok: [rhel8-01] => (item=ansible_hostname)
ok: [rhel9-02] => (item=ansible_hostname)
ok: [rhel9-01] => (item=ansible_hostname)
ok: [rhel8-02] => (item=ansible_distribution*)
ok: [rhel8-02] => (item=ansible_hostname)

TASK [satellite_tasks : Register to Satellite] *************************************************************************************************************************************************************
changed: [rhel9-02]
changed: [rhel9-01]
changed: [rhel7-01]
changed: [rhel7-02]
changed: [rhel8-01]
changed: [rhel8-02]

TASK [satellite_tasks : Unregister to Satellite] ***********************************************************************************************************************************************************
changed: [rhel9-02]
changed: [rhel9-01]
changed: [rhel7-02]
changed: [rhel7-01]
changed: [rhel8-01]
changed: [rhel8-02]

PLAY RECAP *************************************************************************************************************************************************************************************************
rhel7-01                   : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
rhel7-02                   : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
rhel8-01                   : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
rhel8-02                   : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
rhel9-01                   : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
rhel9-02                   : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

Output when I run the command with no tags

PLAY [all] *************************************************************************************************************************************************************************************************

TASK [satellite_tasks : setup] *****************************************************************************************************************************************************************************
ok: [rhel7-01] => (item=ansible_distribution*)
ok: [rhel7-02] => (item=ansible_distribution*)
ok: [rhel8-02] => (item=ansible_distribution*)
ok: [rhel9-01] => (item=ansible_distribution*)
ok: [rhel9-02] => (item=ansible_distribution*)
ok: [rhel8-01] => (item=ansible_distribution*)
ok: [rhel7-01] => (item=ansible_hostname)
ok: [rhel7-02] => (item=ansible_hostname)
ok: [rhel9-01] => (item=ansible_hostname)
ok: [rhel8-01] => (item=ansible_hostname)
ok: [rhel9-02] => (item=ansible_hostname)
ok: [rhel8-02] => (item=ansible_hostname)

TASK [satellite_tasks : setup] *****************************************************************************************************************************************************************************
ok: [rhel7-01] => (item=ansible_distribution*)
ok: [rhel7-02] => (item=ansible_distribution*)
ok: [rhel8-01] => (item=ansible_distribution*)
ok: [rhel9-02] => (item=ansible_distribution*)
ok: [rhel9-01] => (item=ansible_distribution*)
ok: [rhel7-02] => (item=ansible_hostname)
ok: [rhel7-01] => (item=ansible_hostname)
ok: [rhel9-02] => (item=ansible_hostname)
ok: [rhel9-01] => (item=ansible_hostname)
ok: [rhel8-02] => (item=ansible_distribution*)
ok: [rhel8-02] => (item=ansible_hostname)
ok: [rhel8-01] => (item=ansible_hostname)

PLAY RECAP *************************************************************************************************************************************************************************************************
rhel7-01                   : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
rhel7-02                   : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
rhel8-01                   : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
rhel8-02                   : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
rhel9-01                   : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
rhel9-02                   : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  


I've tried various things in the role like import_role, include_role with the tag placement and I still get the same result every single time. I know I'm missing something obvious but I've spent so much time messing with this I'm just not seeing it. Appreciate any help.


Solution

  • The tasks are running twice because you've included the role twice. Tags are a way to select, from the command line, a subset of tasks to run. You've already applied the tags to the individual tasks in the role, so in your playbook you should just include the role once without adding additional tags.

    - hosts: all
      gather_facts: false
      become: true
      roles:
        - role: satellite_tasks
    

    Also, the way you are calling setup is very inefficient. filter does not change how fact gathering is done, it just throws away some of the data that is gathered. You're gathering the full set of facts twice, then throwing most of it away. You should instead use gather_subset:

    - setup:
        gather_subset: min
      tags: always
    

    If you really want to throw away some of the gathered data (and are using a supported version of Ansible), you can pass a list to filter instead of calling setup twice:

    - setup:
        gather_subset: min
        filter:
          - ansible_distribution*
          - ansible_hostname
      tags: always