---
title: 'Ansible Tip - Using the expect module'
url: 'https://www.linuxserver.io/blog/2018-03-23-ansible-tip-expect-module'
markdown: 'https://www.linuxserver.io/blog/2018-03-23-ansible-tip-expect-module.md'
date: '2018-03-23'
description: 'Another Ansible top tip. Suppose you are running an installer or other process via Ansible which expects the user to be present accept a license agreement, enter some information or something. Enter expect, a handy little utility which can listen for certain strings in stdout and react accordingly.…'
taxonomy:
  tag:
    - ansible
    - linux
---

# Ansible Tip - Using the expect module

Another Ansible top tip. Suppose you are running an installer or other process via Ansible which expects the user to be present accept a license agreement, enter some information or something. Enter `expect`, a handy little utility which can listen for certain strings in stdout and react accordingly.

```
- name: expect example
  expect:
    echo: yes
    chdir: "{{ dir }}"
    command: "./{{ exectuable_filename }}"
    timeout: "{{ timeout }}"
    responses:
      (.*)Please enter your name(.*): "Fred"
      (.*)Please enter your age(>*): "37"
      (.*)db port(.*): "{{ db_port }}"
      (.*)db user(.*): "{{ db_user }}"
      (.*)db pass(.*): "{{ db_pass }}"
    register: expect_example_result
    failed_when: "expect_example_result.rc != 0 and 'Success' not in expect_example_result.stdout"
```

The responses can be parameterised via variables or hard coded. We can also see that using the `failed_when` option it is easily possible to react to specific events and fail on certain conditions.

---

## Navigation

- Parent: [Blog](https://www.linuxserver.io/blog.md)
- Previous: [The dangers of pulling :latest](https://www.linuxserver.io/blog/2018-04-22-the-dangers-of-pulling-latest.md)
- Next: [Ansible Tip - Referencing items from a hash](https://www.linuxserver.io/blog/2018-03-23-an-ansible-tip-for-referencing-items-from.md)
