Conditionally input to a playbook based on vars_prompt

46 views
Skip to first unread message

Sobhan Anavarapu

unread,
Oct 22, 2020, 2:29:01 AM10/22/20
to Ansible Project
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 ?

Rahul Puli

unread,
Oct 22, 2020, 5:26:09 AM10/22/20
to ansible...@googlegroups.com
Yes, this can be achievable. You can use when condition.


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/b134e188-0ad0-488e-b8a3-fe66a42350a6n%40googlegroups.com.

Sobhan Anavarapu

unread,
Oct 22, 2020, 5:45:43 AM10/22/20
to Ansible Project
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

Vladimir Botka

unread,
Oct 22, 2020, 6:11:50 AM10/22/20
to Sobhan Anavarapu, ansible...@googlegroups.com
On Thu, 22 Oct 2020 02:45:43 -0700 (PDT)
Sobhan Anavarapu <sobhan.a...@gmail.com> wrote:

> 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

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

vars:
command_set:
A: ipaddr
B: ifconfig
C: freespace -m
D: yum repolist
vars_prompt:
- name: input
prompt: Please enter the option {{ command_set.keys()|list }}
private: no
tasks:
- debug:
msg: Command {{ command_set[input] }}
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

--
Vladimir Botka

Sobhan Anavarapu

unread,
Oct 22, 2020, 7:16:28 AM10/22/20
to Ansible Project
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:
A: ipaddr
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  

Vladimir Botka

unread,
Oct 22, 2020, 7:59:06 AM10/22/20
to Sobhan Anavarapu, ansible...@googlegroups.com
On Thu, 22 Oct 2020 04:16:28 -0700 (PDT)
Sobhan Anavarapu <sobhan.a...@gmail.com> wrote:

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

- command: "{{ command_set[input] }}"
vars:
command_set:
A: ipaddr
B: ifconfig
C: freespace -m
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


--
Vladimir Botka

Sobhan Anavarapu

unread,
Oct 22, 2020, 12:53:02 PM10/22/20
to Ansible Project
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

Reply all
Reply to author
Forward
0 new messages