Introduction

As an automation expert, you may have encountered situations where you need to interact with command-line interfaces that require user input. Ansible provides a solution to this challenge with the “expect” module. The “expect” module is a powerful tool that allows you to automate the interaction with command-line interfaces using Ansible.

The “expect” module works by sending predefined responses to a command-line interface based on expected prompts. It can also handle complex scenarios with multiple prompts and dynamic responses. This makes it an ideal solution for automating tasks that involve interactive command-line interfaces such as routers, switches, and firewalls.

To use the expect module, you need to have the pexpect Python module installed on your Ansible control node. The pexpect module is a Python module that provides an interface to the expect command-line utility. You can install it using the following command:

pip install pexpect

Once you have the “pexpect” module installed, you can use the “expect” module in your Ansible playbook. Here’s an example:

- name: Configure router
  hosts: router
  gather_facts: no
  vars:
    router_username: admin
    router_password: password
    router_prompt: "Router#"
  tasks:
    - name: Log in to router
      expect:
        command: ssh {{ router_username }}@{{ inventory_hostname }}
        responses:
          "Password:": "{{ router_password }}"
          "{{ router_prompt }}": ""

In this example, the expect module is used to log in to a router using SSH and configure it. The “responses” section defines the expected prompts and the corresponding responses. When the ssh command is executed, the expect module will send the “router_password” variable when the “Password:” prompt is detected. When the “Router#” prompt is detected, the expect module will send an empty response.

It’s essential to note that the expect module requires a secure connection between the Ansible control node and the target host. Therefore, it’s recommended to use SSH or HTTPS when using the expect module.

Conclusion

In conclusion, the expect module is a powerful tool that allows you to automate the interaction with command-line interfaces using Ansible. By sending predefined responses to expected prompts, you can automate complex scenarios that involve user input. To use the “expect” module, you need to have the pexpect module installed on your Ansible control node, and you must ensure a secure connection between the control node and the target host. With these considerations in mind, the expect module can be a valuable addition to your Ansible playbook for automating tasks that involve interactive command-line interfaces. 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 AWX 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