首页 > 解决方案 > 在一个 Ansible 剧本中安装 Python 后如何收集事实?

问题描述

在执行我的剧本时,我需要收集事实。但是主机还没有安装 Python。

如果我先收集事实,剧本会因为缺少 Python 而引发错误。如果我先安装 Python,我必须设置gather_factsno.

在一个 Ansible 剧本中安装 Python 后如何收集事实?

这是我的剧本:

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

标签: pythonansible

解决方案


使用setup模块。

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

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


推荐阅读