Using vars_prompt to create menu with options

Normally I prefer to reduce the probability that a user does mistakes. So sometimes is better if you ask through a menu what do you want to do when running a playbook. For example, if you’re using the same playbook to perform deployments of different applications is a good idea to create a menu with options. So you can reuse the same playbook for different actions. If you have following menu created by using vars_prompt:

 - name: "application"
   prompt: "Which application do you want to deploy?\n1- Web\n2- Wiki\n"
   private: no

You will have following prompt when running the playbook:

Which application do you want to deploy?
1- Web
2- Wiki

Then inside your playbook you can work with this condition.

For example you can define variables using set_fact:

 - set_fact: app=web
 when: ( application == "1" )
 - set_fact: app=wiki
 when: ( application == "2" )

Or perform actions taking into account which option you have chosen:

 - name: Build the war for website
   shell: /opt/builds/
   when: ( application == "1" ) 

- name: Build the war for wiki
   shell: /opt/builds/
   when: ( application == "2" )

In that way you can be sure that user can do only one mistake: press the wrong number 🙂


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s