Ansible“;rc";当条件为时,条件检查失败

Ansible“;rc";当条件为时,条件检查失败,ansible,ansible-2.x,Ansible,Ansible 2.x,根据寄存器值执行条件检查时,我的playbook失败。=使用when条件 以下角色是ansible角色的一部分: 我的剧本 当我执行此操作时,会出现以下错误 错误: 甚至当:umask_grep[“rc”]!=0到时间:umask_grep.rc!=0但即使收到相同的消息 在此方面的任何帮助都将不胜感激 我的易懂版本 表2.9.10 调试信息 在检查模式下运行时,Ansible不会执行shell或命令任务。由于“检查umask”任务中的shell脚本没有运行,因此在umask_grep中注册的相

根据
寄存器
值执行条件检查时,我的playbook失败。=使用
when
条件

以下角色是ansible角色的一部分:

我的剧本 当我执行此操作时,会出现以下错误

错误: 甚至当:umask_grep[“rc”]!=0到
时间:umask_grep.rc!=0
但即使收到相同的消息

在此方面的任何帮助都将不胜感激

我的易懂版本 表2.9.10

调试信息
在检查模式下运行时,Ansible不会执行
shell
命令
任务。由于“检查umask”任务中的shell脚本没有运行,因此在
umask_grep
中注册的相应结果没有
rc
属性。这就是你错误的根源

最简单的解决方案可能是在
rc
属性不可用时设置默认值:

- name: CIS 5.4.4 Ensure default user umask is 027 or more restrictive (/etc/profile 2/2)
  shell: echo umask 027 >> /etc/profile
  when: umask_grep.rc|default(0) != 0
  become: yes
您还可以修改剧本,使
grep
任务在检查模式下运行:

- hosts: localhost
  gather_facts: false
  tasks:
    - name: CIS 5.4.4 Ensure default user umask is 027 or more restrictive (/etc/profile 1/2)
      check_mode: false
      shell: tail -1 /etc/profile | grep "umask 027"
      register: umask_grep
      changed_when: umask_grep.rc != 0
      failed_when: false

    - name: CIS 5.4.4 Ensure default user umask is 027 or more restrictive (/etc/profile 2/2)
      shell: echo umask 027 >> /etc/profile
      when: umask_grep is changed
      become: yes

    - name: CIS 5.4.4 Ensure default user umask is 027 or more restrictive (/etc/bashrc 1/2)
      check_mode: false
      shell: tail -1 /etc/bashrc | grep "umask 027"
      register: umask_grep
      changed_when: umask_grep.rc != 0
      failed_when: false

    - name: CIS 5.4.4 Ensure default user umask is 027 or more restrictive (/etc/bashrc 2/2)
      shell: echo umask 027 >> /etc/bashrc
      when: umask_grep is changed
      become: yes

在这个模型中,进行更改的任务不需要显式地检查
rc
属性,因为我们在
grep
任务中使用该属性来设置任务的“更改”状态。

我无法通过Ansible 2.9.10运行您问题中的playbook重现该错误。你能用完整的复制机更新这个问题吗?不仅仅是一个任务列表,还有一个剧本,当它运行时,会产生你要问的错误。@larsks,谢谢你的评论,不过这本身就是一个完整的剧本,它只是角色的一部分,所以,它是完整的。如果有帮助的话,我只是更新了调试信息。您在问题中没有提到您是在检查模式下运行的。@larsks,很抱歉,我只是在详细模式下运行了它并回忆了它。非常感谢@larsks。这看起来
umask_grep.rc | default(0)!=0
nice,当:umask_grep[“rc”]!=0。
somevar.key
somevar[“key”]
之间除了一些额外的键入之外没有区别。再次感谢
skipping: [xx.xx.xx.xx] => {
    "changed": false,
    "invocation": {
        "module_args": {
            "_raw_params": "tail -1 /etc/profile | grep \"umask 027\"",
            "_uses_shell": true,
            "argv": null,
            "chdir": null,
            "creates": null,
            "executable": null,
            "removes": null,
            "stdin": null,
            "stdin_add_newline": true,
            "strip_empty_ends": true,
            "warn": true
        }
    },
    "msg": "skipped, running in check mode"
}
- name: CIS 5.4.4 Ensure default user umask is 027 or more restrictive (/etc/profile 2/2)
  shell: echo umask 027 >> /etc/profile
  when: umask_grep.rc|default(0) != 0
  become: yes
- hosts: localhost
  gather_facts: false
  tasks:
    - name: CIS 5.4.4 Ensure default user umask is 027 or more restrictive (/etc/profile 1/2)
      check_mode: false
      shell: tail -1 /etc/profile | grep "umask 027"
      register: umask_grep
      changed_when: umask_grep.rc != 0
      failed_when: false

    - name: CIS 5.4.4 Ensure default user umask is 027 or more restrictive (/etc/profile 2/2)
      shell: echo umask 027 >> /etc/profile
      when: umask_grep is changed
      become: yes

    - name: CIS 5.4.4 Ensure default user umask is 027 or more restrictive (/etc/bashrc 1/2)
      check_mode: false
      shell: tail -1 /etc/bashrc | grep "umask 027"
      register: umask_grep
      changed_when: umask_grep.rc != 0
      failed_when: false

    - name: CIS 5.4.4 Ensure default user umask is 027 or more restrictive (/etc/bashrc 2/2)
      shell: echo umask 027 >> /etc/bashrc
      when: umask_grep is changed
      become: yes