# Ansible Shell With Expect

**URL:** <https://forum.ansible.com/t/ansible-shell-with-expect/29443>\
**Category:** Ansible Project\
**Created:** [January 15, 2019, 3:10pm UTC](https://forum.ansible.com/t/ansible-shell-with-expect/29443 "2019-01-15T15:10:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Freddie\_Eisa](https://avatars.discourse-cdn.com/v4/letter/f/ecae2f/32.png) [@Freddie\_Eisa](https://forum.ansible.com/u/Freddie_Eisa)\
**Post date:** [January 15, 2019, 3:10pm UTC](https://forum.ansible.com/t/ansible-shell-with-expect/29443/1 "2019-01-15T15:10:34Z")

</div>

We are trying the following

- name: Configure Orion Agent

shell: |

set timeout 30

spawn service swiagentd init

expect “Choose an option\>”

send “2”

exit 0

args:

executable: /usr/bin/expect

We are able to spawn the process but it looks like the expect part never works

“changed”: true,

“cmd”: “set timeout 30\n spawn service swiagentd init\n\n expect "Choose an option\>"\n send "2"\n\n exit 0”,

“delta”: “0:00:00.132261”,

“end”: “2019-01-15 10:08:43.611145”,

“invocation”: {

“module\_args”: {

“\_raw\_params”: “set timeout 30\n spawn service swiagentd init\n\n expect "Choose an option\>"\n send "2"\n\n exit 0”,

“\_uses\_shell”: true,

“argv”: null,

“chdir”: null,

“creates”: null,

“executable”: “/usr/bin/expect”,

“removes”: null,

“stdin”: null,

“warn”: true

}

},

“rc”: 0,

“start”: “2019-01-15 10:08:43.478884”,

“stderr”: “”,

“stderr\_lines”: ,

“stdout”: “spawn service swiagentd init\r\n\r\nSolarWinds Orion Agent [2.0.0.70] Settings, OpenSSL [OpenSSL 1.0.2k-fips 26 Jan 2017]\r\n 0. Cancel and Exit without Saving\r\n 1. Agent Mode (1 - Agent Initiated, 0 - Server Initiated) [1]\r\n 2. Orion Poller Hostname/IP [localhost]\r\n 3. Orion Poller Port [17778]\r\n 4. Orion Username [admin]\r\n 5. Orion Password \r\n 6. Proxy Settings… [mode=disabled]\r\n 7. Save Changes and Exit\r\n\r\nChoose an option\>”,

“stdout\_lines”: [

“spawn service swiagentd init”,

“”,

“SolarWinds Orion Agent [2.0.0.70] Settings, OpenSSL [OpenSSL 1.0.2k-fips 26 Jan 2017]”,

" 0. Cancel and Exit without Saving",

" 1. Agent Mode (1 - Agent Initiated, 0 - Server Initiated) [1]",

" 2. Orion Poller Hostname/IP [localhost]",

" 3. Orion Poller Port [17778]",

" 4. Orion Username [admin]",

" 5. Orion Password ",

" 6. Proxy Settings… [mode=disabled]",

" 7. Save Changes and Exit",

“”,

“Choose an option\>”

]

}

---

<div class="post-metadata">

**Author:** ![Freddie\_Eisa](https://avatars.discourse-cdn.com/v4/letter/f/ecae2f/32.png) [@Freddie\_Eisa](https://forum.ansible.com/u/Freddie_Eisa)\
**Post date:** [January 15, 2019, 3:48pm UTC](https://forum.ansible.com/t/ansible-shell-with-expect/29443/2 "2019-01-15T15:48:07Z")

</div>

We are a little further, the second expect is not working

- name: Configure Orion Agent  
shell: |  
set timeout 30  
spawn service swiagentd init

expect “Choose an option\>”  
send “2\n”  
expect “Enter Orion Poller Hostname/IP”  
send “[orion.hq.domain.com](http://orion.hq.domain.com)”

exit 0  
args:  
executable: /usr/bin/expect

changed: [[vm-host04.hq.domain.com](http://vm-host04.hq.domain.com)] =\> {  
“changed”: true,  
“cmd”: “set timeout 30\n spawn service swiagentd init\n\n expect "Choose an option\>"\n send "2\n"\n expect "Enter Orion Poller Hostname/IP"\n send "[orion.hq.domain.com](http://orion.hq.domain.com)"\n\n exit 0”,  
“delta”: “0:00:00.124392”,  
“end”: “2019-01-15 10:46:13.836854”,  
“invocation”: {  
“module\_args”: {  
“\_raw\_params”: “set timeout 30\n spawn service swiagentd init\n\n expect "Choose an option\>"\n send "2\n"\n expect "Enter Orion Poller Hostname/IP"\n send "[orion.hq.domain.com](http://orion.hq.domain.com)"\n\n exit 0”,  
“\_uses\_shell”: true,  
“argv”: null,  
“chdir”: null,  
“creates”: null,  
“executable”: “/usr/bin/expect”,  
“removes”: null,  
“stdin”: null,  
“warn”: true  
}  
},  
“rc”: 0,  
“start”: “2019-01-15 10:46:13.712462”,  
“stderr”: “”,  
“stderr\_lines”: ,  
“stdout”: “spawn service swiagentd init\r\n\r\nSolarWinds Orion Agent [2.0.0.70] Settings, OpenSSL [OpenSSL 1.0.2k-fips 26 Jan 2017]\r\n 0. Cancel and Exit without Saving\r\n 1. Agent Mode (1 - Agent Initiated, 0 - Server Initiated) [1]\r\n 2. Orion Poller Hostname/IP [localhost]\r\n 3. Orion Poller Port [17778]\r\n 4. Orion Username [admin]\r\n 5. Orion Password \r\n 6. Proxy Settings… [mode=disabled]\r\n 7. Save Changes and Exit\r\n\r\nChoose an option\>2\r\nEnter Orion Poller Hostname/IP”,  
“stdout\_lines”: [  
“spawn service swiagentd init”,  
“”,  
“SolarWinds Orion Agent [2.0.0.70] Settings, OpenSSL [OpenSSL 1.0.2k-fips 26 Jan 2017]”,  
" 0. Cancel and Exit without Saving",  
" 1. Agent Mode (1 - Agent Initiated, 0 - Server Initiated) [1]“,  
" 2. Orion Poller Hostname/IP [localhost]”,  
" 3. Orion Poller Port [17778]“,  
" 4. Orion Username [admin]”,  
" 5. Orion Password “,  
" 6. Proxy Settings… [mode=disabled]”,  
" 7. Save Changes and Exit",  
“”,  
“Choose an option\>2”,  
“Enter Orion Poller Hostname/IP”

---

<div class="post-metadata">

**Author:** ![Freddie\_Eisa](https://avatars.discourse-cdn.com/v4/letter/f/ecae2f/32.png) [@Freddie\_Eisa](https://forum.ansible.com/u/Freddie_Eisa)\
**Post date:** [January 15, 2019, 5:19pm UTC](https://forum.ansible.com/t/ansible-shell-with-expect/29443/3 "2019-01-15T17:19:25Z")

</div>

One more update. Looks like most of this is working now besides the final part. My assumption is I need to wildcard or use a different escape character. Please provide any feedback

- name: Configure Orion Agent  
shell: |  
set timeout 30  
spawn service swiagentd init

expect “Choose an option\>”  
send “2\n”  
expect “Enter Orion Poller Hostname/IP”  
send “[orion.hq.crabel.com](http://orion.hq.crabel.com)\n”  
expect “Choose an option\>”  
send “4\n”  
expect “Enter Orion Username”  
send “orion\_agent\n”  
expect “Choose an option\>”  
send “5\n”  
expect “Enter Orion Password”  
send “{{ orion\_agent\_password }}\n”  
expect “Choose an option\>”  
send “7\n”  
expect “…”  
send “\n”  
args:  
executable: /usr/bin/expect

“”,  
“Choose an option\>7”,  
“”,  
“Agent device ID [F988BC59-EEC5-42E3-81BE-6E854E5A1000], Provisioned by [ch1-mntrw001], device ID [A7C87BAE-07A0-4D74-A3CE-D0810B04B347]”,  
“Press ENTER to continue…”
