Ansible: Information pause before run

February 26, 2017

Having reusable Ansible playbook is great. It enables well tested setups, code reuse and less boilerplate. The flip side of it is when we start using the same playbook across environments, (cloud) accounts and clusters. This leaves the possibility of running playbooks on targets other than what we originally intend for.

A quick solution is to print out the details of the run before the run. One such information display could look like:

# File: roles/prompt/tasks/main.yaml
- set_fact:
    msg: |
      Stack details:
      <<AWS Account: {{ aws_account.identifier }}>>
      <<Env: {{ current_environment }}>>
      <<Prefix: {{ code_name }}>>

- name: "{{ msg }}"
  pause:
    seconds: 5

The above code can be its own task in its own role that would then run before any other roles in the playbook.

The output could look like:

 ______________________________________________________________
/ TASK [prompt : Stack details: <<AWS Account: Dev_us-east-1>> \
\ <<Env: staging>> <<Prefix: my-prefix>>]                      /
 --------------------------------------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

Pausing for 5 seconds
(ctrl+C then 'C' = continue early, ctrl+C then 'A' = abort)

The user then has the option to stop the run, or do nothing to let the run continue on its own. This solution also works in case you are running your Ansible playbooks in an automated manner e.g. inside Jenkins.