Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible: Output a variable in vars_prompt

Tags:

ansible

Is it possible to include the value of an existing variable in the message output by vars_prompt?

I am looking to do something like this:

- hosts: production
vars_prompt:
    confirm: "This playbook is configured to act on all $hosts hosts, please confirm"

...where the question printed by vars_prompt should be:

"This playbook is configured to act on all **production** hosts, please confirm"

like image 261
Claus Conrad Avatar asked Jan 22 '14 12:01

Claus Conrad


2 Answers

This is not exactly answering the question, as what I tried to achieve apparently is not possible using the current stable version of Ansible, but I would like to describe a workaround that helped my solve my use case:

First of all, the reason I wanted to output the hosts from the playbook inside a vars_prompt message was to make sure that the playbook would never be run on hosts it was not intended for. The playbook sets up backup for a VM, and while its actions are idempotent, only some of my VMs require backups in the first place. So I wrote the playbook to automate installation of a backup client on a VM and to configure the new client on a central backup server.

What I did to make sure that I only ever run this playbook for the intended host was to make the hosts parameter in the playbook itself a variable, like this:

- hosts: '{{ host }}'

Now the playbook will fail immediately, if no host is specified on the command line. The way to specify such a variable on the command line is this:

ansible-playbook --extra-vars="host=HOSTNAME" playbook.yml

like image 162
Claus Conrad Avatar answered Oct 04 '22 14:10

Claus Conrad


This is not possible, at least as recently as ansible version 1.4.4. The vars_prompt displays literally exactly what is within the quotes, so any $, {{ }}, etc. is displayed exactly as entered.

It might be worth submitting a feature request for this by posting at https://github.com/ansible/ansible/issues

like image 45
Bruce P Avatar answered Oct 04 '22 15:10

Bruce P