根据另一个游戏的输出调用另一个 'play' - Ansible

Invoke another 'play' based on output of another play - Ansible

我正在尝试使用 Ansible 检查 SELinux 是否已启用(设置为强制执行),如果未启用,请启用它。只有在 SELinux 被禁用时才必须调用 play 以启用 SELinux

playbook 看起来像这样:

- hosts: all

  # root should execute this.
  remote_user: root
  become: yes

  tasks:

    # Check if SELinux is enabled.
    - name: check if selinux is enabled
      tags: selinuxCheck
      register: selinuxCheckOut
      command: getenforce
    - debug: var=selinuxCheckOut.stdout_lines

    - name: enable selinux if not enabled already
      tags: enableSELinux
      selinux: policy=targeted state=enforcing
      when: selinuxCheckOut.stdout_lines == "Enforcing"
    - debug: var=enableSELinuxOut.stdout_lines

当我 运行 执行此操作时,任务 enableSELinux 失败,原因是 "Conditional check failed"。输出为:

TASK [debug] *******************************************************************
task path: /root/ansible/playbooks/selinuxConfig.yml:24
ok: [localhost] => {
    "selinuxCheckOut.stdout_lines": [
        "Enforcing"
    ]
}

TASK [enable selinux if not enabled already] ***********************************
task path: /root/ansible/playbooks/selinuxConfig.yml:26
skipping: [localhost] => {"changed": false, "skip_reason": "Conditional check failed", "skipped": true}

我的问题:
1. 这是根据另一个游戏的输出来执行一个游戏的正确方法吗?
2. 我如何让它工作?

你的剧本是正确的。但是 stdout_lines 是一个列表。您必须比较该列表中的第一个元素。试试这个:

when: selinuxCheckOut.stdout_lines[0] == "Enforcing"