# Conditionally input to a playbook based on vars\_prompt

**URL:** <https://forum.ansible.com/t/conditionally-input-to-a-playbook-based-on-vars-prompt/34082>\
**Category:** Ansible Project\
**Created:** [October 22, 2020, 6:29am UTC](https://forum.ansible.com/t/conditionally-input-to-a-playbook-based-on-vars-prompt/34082 "2020-10-22T06:29:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sobhan\_Anavarapu](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@Sobhan\_Anavarapu](https://forum.ansible.com/u/Sobhan_Anavarapu)\
**Post date:** [October 22, 2020, 6:29am UTC](https://forum.ansible.com/t/conditionally-input-to-a-playbook-based-on-vars-prompt/34082/1 "2020-10-22T06:29:01Z")

</div>

I have a question, with optional input of A or B in vars\_prompt can the command be executed in a playbook task.

example

- name: checking the switchshow  
value\_command:  
command\_set:
- command: switchdisable
- command: switchshow

Like with vars\_prompt if we select A 1st command need to execute if B second command need to execute

Can this be achieved with Ansible ?

---

<div class="post-metadata">

**Author:** ![Rahul\_Puli](https://avatars.discourse-cdn.com/v4/letter/r/82dd89/32.png) [@Rahul\_Puli](https://forum.ansible.com/u/Rahul_Puli)\
**Post date:** [October 22, 2020, 9:27am UTC](https://forum.ansible.com/t/conditionally-input-to-a-playbook-based-on-vars-prompt/34082/2 "2020-10-22T09:27:32Z")

</div>

Yes, this can be achievable. You can use when condition.

---

<div class="post-metadata">

**Author:** ![Sobhan\_Anavarapu](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@Sobhan\_Anavarapu](https://forum.ansible.com/u/Sobhan_Anavarapu)\
**Post date:** [October 22, 2020, 9:45am UTC](https://forum.ansible.com/t/conditionally-input-to-a-playbook-based-on-vars-prompt/34082/3 "2020-10-22T09:45:43Z")

</div>

can we have multiple when conditions in a single task?

vars\_prompt:

- name: input  
prompt: Please enter the option A,B,C  
private: no
- name: checking the switchshow  
value\_command:  
command\_set:
- command: ipaddr
- command: ifconfig
- command: freespace -m
- command: yum repolist

With the above options input A, B, C specific commands associated with that option need to be executed. All the commands are there in a single task.

- A - ipaddr
- B - ifconfig
- C - freespace -m , yum repolist

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [October 22, 2020, 10:11am UTC](https://forum.ansible.com/t/conditionally-input-to-a-playbook-based-on-vars-prompt/34082/4 "2020-10-22T10:11:25Z")

</div>

> can we have multiple when conditions in a single task?
> 
> vars\_prompt:  
> - name: input  
> &nbsp;&nbsp;&nbsp;prompt: Please enter the option A,B,C  
> &nbsp;&nbsp;&nbsp;private: no  
> - name: checking the switchshow  
> &nbsp;&nbsp;&nbsp;value\_command:  
> &nbsp;&nbsp;&nbsp;command\_set:  
> &nbsp;&nbsp;&nbsp;&nbsp;- command: ipaddr  
> &nbsp;&nbsp;&nbsp;&nbsp;- command: ifconfig  
> &nbsp;&nbsp;&nbsp;&nbsp;- command: freespace -m  
> &nbsp;&nbsp;&nbsp;&nbsp;- command: yum repolist

Instead of a list, put the commands into a dictionary. Test with  
debug first. For example

&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;command\_set:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;B: ifconfig  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C: freespace -m  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;D: yum repolist  
&nbsp;&nbsp;vars\_prompt:  
&nbsp;&nbsp;&nbsp;&nbsp;- name: input  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;prompt: Please enter the option {{ command\_set.keys()|list }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private: no  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: Command {{ command\_set[input] }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: input in command\_set.keys()|list

You'll be better off with "Data driven programming" in similar  
use-cases. See  
[https://stackoverflow.com/questions/1065584/what-is-data-driven-programming](https://stackoverflow.com/questions/1065584/what-is-data-driven-programming)

---

<div class="post-metadata">

**Author:** ![Sobhan\_Anavarapu](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@Sobhan\_Anavarapu](https://forum.ansible.com/u/Sobhan_Anavarapu)\
**Post date:** [October 22, 2020, 11:16am UTC](https://forum.ansible.com/t/conditionally-input-to-a-playbook-based-on-vars-prompt/34082/5 "2020-10-22T11:16:28Z")

</div>

Thank you @Vladimir for the solution. I will test this and share you the update. value\_command is the module, do we need to add the vars in the module ?

vars:  
command\_set:  
B: ifconfig  
C: freespace -m  
D: yum repolist

vars\_prompt:

- name: input  
prompt: Please enter the option {{ command\_set.keys()|list }}  
private: no

tasks:

- name: checking the switchshow  
value\_command:  
command\_set:

- debug:  
msg: Command {{ command\_set[input] }}  
when: input in command\_set.keys()|list

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [October 22, 2020, 11:58am UTC](https://forum.ansible.com/t/conditionally-input-to-a-playbook-based-on-vars-prompt/34082/6 "2020-10-22T11:58:33Z")

</div>

> ... value\_command is the module, do we need to add the vars in the  
> module ?
> 
> vars:  
> command\_set:  
> A: ipaddr  
> B: ifconfig  
> C: freespace -m  
> D: yum repolist

It's up to you where you put the variables. Yes, module is one of the  
options. Mind the indentation. For example

&nbsp;&nbsp;- command: "{{ command\_set[input] }}"  
&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command\_set:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;B: ifconfig  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C: freespace -m  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;D: yum repolist

But, the point here is to manage the control-flow by data. Using the  
hard-coded data you get rid of this advantage. See  
"Variable precedence: Where should I put a variable?"  
[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_variables.html#variable-precedence-where-should-i-put-a-variable](https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable)

---

<div class="post-metadata">

**Author:** ![Sobhan\_Anavarapu](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@Sobhan\_Anavarapu](https://forum.ansible.com/u/Sobhan_Anavarapu)\
**Post date:** [October 22, 2020, 4:53pm UTC](https://forum.ansible.com/t/conditionally-input-to-a-playbook-based-on-vars-prompt/34082/7 "2020-10-22T16:53:01Z")

</div>

Thank you @Vladimir provided solution worked for me. But do we have an option to pass both commands with a single option input like:  
D: freespace -m  
yum repolist
