Automating Command-Line Interfaces with Ansible expect Module
Learn how to use Ansible expect module to automate interactive command-line interfaces and streamline your workflow. This guide covers installation, implementation, and best practices for secure and efficient automation.


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.
The Best Resources For Ansible
Video Course
Printed Book
eBooks
- Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
- Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
- Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
- Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
- Ansible For Containers and Kubernetes By Examples: 20+ Automation Examples To Automate Containers, Kubernetes and OpenShift
- Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
- Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
- Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
- Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
- Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
Links
Recap
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, Website, Twitter, and Substack to not miss the next episode of the Ansible Pilot.
Academy
Learn the Ansible automation technology with some real-life examples in my
My book Ansible By Examples: 200+ Automation Examples For Linux and AWX System Administrator and DevOps
Donate
Want to keep this project going? Please donate