简体   繁体   中英

How to gather facts after installing Python in one Ansible playbook?

I need to gather facts when executing my playbook. But the hosts have not installed Python yet.

If I gather facts first, the playbook will raise error because of lacking Python. If I install Python first, I have to set gather_facts to no .

How to gather facts after installing Python in one Ansible playbook?

Here is my playbook:

---
- hosts: all
  gather_facts: yes

  pre_tasks:
    - name: Install Python 2.x
      raw: test -e /usr/bin/python || (apt update && apt install -y python-simplejson)

  tasks:
    ...

Use setup module.

---
- hosts: all
  gather_facts: no

  pre_tasks:
    - name: Install Python 2.x
      raw: test -e /usr/bin/python || (apt update && apt install -y python-simplejson)

  tasks:
  - name: Get facts
    setup:

  - name: Other tasks.....
  ....

Documentation: https://docs.ansible.com/ansible/latest/collections/ansible/builtin/setup_module.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM