specific task is not executing by using tags in roles

when I defined tags in main.yml and roles, i couldn’t able to execute specific tags by using --tags or --skip-tags…

when I run the below with --tags=tag1 , I see whole playbook has executing
I have defined a task ( 127.0.0.1 ) in role, that’s also not working after role executed

The below are my tags,

tags: on role lis applying those tags to ALL tasks, not 'selecting
which tags to run'

so you need to use ALL of the tags in --tags to run those tasks.

Thank you Brian. yes after remove tag from role specific task works fine ! but localhost task executes fails …it worked till prompt syntax…

That seems unrelated (no tags?) also .. without an error its hard to
know what "doesn't work" means.

Ok here’s the playbook and output . and am using only --tags=tag1 but am getting all output as mentioned ( with_items ) … since tag1 executing should show only tag1 correct

$ validation.yml

Yes, this is correct.
There is a list of 5 tags on this task, using any single one with
"ansible-playbook --tags" will cause the entire task to run - which
includes a loop.
You can't use --tags on ansible-playbook to limit the items in a loop on a task.

Ok here's the playbook and output . and am using only --tags=tag1 but am getting all output as mentioned ( with_items ) .. since tag1 executing should show only tag1 correct

$ validation.yml
---
- hosts: hosts
  connection: local
  gather_facts: no
  roles:
    - role: validation

tasks:
  - name: Comment on ticket
    tags: ['tag1', 'tag2', 'tag3',' tag4', 'tag5']
    jira:
     uri: 'https://ccp.sys.comcast.net'
     username: "{{ username }}"
     password: "{{ password }}"
     issue: "{{ issue }}"
     operation: comment
     comment: "*{{ item.title }}* \n \n {{ lookup('file', '/tmp/{{ item.file }}') }}"
    with_items:
        - { title: tag1 created, file: tag1.txt }
        - { title: tag2 created, file: tag2.txt }
        - { title: tag3 created, file: tag3.txt }
        - { title: tag4 created, file: tag4.txt }
        - { title: tag5 created, file: tag5.txt }

Yes, this is correct.
There is a list of 5 tags on this task, using any single one with
"ansible-playbook --tags" will cause the entire task to run - which
includes a loop.
You can't use --tags on ansible-playbook to limit the items in a loop on a task.

You can try though the following condition to run the matching items from your list:

   when: item.title.split()[0] in ansible_run_tags

Regards
       Racke

Racke,

No, still its going through all tags … not working on specific tag in loop

Thanks,
Ramesh

Racke,

No, still its going through all tags .. not working on specific tag in loop

You are sure?

My task:

    - debug:
        msg: "{{ item.title.split()[0] }}"
      when: item.title.split()[0] in ansible_run_tags
      with_items:
        - { title: tag1 created, file: tag1.txt }
        - { title: tag2 created, file: tag2.txt }
        - { title: tag3 created, file: tag3.txt }
        - { title: tag4 created, file: tag4.txt }
        - { title: tag5 created, file: tag5.txt }
      tags: ['tag1', 'tag2', 'tag3','tag4', 'tag5']

Executed with --tags=tag4:

TASK [debug] *******************************************************************
skipping: [buster-test-box] => (item={'title': 'tag1 created', 'file': 'tag1.txt'})
skipping: [buster-test-box] => (item={'title': 'tag2 created', 'file': 'tag2.txt'})
skipping: [buster-test-box] => (item={'title': 'tag3 created', 'file': 'tag3.txt'})
ok: [buster-test-box] => (item={'title': 'tag4 created', 'file': 'tag4.txt'}) =>
  msg: tag4
skipping: [buster-test-box] => (item={'title': 'tag5 created', 'file': 'tag5.txt'})

Regards
         Racke

Not sure, what’s the problem. I see all skipping …

tasks:

  • name: run a task
    jira:
    uri: ‘https://xxxx
    username: “{{ username }}”
    password: “{{ password }}”
    issue: “{{ issue }}”
    operation: comment
    comment: “{{ item.title }} \n \n {{ lookup(‘file’, ‘/tmp/{{ item.file }}’) }}”
  • debug:
    msg: “{{ item.title.split()[0] }}”
    when: item.title.split()[0] in ansible_run_tags
    with_items:
  • { title: tag1 created, file: tag1.txt }
  • { title: tag2 created , file: tag2.txt }
  • { title: tag3 created, file: tag3.txt }
  • { title: tag4 created, file: tag4.txt }
  • { title: tag5 created, file: tag5txt }
    tags: [‘tag1’, ‘tag2’, ‘tag3’, ‘tag4’, ‘tag5’]

executed for tag5

PLAY [127.0.0.1] ***********************************************************************************************************************************************************

TASK [debug] ***************************************************************************************************************************************************************
skipping: [localhost] => (item={‘title’: ‘tag1 created’, ‘file’: ‘tag1.txt’})
skipping: [localhost] => (item={‘title’: ‘tag2 created’, ‘file’: ‘tag2.txt’})
skipping: [localhost] => (item={‘title’: ‘tag3 created’, ‘file’: ‘tag3.txt’})
skipping: [localhost] => (item={‘title’: ‘tag4 created’, ‘file’: ‘tag4.txt’})
skipping: [localhost] => (item={‘title’: ‘tag5 created’, ‘file’: ‘tag5.txt’})
skipping: [localhost]

PLAY RECAP *****************************************************************************************************************************************************************
host1 : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
localhost : ok=0 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0

Not sure, what's the problem. I see all skipping ..

Sometimes it's possible to spot a problem immediately. Unfortunately,
this is not the case. Loop No.10 and still nothing. The resolution of
this problem needs more effort.

When in trouble, minimise the code and make it both complete and
reproducible. See "Minimal working example"
https://en.wikipedia.org/wiki/Minimal_working_example

For example

  - hosts: localhost
    tasks:
      - debug:
          var: ansible_run_tags
        tags: always
      - debug:
          msg: "{{ item.title.split().0 }}"
        when: item.title.split().0 in ansible_run_tags
        loop:
          - title: tag1 created
          - title: tag2 created
          - title: tag3 created
        tags: [tag1, tag2, tag3]

  > ansible-playbook pb.yml -t tag2

  PLAY [localhost] ********************************************

  TASK [debug] ************************************************
  ok: [localhost] =>
    ansible_run_tags:
    - tag2

  TASK [debug] ************************************************
  skipping: [localhost] => (item={'title': 'tag1 created'})
  ok: [localhost] => (item={'title': 'tag2 created'}) =>
    msg: tag2
  skipping: [localhost] => (item={'title': 'tag3 created'})

Proceed step by step adding complexity until you find what's causing
the problem. Then post minimal, complete, and reproducible details if
you can't fix it.

This is not clear.
What is the exact command you invoke here?