【问题标题】:Ansible task is run regardless of conditionAnsible 任务无论条件如何都运行
【发布时间】:2020-11-17 23:26:50
【问题描述】:

如果我将字符串存储到 ansible 寄存器中,然后让其他任务仅在该寄存器包含某个值时运行,它们总是运行,而不管寄存器中的值如何。

为什么当条件失败时 ansible 运行任务?

这是一个简单的例子。它应该只调试一次(如test_foo.stdout == "Foo"),但它会打印两个调试语句:

- name: foo
  command: "echo \"Foo\""
  register: test_foo
  tags: [ 'foo-test' ]

- debug:
    var: test_foo
  when: test_foo.stdout == "Foo",
  tags: [ 'foo-test' ]

- debug:
    var: test_foo
  when: test_foo.stdout == "Bar",
  tags: [ 'foo-test' ]

请注意,当使用相同条件触发另一个命令调用时也会发生这种情况,而不仅仅是调试调用。它将始终执行这两个任务。

这是输出:

TASK [test : foo] ***********************************************************************************************************************************************************************************
changed: [localServer -> localhost] => {"changed": true, "cmd": ["echo", "Foo"], "delta": "0:00:01.002745", "end": "2020-11-17 11:22:25.733089", "rc": 0, "start": "2020-11-17 11:22:24.730344", "stderr": "", "stderr_lines": [], "stdout": "Foo", "stdout_lines": ["Foo"]}

TASK [test : debug] *********************************************************************************************************************************************************************************
ok: [localServer] => {
    "test_foo": {
        "changed": true,
        "cmd": [
            "echo",
            "Foo"
        ],
        "delta": "0:00:01.002745",
        "end": "2020-11-17 11:22:25.733089",
        "failed": false,
        "rc": 0,
        "start": "2020-11-17 11:22:24.730344",
        "stderr": "",
        "stderr_lines": [],
        "stdout": "Foo",
        "stdout_lines": [
            "Foo"
        ]
    }
}

TASK [test : debug] *********************************************************************************************************************************************************************************
ok: [localServer] => {
    "test_foo": {
        "changed": true,
        "cmd": [
            "echo",
            "Foo"
        ],
        "delta": "0:00:01.002745",
        "end": "2020-11-17 11:22:25.733089",
        "failed": false,
        "rc": 0,
        "start": "2020-11-17 11:22:24.730344",
        "stderr": "",
        "stderr_lines": [],
        "stdout": "Foo",
        "stdout_lines": [
            "Foo"
        ]
    }
}

以防万一这是特定于版本的奇怪东西,我使用的是 Ubuntu 20.04,这是我的版本:

$ ansible --version
ansible 2.9.6
  config file = /path/to/project/orchestration/ansible.cfg
  configured module search path = ['/home/myUser/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.8.5 (default, Jul 28 2020, 12:59:40) [GCC 9.3.0]

【问题讨论】:

    标签: ansible


    【解决方案1】:

    问题在于结尾的逗号。下面的任务按预期工作并给出“msg: false

        - debug:
            msg: '{{ "A" == "B" }}'
    

    结尾的逗号创建一个tuple

        - debug:
            msg: '{{ "A" == "B", }}'
        - debug:
            msg: "{{ (false,)|type_debug }}"
    

    给予

      msg: (False,)
    
      msg: tuple
    

    非空变量,包括元组,计算结果为 true

        - debug:
            msg: OK
          when: (A,)
    

    给予

      msg: OK
    

    CONDITIONAL_BARE_VARS

    【讨论】:

    • ?‍♂️ 谢谢!我从另一个角色以 JSON 格式复制了条件,逗号随之而来。我把头发扯掉了。
    【解决方案2】:

    我刚刚将您的代码复制到我的实验室 VM 中,我注意到您在“when”语句的末尾使用了一个逗号(我从未见过)。我已删除它们并获得预期的输出。

    当变量为 Foo 时:

    PLAY [localhost] ***************************************************************************************************************
    
    TASK [foo] *********************************************************************************************************************
    changed: [localhost]
    
    TASK [debug] *******************************************************************************************************************
    ok: [localhost] => {
        "test_foo": {
            "changed": true,
            "cmd": [
                "echo",
                "Foo"
            ],
            "delta": "0:00:00.006168",
            "end": "2020-11-17 12:50:11.863107",
            "failed": false,
            "rc": 0,
            "start": "2020-11-17 12:50:11.856939",
            "stderr": "",
            "stderr_lines": [],
            "stdout": "Foo",
            "stdout_lines": [
                "Foo"
            ]
        }
    }
    
    TASK [debug] *******************************************************************************************************************
    skipping: [localhost]
    
    PLAY RECAP *********************************************************************************************************************
    localhost                  : ok=2    changed=1    unreachable=0    failed=0
    
    

    当变量为 Bar 时:

    
    PLAY [localhost] ***************************************************************************************************************
    
    TASK [foo] *********************************************************************************************************************
    changed: [localhost]
    
    TASK [debug] *******************************************************************************************************************
    skipping: [localhost]
    
    TASK [debug] *******************************************************************************************************************
    ok: [localhost] => {
        "test_foo": {
            "changed": true,
            "cmd": [
                "echo",
                "Bar"
            ],
            "delta": "0:00:00.006077",
            "end": "2020-11-17 12:53:24.142058",
            "failed": false,
            "rc": 0,
            "start": "2020-11-17 12:53:24.135981",
            "stderr": "",
            "stderr_lines": [],
            "stdout": "Bar",
            "stdout_lines": [
                "Bar"
            ]
        }
    }
    
    PLAY RECAP *********************************************************************************************************************
    localhost                  : ok=2    changed=1    unreachable=0    failed=0
    
    

    我的 ansible 版本是:

    ansible 2.7.10
    python version = 2.7.5 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-24
      • 1970-01-01
      相关资源
      最近更新 更多