Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible vars_prompt for roles

Tags:

ansible

I have a set of Ansible playbooks and the main yml file is like this

- hosts: all 
  roles:
    - common
    - install_nginx

I want to add the confirm message when I trigger the playbook. I tried this and did not work

- hosts: all
  vars_prompt:
    - name: CONFIRM
      prompt: Just to confirm you will install stuff
  tasks:
    - fail: no deployment this time
      when: CONFIRM != 'yes'
  roles:
    - common
    - install_nginx

How can I use vars_prompt in this case without modify every role?

like image 927
neo0 Avatar asked Dec 29 '15 04:12

neo0


People also ask

How do you define roles in Ansible?

Roles provide a framework for fully independent, or interdependent collections of variables, tasks, files, templates, and modules. In Ansible, the role is the primary mechanism for breaking a playbook into multiple files. This simplifies writing complex playbooks, and it makes them easier to reuse.

Can Ansible prompt for input?

If you want your playbook to prompt the user for certain input, add a 'vars_prompt' section. Prompting the user for variables lets you avoid recording sensitive data like passwords. In addition to security, prompts support flexibility.

What is the difference between Ansible playbook and roles?

Role is a set of tasks and additional files to configure host to serve for a certain role. Playbook is a mapping between hosts and roles.


1 Answers

If you look at the output from running your playbook with the vars_prompt you'll see that the fail task runs after the other roles. This is also mentioned in the Ansible docs for playbooks and roles:

If the play still has a ‘tasks’ section, those tasks are executed after roles are applied.

As the above docs also mention if you want to force a task to run before any roles then you can use pre_tasks.

So to have your confirmation style prompt you could simply do this:

- hosts: all
  vars_prompt:
    - name: CONFIRM
      prompt: Just to confirm you will install stuff
  pre_tasks:
    - fail: no deployment this time
      when: CONFIRM != 'yes'
  roles:
    - common
    - install_nginx
like image 171
ydaetskcoR Avatar answered Oct 17 '22 18:10

ydaetskcoR