检查Ansible中的多个条件

时间:2017-07-18 08:49:22

标签: ansible ansible-role

我的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'

我希望让用户能够传递多个ACTION,例如续订&安装所以它包括

- 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"

如何,我可以传递一个或多个值并根据传递的值/包含文件?

请告诉我需要对命令/ yml做出哪些更改?

2 个答案:

答案 0 :(得分:1)

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"

如果由于某些原因无法使用标签,我们也可以使用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(',')"

答案 1 :(得分:1)

Ansible tags旨在执行此类任务选择。

如果您确实想要不使用标记,则可以在extra-vars中传递JSON对象:

if (Foo?.Any() == true)

然后针对数组进行测试:

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