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?
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With