Force ansible-playbook to collect ansible facts

Hello,

I ran my ansible playbook, pb.yml file, with --tags, like following:

ansible-playbook -i “192.168.100.1,” pb.yml --tags “networking”

ansible-playbook does NOT collect ansible facts. However, if I run it without --tages, the ansible facts are collected.

Why is that?

Here is my directory layout:

pb.yml
roles/
networking.yml

Here is the pb.yml:

Hello,

I ran my ansible playbook, pb.yml file, with --tags, like following:

ansible-playbook -i "192.168.100.1," pb.yml --tags "networking"

ansible-playbook does NOT collect ansible facts. However, if I run it
without --tages, the ansible facts are collected.

Why is that?

Because you have replaced the tags with the config tag.

Here is my directory layout:

pb.yml
roles/
  networking.yml

Here is the pb.yml:

---

- hosts: all
  gather_facts: yes

  roles:
    - roles: networking
  tags:
    - config

You should read about how tags work in the documentation
https://docs.ansible.com/ansible/latest/user_guide/playbooks_tags.html

Because you have replaced the tags with the config tag.

I want to configure only networking part inside config. That is, config is parent level tag, I only want to invoke networking tag of the parent tag. Can I do that? BTW, below isn’t working:

ansible-playbook -i test -l my_servers pb.yml --tags config,networking

  • Xinhuan

I want to configure only networking part inside config. That is, config is
parent level tag, I only want to invoke networking tag of the parent tag.
Can I do that?

When you set the tag config on the play the always tag that gather facts has is overwritten and will not be run without specifying the config tag.

An alternative is not to use gather_facts but instead use the setup module to gather facts.

BTW, below isn't working:

ansible-playbook -i test -l my_servers pb.yml --tags config,networking

What is not working?
If you mean gather facts you must be doing something else that your previous mail since it work when I test.

test2.yml

Hello Mr. Kai,

Sorry for not being clear. What I’m working on is probably a big playbook. So I used Ansible roles wisdom, for code re-usability. The main playbook will invoke each role as I develop, and I need to debug each role by calling the main playbook. That’s why I figure it to use tags, since each role is also tagged differently. Today I found that some pieces of task in roles are not tagged at all, so it skipped. As I added missing tags, everything worked as expected.

One thing I want to share with you and others is it is probably not a good idea to tag the main playbook, if you already tag the roles. If main playbook is tagged, when invoking with both main playbook’s tag and role’s tag, it causes unwanted roles to be invoked, and when invoking only role’s tag, gathering facts will be skipped. So it is better to not tag the main playbook.

There is no need to create setup module. Using gather_facts: yes in main playbook is fine.

Just find tags can be applied to roles this way: https://docs.ansible.com/ansible/latest/user_guide/playbooks_tags.html:

roles:
  - role: webserver                

    tags: [ web, foo ]

  • Xinhuan