Ansible 不收集标签上的事实

Pie*_*tin 5 tags ansible ansible-facts

我习惯使用--tags当我使用时ansible-playbook来过滤哪些任务将被执行。我最近从 Ansible 2.7 切换到 2.9(差距很大,嗯?)。

我很惊讶 ansible 在我使用时没有专门收集事实--tags。我在 GitHub 上看到了多个类似的案例,比如这个或这个。它似乎从 2.8 版本开始影响 ansible,但显示为已解决。有人可以证实这种行为吗?这似乎是从2.8开始发生的。

ANSIBLE 版本:

ansible --version

ansible 2.9.9.post0
  config file = None
  configured module search path = [u'/opt/ansible/ansible/library']
  ansible python module location = /opt/ansible/ansible/lib/ansible
  executable location = /opt/ansible/ansible/bin/ansible
  python version = 2.7.6 (default, Nov 13 2018, 12:45:42) [GCC 4.8.4]
Run Code Online (Sandbox Code Playgroud)

ANSIBLE 配置:

ansible-config dump --only-changed

DEFAULT_MODULE_PATH(env: ANSIBLE_LIBRARY) = [u'/opt/ansible/ansible/library']
Run Code Online (Sandbox Code Playgroud)

重现步骤:

剧本测试.yml:

- name: First test
  hosts: localhost
  connection: local
  gather_facts: yes
  roles:
    - { role: test, tags: test }
  tags: first

- name: Second test
  hosts: localhost
  connection: local
  gather_facts: yes
  roles:
    - { role: test, tags: test }
  tags: second
Run Code Online (Sandbox Code Playgroud)

角色:角色/测试/任务/main.yml

- debug:
    msg: System {{ inventory_hostname }} has uuid {{ ansible_product_uuid }}
Run Code Online (Sandbox Code Playgroud)

结果:

ansible-playbook test.yml --check
Run Code Online (Sandbox Code Playgroud)

= 没有错误。


ansible-playbook test.yml --check --tags "test"
Run Code Online (Sandbox Code Playgroud)

= 失败:1

“该任务包含一个带有未定义变量的选项。错误是:'ansible_product_uuid'未定义[...]”

我可以从输出中看到事实没有被收集。

β.ε*_*.βε 4

好吧,当你在游戏关卡上有标签时,这似乎是一种有目的的行为:

这是有意的行为。使用标签标记播放会将这些标签应用到步骤gather_facts并删除always默认应用的标签。如果目标是为该剧添加标签,您可以添加setup带有标签的任务以收集事实。

samdoran于 2019 年 6 月 11 日发表评论


请注意,这与角色的使用无关,因为它可以通过简单地执行以下操作来重现:

- name: First test
  hosts: all
  tags:
    - first

  tasks:
    - debug:
        msg: System {{ inventory_hostname }} has uuid {{ ansible_product_uuid }}
      tags: test
Run Code Online (Sandbox Code Playgroud)

这会产生失败的回顾:

$ ANSIBLE_STDOUT_CALLBACK=debug ansible-playbook play.yml --tags "test"

PLAY [First test] *************************************************************************************************

TASK [debug] ******************************************************************************************************
fatal: [localhost]: FAILED! => {}

MSG:

The task includes an option with an undefined variable. The error was: 'ansible_product_uuid' is undefined

The error appears to be in '/ansible/play.yml': line 7, column 7, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  tasks:
    - debug:
      ^ here


PLAY RECAP ********************************************************************************************************
localhost                  : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0  
Run Code Online (Sandbox Code Playgroud)

因此,您要么必须删除您的游戏级别的标签,要么setup按照提示使用该模块。

这可以在您的角色内部完成,因此您的角色不再依赖于可能无法设置的变量。

给定角色Roles/test/tasks/main.yml

$ ANSIBLE_STDOUT_CALLBACK=debug ansible-playbook play.yml --tags "test"

PLAY [First test] *************************************************************************************************

TASK [debug] ******************************************************************************************************
fatal: [localhost]: FAILED! => {}

MSG:

The task includes an option with an undefined variable. The error was: 'ansible_product_uuid' is undefined

The error appears to be in '/ansible/play.yml': line 7, column 7, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  tasks:
    - debug:
      ^ here


PLAY RECAP ********************************************************************************************************
localhost                  : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0  
Run Code Online (Sandbox Code Playgroud)

和剧本:

- setup:
- debug:
    msg: System {{ inventory_hostname }} has uuid {{ ansible_product_uuid }}
Run Code Online (Sandbox Code Playgroud)

以下是它的运行和回顾:

$ ansible-playbook play.yml --tags "test"

PLAY [First test] *************************************************************************************************

TASK [test : setup] ***********************************************************************************************
ok: [localhost]

TASK [test : debug] ***********************************************************************************************
ok: [localhost] => {
    "msg": "System localhost has uuid 3fc44bc9-0000-0000-b25d-bf9e26ce0762"
}

PLAY [Second test] ************************************************************************************************

TASK [test : setup] ***********************************************************************************************
ok: [localhost]

TASK [test : debug] ***********************************************************************************************
ok: [localhost] => {
    "msg": "System localhost has uuid 3fc44bc9-0000-0000-b25d-bf9e26ce0762"
}

PLAY RECAP ********************************************************************************************************
localhost                  : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
Run Code Online (Sandbox Code Playgroud)

所有这一切都运行在:

ansible 2.9.9
  config file = None
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.8/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.8.3 (default, May 15 2020, 01:53:50) [GCC 9.3.0]
Run Code Online (Sandbox Code Playgroud)