Create a menu with options using var_prompt in Ansible

While creating a provisioning task, I found it difficult to create a “menu-like” structure in Ansible, where you can easily select from a number of options.

Various people online recommended janky hacks which involve wrappers in bash. I wanted an Ansible-native solution.

This might also fall in the category of “janky”, but I like it better.

- hosts: localhost
  connection: local
  gather_facts: no
  - name: "server_type"
    prompt: "nWhich type of server would you like to deploy?n1- Oraclen2- ColdFusionn3- IISn4- Tomcatn5- File Servern6- Base Servern"
    private: no
  - name: "environment"
    prompt: "nWhich environment will the server reside in?n1- Productionn2- Stagingn3- DMZn"
    private: no

Using a n, I was able to separate various options to their own separate lines.

The output now reads as such:

ansible@ansible:~$ ansible-playbook -i inventory provision.yml

Which type of server would you like to deploy?  
1- Oracle  
2- ColdFusion  
3- IIS  
4- Tomcat  
5- File Server  
6- Base Server  
: 1

Which environment will the server reside in?  
1- Production  
2- Staging  
3- DMZ  
: 1

Much more legible, and easy for users to enter input.

Leave a Reply

Your email address will not be published.