我写了一部剧本,其行为与提供给剧本的标签不同。我已确保用户在运行剧本时至少为其提供标签。但是,我需要确保用户仅向剧本提供一个标签。有可能吗?我已经在互联网上进行搜索,但结果为负面。
-->cat foo.yml
- hosts: localhost
gather_facts: yes
tasks:
- name: Check user inputs
fail: msg="ERROR:User must use ONE TAG"
- name: print message A
debug:
msg: "This is message A"
tags:
- printA
- name: print message B
debug:
msg: "This is message B"
tags:
- printB
工作:提供一个标签后,就可以工作。
ansible-playbook -i localhost foo.yml --tags="printA"
ansible-playbook -i localhost foo.yml --tags="printB"
工作:如果未提供标签,则失败并退出
ansible-playbook -i localhost foo.yml
fatal: [localhost]: FAILED! => {"changed": false, "msg": "ERROR:User must use ONE TAG"}
不确定如何执行:提供了一个以上的标签。失败并退出
ansible-playbook -i localhost foo.yml --tags="printB, printA"
更新:
这是@ ilias-sp答案中更新的PB:
---
- hosts: localhost
gather_facts: yes
tasks:
tasks:
- name: Check how many tags were provided
fail:
msg: "0 or more than 1 tags were provided. Exiting.."
failed_when: hostvars['localhost']['ansible_run_tags']|length > 1 or
(hostvars['localhost']['ansible_run_tags']|length == 1 and hostvars['localhost']['ansible_run_tags'][0] == 'all')
tags:
- always
- name: print message A
debug:
msg: "This is message A"
tags:
- printA
- name: print message B
debug:
msg: "This is message B"
tags:
- printB
ansible-playbook foo.yml --tags="printA, printB" # failing
ansible-playbook foo.yml --tags="printA" #failing
ansible-playbook foo.yml #failing
TASK [Check how many tags were provided] *************************************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The conditional check 'hostvars['localhost']['ansible_run_tags']|length > 1 or (hostvars['localhost']['ansible_run_tags']|length == 1 and hostvars['localhost']['ansible_run_tags'][0] == 'all')' failed. The error was: error while evaluating conditional (hostvars['localhost']['ansible_run_tags']|length > 1 or (hostvars['localhost']['ansible_run_tags']|length == 1 and hostvars['localhost']['ansible_run_tags'][0] == 'all')): 'dict object' has no attribute 'ansible_run_tags'"}
答案 0 :(得分:1)
您可以使用此fail
模块任务来检查PB中的标签,当您提供0个或多个1个标签时,它将使PB失败:
- name: Check how many tags were provided
fail:
msg: "0 or more than 1 tags were provided. Exiting.."
failed_when: hostvars['localhost']['ansible_run_tags']|length > 1 or
(hostvars['localhost']['ansible_run_tags']|length == 1 and hostvars['localhost']['ansible_run_tags'][0] == 'all')
tags:
- always
我测试过:
ansible_run_tags
有1个元素:all
),它按预期工作。希望对您有帮助
示例PB和执行运行:
---
- hosts: localhost
gather_facts: false
vars:
tasks:
- name: Check how many tags were provided
fail:
msg: "0 or more than 1 tags were provided. Exiting.."
failed_when: hostvars['localhost']['ansible_run_tags']|length > 1 or
(hostvars['localhost']['ansible_run_tags']|length == 1 and hostvars['localhost']['ansible_run_tags'][0] == 'all')
tags:
- always
- name: print results
debug:
var: hostvars['localhost']['ansible_run_tags']
tags:
- always
3次运行:
[http_offline@greenhat-29 tests]$ ansible-playbook test.yml
PLAY [localhost] *******************************************************************************************************************************************************************************************************
TASK [Check how many tags were provided] *******************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "failed_when_result": true, "msg": "0 or more than 1 tags were provided. Exiting.."}
PLAY RECAP *************************************************************************************************************************************************************************************************************
localhost : ok=0 changed=0 unreachable=0 failed=1
[http_offline@greenhat-29 tests]$ ansible-playbook test.yml --tags="printA"
PLAY [localhost] *******************************************************************************************************************************************************************************************************
TASK [Check how many tags were provided] *******************************************************************************************************************************************************************************
ok: [localhost]
TASK [print results] ***************************************************************************************************************************************************************************************************
ok: [localhost] => {
"hostvars['localhost']['ansible_run_tags']": [
"printA"
]
}
PLAY RECAP *************************************************************************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0
[http_offline@greenhat-29 tests]$ ansible-playbook test.yml --tags="printA, printB"
PLAY [localhost] *******************************************************************************************************************************************************************************************************
TASK [Check how many tags were provided] *******************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "failed_when_result": true, "msg": "0 or more than 1 tags were provided. Exiting.."}
PLAY RECAP *************************************************************************************************************************************************************************************************************
localhost : ok=0 changed=0 unreachable=0 failed=1
[http_offline@greenhat-29 tests]$
此解决方案将与Ansible 2.7.X一起使用。像2.4.X这样的旧版本将需要另一种方法-如果有的话