问题描述
当我使用--tags
来过滤将要执行的任务时,我习惯于使用ansible-playbook
。
我最近从Ansible 2.7切换到2.9(巨大的距离,是吗?)。
当我在使用--tags
时,ansible并没有专门收集事实,这让我感到惊讶。而且我看到GitHub中有多个类似的案例,例如this one或this one。从2.8版本开始似乎影响ansible,但显示为已解决。有人可以确认这种行为吗?似乎是从2.8开始的。
ANSIVERSION :
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]
ANSI CONFIG :
ansible-config dump --only-changed
DEFAULT_MODULE_PATH(env: ANSIBLE_LIBRARY) = [u'/opt/ansible/ansible/library']
要复制的步骤:
playbook test.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
角色:roles / test / tasks / main.yml
- debug:
msg: System {{ inventory_hostname }} has uuid {{ ansible_product_uuid }}
结果:
ansible-playbook test.yml --check
=没有错误。
ansible-playbook test.yml --check --tags "test"
=失败:1
“该任务包括带有未定义变量的选项。错误是:'ansible_product_uuid'未定义[...]”
我可以在输出中看到没有收集事实。
解决方法
好吧,当您在播放级别上放置标签时,这似乎是有目的的行为:
这是预期的行为。使用标签标记剧本会将这些标签应用于
gather_facts
步骤,并删除默认情况下应用的always
标签。如果目标是标记剧本,则可以添加带有标签的setup
任务以收集事实。
请注意,因此,这与角色的使用无关,因为只需执行以下操作即可复制:
- name: First test
hosts: all
tags:
- first
tasks:
- debug:
msg: System {{ inventory_hostname }} has uuid {{ ansible_product_uuid }}
tags: test
回顾失败的地方
$ 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
因此,您将必须删除播放级别的标签,或者根据提示使用setup
模块。
这可以在您的角色内部完成,因此您的角色不再依赖可能无法设置的变量。
赋予角色 roles / test / tasks / main.yml
- setup:
- debug:
msg: System {{ inventory_hostname }} has uuid {{ ansible_product_uuid }}
还有剧本:
- name: First test
hosts: all
tags:
- first
roles:
- role: test
tags:
- test
- name: Second test
hosts: all
tags:
- second
roles:
- role: test
tags:
- test
这是运行和总结:
$ 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
所有这些运行于:
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]