2

Facing one issue related to tags in roles. i have specified one tag(create) to run and other tag should skip, but both the tag are executing, can someone explain how to solve this issue.
roles_main.yml

---
- name: roles
  hosts: "{{ host }}"
#  hosts: localhost
  gather_facts: False
  become: yes
  tasks:
  - import_role:
      name: "{{ role_name }}"
    tags: "{{ tag_name }}"

linux/main.yml

---
- name: in file
  import_tasks: in.yml
  tags: create
  delegate_to: localhost

- name: out file
  import_tasks: out.yml
  tags: get
  delegate_to: localhost 

ansible-playbook roles_main.yml -e host=localhost -e ritm_ticket=test1 -e role_name=linux -e tag_name=create -v
satheesh kumar
  • 61
  • 1
  • 2
  • 9
  • 3
    You don't have a `--tags` or `--skip-tags` in your run command, though, is this normal? – β.εηοιτ.βε Jun 29 '20 at 18:12
  • Yes, instead of passing --tags i created a tags variable called tag_name in roles_main.yml – satheesh kumar Jul 01 '20 at 09:39
  • 1
    This is not how tags work. If you want to skip some task you have to: 1. tags the tasks accordingly, 2. pass `--tags "list of tags you want to run"` or `--skip-tags="list of tags you want to skip"` – β.εηοιτ.βε Jul 01 '20 at 10:46
  • Putting a tag on a role will **just** apply it to all tasks of that role: https://docs.ansible.com/ansible/latest/user_guide/playbooks_tags.html#tag-inheritance – β.εηοιτ.βε Jul 01 '20 at 10:47
  • Yes: Adding tags: to a play, or to statically imported tasks and roles, adds those tags to all of the contained tasks. BUT! As @β.εηοιτ.βε says, for use this tags when you run "ansible-playbook" you need to use: --tags or --skip-tags – Víctor Oriol Jul 02 '20 at 10:34

2 Answers2

2

You need add the argument "--tags" (or --skip-tags) to specify the tag you want to run ie:

ansible-playbook roles_main.yml -e host=localhost -e ritm_ticket=test1 -e role_name=linux -e tag_name=create -v --tags=create

For more information:

https://docs.ansible.com/ansible/latest/user_guide/playbooks_tags.html

Víctor Oriol
  • 492
  • 4
  • 15
  • 1
    Yes, already tried this, i am trying to avoid --tags and make tags as a extra variable in role_main.yml ie) tags: "{{ tag_name }}" – satheesh kumar Jul 01 '20 at 09:41
1

There are 3 special keywords for tags:

  • tagged: run only tagged tasks.
  • untagged: run only untagged tasks.
  • all: run all tasks.

By default, Ansible runs as if --tags all had been specified.

You don't pass any tag as argument so ansible runs with --tags all and execute all tasks and import both in.yml and out.yml.

if you want to use Tags feature of ansible you have you pass --tags or --skip-tags arguments. As your play depend on the value of a variable, an alternative solution can be using Conditionals statements.

You can change your scripts to:

roles_main.yml

---
- name: roles
  hosts: "{{ host }}"
#  hosts: localhost
  gather_facts: False
  become: yes
  tasks:
  - import_role:
      name: "{{ role_name }}"
    when: tag_name == "create"

linux/main.yml

---
- name: in file
  import_tasks: in.yml
  when: tag_name == "create"
  delegate_to: localhost

- name: out file
  import_tasks: out.yml
  when: tag_name == "get"
  delegate_to: localhost 

That should work.

Good luck!

Jhkcia
  • 305
  • 2
  • 8