检查 Ansible 中的多个条件包括

Check Multiple conditions in Ansible include

我的 ansible 角色有这个 main.yml

more tasks/main.yml
---

- include: ssl_pull.yml
  when: ACTION == 'renewal'
- include: ssl_gen.yml
  when: ACTION == 'generate'
- include: ssl_push.yml
  when: ACTION == 'install'
- include: ssl_install.yml
  when: ACTION == 'install'

我想让用户能够通过更新和安装等多项操作,因此它包括

- include: ssl_pull.yml
- include: ssl_push.yml
- include: ssl_install.yml

我知道如何为 ACTION 变量传递单个值,如下所示

ansible-playbook -v -i  /web/hosts.txt site.yml -e "ACTION=renewal"

如何,我可以传递一个或多个值并包含基于传递的 value/s 的文件吗?

能否建议我需要对我的命令/yml 进行哪些更改?

Tags 是您要找的东西吗?

您将任务标记为:

- include: ssl_pull.yml
  tags: renewal

- include: ssl_push.yml
  tags: install

- include: ssl_install.yml
  tags: install

然后使用 -t 参数指定标签:

ansible-playbook -v -i  /web/hosts.txt site.yml -t "renewal,generate"

如果你因为某些原因不能使用tags,我们也可以用vars做类似的事情

将 var 指定为逗号分隔值:

ansible-playbook -v -i  /web/hosts.txt site.yml -e "ACTIONS=renewal,generate"

并更新条件以逗号分割 var:

- include: ssl_pull.yml
  when: "'renewal' in ACTIONS.split(',')"

Ansible tags就是为了做这种任务选择。


如果你真的不想使用标签,你可以在extra-vars中传递一个JSON对象:

ansible-playbook --inventory-file /web/hosts.txt site.yml \
                 --extra-vars '{"ACTIONS": ["renewal", "install"]}'

然后针对数组进行测试:

- include: ssl_pull.yml
  when: "'renewal' in ACTIONS"