如何在一个任务中组合两个命令?|可行

axr492tv  于 2022-10-06  发布在  Nginx
关注(0)|答案(1)|浏览(226)

所以,我的问题是,我想检查nginx是否安装在两个具有不同包管理器的不同操作系统上。

- name: Veryfying nginx installation # RedHat
   command: "rpm -q nginx"
   when: ansible_facts.pkg_mgr in ["yum","dnf","rpm"] #or (ansible_os_family == "RedHat")

 - name: Veryfying nginx installation # Debian
   command: "dpkg -l nginx"
   when: ansible_facts.pkg_mgr in ["dpkg", "apt"] #or (ansible_os_family == "Debian")

我可以将其合并到一项任务中吗?如果可能,如何将其合并?因为我需要注册输出结果,然后继续使用它。我想不出来。

4jb9z9bj

4jb9z9bj1#

另一种解决方案是使用package_facts模块,如下所示:

- hosts: localhost
  tasks:
    - package_facts:

    - debug:
        msg: "Nginx is installed!"
      when: "'nginx' in packages"

但您也可以为两个任务注册单独的变量,然后合并结果:

- hosts: localhost
  tasks:
    - name: Veryfying nginx installation # RedHat
      command: "rpm -q nginx"
      when: ansible_facts.pkg_mgr in ["yum","dnf","rpm"] #or (ansible_os_family == "RedHat")
      failed_when: false
      register: rpm_check

    - name: Veryfying nginx installation # Debian
      command: "dpkg -l nginx"
      when: ansible_facts.pkg_mgr in ["dpkg", "apt"] #or (ansible_os_family == "Debian")
      failed_when: false
      register: dpkg_check

    - set_fact:
        nginx_result: >-
          {{
          (rpm_check is not skipped and rpm_check.rc == 0) or
          (dpkg_check is not skipped and dpkg_check.rc == 0)
          }}

    - debug:
        msg: "nginx is installed"
      when: nginx_result

相关问题