How to make Ansible execute a shell script if a package is not installed

后端 未结 7 1053
执念已碎
执念已碎 2021-01-30 01:48

How can I make Ansible execute a shell script if a (rpm) package is not installed? Is it somehow possible to leverage the yum module?

7条回答
  •  日久生厌
    2021-01-30 02:12

    Since Ansible 2.5, you can use the package_facts module:

    - name: Gather package facts
      package_facts:
        manager: auto
    
    - name: Debug if package is present
      debug:
        msg: 'yes, mypackage is present'
      when: '"mypackage" in ansible_facts.packages'
    
    - name: Debug if package is absent
      debug:
        msg: 'no, mypackage is absent'
      when: '"mypackage" not in ansible_facts.packages'
    

    Note: you need the python bindings for apt/rpm installed on the target, e.g. python-apt for Debian.

提交回复
热议问题