0

My ansible role has this 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'

I want the to give the user the ability to pass multiple ACTION like renewal & install so it includes

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

I know how to pass a single value for ACTION variable as below

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

How, can i pass either one or multiple values and include files based on the value/s passed ?

Can you please suggest what changes i need to make to my command / yml ?

shifahim
  • 127
  • 1
  • 7
  • 19

2 Answers2

1

Are Tags what you are looking for?

You tag the tasks like:

- include: ssl_pull.yml
  tags: renewal

- include: ssl_push.yml
  tags: install

- include: ssl_install.yml
  tags: install

Then specify the tags using the -t argument:

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

If you can't use tags for some reasons, we can also do something similar with vars.

Specify the var as comma separated values:

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

And update the condition to split the var by comma:

- include: ssl_pull.yml
  when: "'renewal' in ACTIONS.split(',')"
Chris Lam
  • 3,526
  • 13
  • 10
1

Ansible tags are designed to do this kind of task selection.


If you really want to not use tags, you can pass a JSON object in extra-vars:

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

Then test against an array:

- include: ssl_pull.yml
  when: "'renewal' in ACTIONS"
zigarn
  • 10,892
  • 2
  • 31
  • 45