# How to use regex to match a response using the expect module

**URL:** <https://forum.ansible.com/t/how-to-use-regex-to-match-a-response-using-the-expect-module/23834>\
**Category:** Ansible Project\
**Created:** [February 27, 2017, 11:57pm UTC](https://forum.ansible.com/t/how-to-use-regex-to-match-a-response-using-the-expect-module/23834 "2017-02-27T23:57:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Williams1](https://avatars.discourse-cdn.com/v4/letter/d/e274bd/32.png) [@David\_Williams1](https://forum.ansible.com/u/David_Williams1)\
**Post date:** [February 27, 2017, 11:57pm UTC](https://forum.ansible.com/t/how-to-use-regex-to-match-a-response-using-the-expect-module/23834/1 "2017-02-27T23:57:16Z")

</div>

I am stumped…I have been trying to use the expect module and format the expected response as a regex that can match multiple responses. I see posts all over where people are trying to match the response exactly and have to escape the regex special characters. It seems nobody writes regex to match multiple responses. Is this not possible?

**Playbook**

- name: Clear Permissions on /var/etc/pam.conf  
hosts: juniper\_ex4200  
tasks:
- name: Access member 0  
expect:  
command: ‘request session member 0’  
responses: “^—\sJUNOS.+\n(warning.+\n){5}{((backup)|(linecard)):((1)|(2))}”: “start shell user root”

**Error**

ERROR! Syntax Error while loading YAML.

The error appears to have been in ‘/home/dwilliams/ex4200\_permissions\_clear/ex4200\_permissions\_clear’: line 8, column 25, but may  
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

command: ‘request session member 0’  
responses: “^—\sJUNOS.+\n(warning.+\n){5}{((backup)|(linecard)):((1)|(2))}”  
^ here  
This one looks easy to fix. It seems that there is a value started  
with a quote, and the YAML parser is expecting to see the line ended  
with the same kind of quote. For instance:

when: “ok” in result.stdout

Could be written as:

when: ‘“ok” in result.stdout’

Or equivalently:

when: “‘ok’ in result.stdout”

---

<div class="post-metadata">

**Author:** ![Matt\_Clay](https://avatars.discourse-cdn.com/v4/letter/m/6bbea6/32.png) [@Matt\_Clay](https://forum.ansible.com/u/Matt_Clay)\
**Post date:** [February 28, 2017, 6:22am UTC](https://forum.ansible.com/t/how-to-use-regex-to-match-a-response-using-the-expect-module/23834/2 "2017-02-28T06:22:13Z")

</div>

David,

I know this isn’t what you asked, but since it looks like you’re working with JUNOS I thought I’d check to see if you’ve already looked at Ansible’s JUNOS support:

[https://docs.ansible.com/ansible/list\_of\_network\_modules.html#junos](https://docs.ansible.com/ansible/list_of_network_modules.html#junos)

- Matt

---

<div class="post-metadata">

**Author:** ![David\_Williams1](https://avatars.discourse-cdn.com/v4/letter/d/e274bd/32.png) [@David\_Williams1](https://forum.ansible.com/u/David_Williams1)\
**Post date:** [February 28, 2017, 12:59pm UTC](https://forum.ansible.com/t/how-to-use-regex-to-match-a-response-using-the-expect-module/23834/3 "2017-02-28T12:59:38Z")

</div>

Thank you Matt, I have looked at the modules but I’m trying to drop down into the linux shell and change some file permissions within the directory structure. It doesn’t look like the module can do that. Thanks for trying though! Any help is always appreciated.

---

<div class="post-metadata">

**Author:** ![ganeshrn](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/ganeshrn/32/1021_2.png) [@ganeshrn](https://forum.ansible.com/u/ganeshrn)\
**Post date:** [March 1, 2017, 8:44am UTC](https://forum.ansible.com/t/how-to-use-regex-to-match-a-response-using-the-expect-module/23834/4 "2017-03-01T08:44:46Z")

</div>

Hi David,

Changing the yml file as below at least loads the file. As per docs the key under responses is python regex so I think it should work.

```auto
- name: Access member 0
  expect:
    command: 'request session member 0'
    responses: 
      ^---\sJUNOS.+\n(warning.+\n){5}\{((backup)|(linecard)):((1)|(2))\}: start shell user root

```

However, I am trying to understand the need to use ‘expect’ module to run junos operational command.  
Can’t it be run using ‘junos\_command’ module? Does running this command prompts for an interactive input?

Regards,  
Ganesh
