ansible playbook run with play tag gathers facts even that gathering facts set to 'no'

Viewed 182

Below ansible-playbook is run using ansible-playbook playbook.yml --tags=rancher

- name: instal docker 
  hosts: rancher-server
  become: yes
  gather_facts: yes

  roles:
    - role: some_galaxy_role

- name: install rancher 
  hosts: rancher-server
  become: yes
  gather_facts: no
  tasks:
    - name: install rancher
      debug:
  tags:
    - rancher

Only install rancher play is selected by rancher tag and runs as expected. However fact gathering of the first play install docker still runs and takes time. Why and is there a way to avoid it?

Below is the output of the playbook run:

PLAY [install docker] *********************************************************************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************************************************************
ok: [rancher-server1]
ok: [rancher-server2]


PLAY [install rancher] 
1 Answers

You can put a tag on the play level so the whole Instal Docker play is skipped.

Given:

- name: Install Docker
  hosts: localhost
  gather_facts: yes
  tags:
    - docker

  tasks:
    - debug:

- name: Install rancher
  hosts: localhost
  gather_facts: yes
  tags:
    - rancher

  tasks:
    - debug:

When run with --tags rancher, this yields:

PLAY [Install Docker] *********************************************************************************************

PLAY [Install rancher] ********************************************************************************************

TASK [Gathering Facts] ********************************************************************************************
ok: [localhost]

TASK [debug] ******************************************************************************************************
ok: [localhost] => 
  msg: Hello world!

PLAY RECAP ********************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

On the other hand, please mind that you are not forced to gather all the facts, you can also gather subsets, to speed up plays.

For example, you can use a minimal subset of the facts only:

- name: Install Docker
  hosts: localhost
  gather_subset:
    - min

  tasks:
    - debug:

Of course, it all just depends what is needed in the some_galaxy_role that requires you to gather facts.

Related