specific task is not executing by using tags in roles

61 views
Skip to first unread message

Ramesh AR

unread,
Jan 27, 2021, 7:19:02 AM1/27/21
to Ansible Project
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,

++++++++++++++++++++++++++++++++++++++++++++
playbook: validation.yml

  play #1 (hosts): hosts      TAGS: []
      TASK TAGS: [tag1, tag2, tag3, tag4, tag5 ]

  play #2 (127.0.0.1): 127.0.0.1        TAGS: []
      TASK TAGS: []
+++++++++++++++++++++++++++++++++++++++++++++
$/etc/ansible/roles/validation/main.yml
---
# tasks file for validation

- include_tasks: task 1.yml
  tags: tag1

- include_tasks: task 2.yml
  tags: tag 2

- include_tasks: task 3.yml
  tags: tag 3

- include_tasks: task 4.yml
  tags: tag 4

- include_tasks: task 5.yml
  tags: tag 5
+++++++++++++++++++++++++++++++++++++++++++++ 
This is playbook defined roles with tasks  included 
---
- hosts: hosts
  connection: local
  gather_facts: no
  roles:
    - { role: validation, tags: [ 'tag1, 'tag2', 'tag3', 'tag4', 'tag5' ] }
 
- hosts: 127.0.0.1
  gather_facts: no
  vars_files:
     - ex.yml
  tasks:
  - name: here some tasks executes 
   +++++++++++++++++++++++++++++++++++++++++++++++++++

THanks,
Ramesh

Brian Coca

unread,
Jan 27, 2021, 10:51:09 AM1/27/21
to Ansible Project
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.


--
----------
Brian Coca

Ramesh AR

unread,
Jan 27, 2021, 12:05:21 PM1/27/21
to Ansible Project
Thank you Brian. yes after remove tag from role  specific task works fine ! but localhost task executes fails ..it worked till prompt syntax.. 


---
- hosts: hosts
  connection: local
  gather_facts: no
  roles:
    -  role: validation

- hosts: 127.0.0.1
  gather_facts: no
  vars_files:
     - var.yml
  vars_prompt:
     - name: "issue"
       prompt: "What is your VOPSN handoff Ticket"
       private: no
  tasks: >>>>>>>>>>>>>>>>>>>> from here not worked. 
  - name: Comment on handoff ticket
    jira:
     uri: 'https://xyz.net'
     username: "{{ username }}"
     password: "{{ password }}"
     issue: "{{ issue }}"
     operation: comment
     comment: "*{{ item.title }}* \n \n {{ lookup('file', '/tmp/{{ item.file }}') }}"
    with_items:
        - { title: accounts created, file: user.txt }
        - { title: validation, file: ex.txt }
    

Brian Coca

unread,
Jan 27, 2021, 12:10:26 PM1/27/21
to Ansible Project
That seems unrelated (no tags?) also .. without an error its hard to
know what "doesn't work" means.


--
----------
Brian Coca

Ramesh AR

unread,
Jan 27, 2021, 12:54:45 PM1/27/21
to Ansible Project
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:
     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 } 
     
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

$ sudo ansible-playbook -i inventory.txt validation.yml --tags=tag1 --ask-vault-pass
Vault password:

PLAY [hosts] **********************************************************************************************************************************************************

TASK [validation : include_tasks] ******************************************************************************************************************************************
included: /etc/ansible/roles/validation/tasks/validation_tag1.yml for host1
What is your Ticket: vopsn-XXX

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

Here i want to get only tag1 not all tags 
TASK [Comment on ticket] *******************************************************************************************************************************************
changed: [127.0.0.1] => (item={'title': 'tag1 validation', 'file': 'tag1.txt'})
changed: [127.0.0.1] => (item={'title': 'tag2 validation', 'file': 'tag2.txt'})
changed: [127.0.0.1] => (item={'title': 'tag3 validation', 'file': 'tag3.txt'})
changed: [127.0.0.1] => (item={'title': 'tag4 validation', 'file': 'tag4.txt'})
changed: [127.0.0.1] => (item={'title': 'tag5 validation', 'file': 'tag5.txt'})

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

thanks,

Dick Visser

unread,
Jan 27, 2021, 1:48:39 PM1/27/21
to ansible...@googlegroups.com
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.










--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Stefan Hornburg (Racke)

unread,
Jan 27, 2021, 2:30:02 PM1/27/21
to ansible...@googlegroups.com
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

>
>
>
>
>
>
>
>
> --
> Dick Visser
> Trust & Identity Service Operations Manager
> GÉANT
>


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

OpenPGP_signature

Ramesh AR

unread,
Jan 27, 2021, 2:49:46 PM1/27/21
to Ansible Project
Racke, 

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


Thanks,
Ramesh 

Stefan Hornburg (Racke)

unread,
Jan 27, 2021, 4:00:35 PM1/27/21
to ansible...@googlegroups.com
On 1/27/21 8:49 PM, Ramesh AR wrote:
> 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

>
> Thanks,
> Ramesh 
> On Wednesday, 27 January 2021 at 14:30:02 UTC-5 brae...@gmail.com wrote:
>
> On 1/27/21 7:48 PM, Dick Visser wrote:
> > On Wed, 27 Jan 2021 at 18:54, Ramesh AR <rames...@gmail.com> wrote:
> >>
> >> 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 <https://ccp.sys.comcast.net>'
> --
> 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/bc9b90a7-8b06-42b9-9bef-061b4443a412n%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/bc9b90a7-8b06-42b9-9bef-061b4443a412n%40googlegroups.com?utm_medium=email&utm_source=footer>.
OpenPGP_signature

Ramesh AR

unread,
Jan 27, 2021, 5:45:29 PM1/27/21
to Ansible Project
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

Vladimir Botka

unread,
Jan 27, 2021, 9:12:59 PM1/27/21
to Ramesh AR, ansible...@googlegroups.com
On Wed, 27 Jan 2021 14:45:29 -0800 (PST)
Ramesh AR <rames...@gmail.com> wrote:

> 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]


shell> 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.


--
Vladimir Botka

Dick Visser

unread,
Jan 27, 2021, 11:52:44 PM1/27/21
to ansible...@googlegroups.com
On Wed, 27 Jan 2021 at 23:45, Ramesh AR <rames...@gmail.com> wrote:
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 


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


--
Sent from a mobile device - please excuse the brevity, spelling and punctuation.
Reply all
Reply to author
Forward
0 new messages