Avoid Unnecessary Prompting and Pausing in Ansible Playbooks

Ansible is a powerful automation tool designed to simplify complex IT tasks. While it’s excellent for handling various configurations and deployments, it’s important to create playbooks that can run unattended, particularly in Continuous Integration/Continuous Deployment (CI/CD) pipelines. This article discusses the Ansible playbook error “no-prompting,” which helps ensure that your playbooks are suitable for automated, unattended execution.

The Challenge: Prompts and Pauses

Sometimes, playbooks include user prompts or unnecessary pauses. While these may be useful for manual interventions in some situations, they can become obstacles when you want your playbooks to execute automatically.

For instance, consider a playbook that asks for user credentials via vars_prompt and includes tasks like ansible.builtin.pause to create wait times. In a CI/CD environment, these prompts and pauses can lead to stalled automation pipelines.

The Solution: no-prompting Rule

To prevent these issues, Ansible provides the no-prompting rule in Ansible-lint. This rule checks playbooks for the presence of vars_prompt or the ansible.builtin.pause module, which are prompts or pauses that can disrupt automation. By enabling this rule, you can identify and rectify any occurrences of these elements in your playbooks.

Enabling the Rule

To use the no-prompting rule, you need to enable it in your Ansible-lint configuration file. Here’s an example of how to do it:

enable_list:
  - no-prompting

By adding this rule to your enable list, Ansible-lint will check your playbooks for prompts and pauses, helping you ensure they are suitable for automated execution.

Problematic Code

Here’s an example of problematic code in an Ansible playbook:

---
- name: Example playbook
  hosts: all
  vars_prompt:
    - name: username
      prompt: What is your username?
      private: false
    - name: password
      prompt: What is your password?
  tasks:
    - name: Pause for 5 minutes
      ansible.builtin.pause:
        minutes: 5
    - name: Display message
      ansible.builtin.debug:
        msg: "{{ username }}, {{ password }}"

In this code, user prompts for username and password are included using vars_prompt. Additionally, there’s a pause task that halts playbook execution for 5 minutes.

Ansible Lint Output

WARNING  Listing 1 violation(s) that are fatal
no-prompting: Play uses vars_prompt
no-prompting.yml:5

Read documentation for instructions on how to ignore specific rule violations.

             Rule Violation Summary              
 count tag          profile rule associated tags 
     1 no-prompting         opt-in               

Failed: 1 failure(s), 0 warning(s) on 1 files. Last profile that met the validation criteria was 'production'. Rating: 5/5 star
Join 50+ hours of courses in our exclusive community

Correct Code

The correct code, without the prompting and pausing, would look like this:

---
- name: Example playbook
  hosts: all
  vars:
    username: username
    password: password
  tasks:
    - name: Display message
      ansible.builtin.debug:
        msg: "{{ username }}, {{ password }}"

In the correct code, user prompts and pauses have been removed, making the playbook suitable for unattended execution in automated workflows.

Conclusion

The “no-prompting” rule in Ansible-lint is a valuable tool to ensure your Ansible playbooks are automation-friendly. By eliminating user prompts and pauses, you can create playbooks that seamlessly integrate into CI/CD pipelines, improving efficiency and reliability in your automation processes.

Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my Udemy 300+ Lessons Video Course.

BUY the Complete Udemy 300+ Lessons Video Course

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Patreon Buy me a Pizza