expect model not working accoring to plan

Hi all,

It is me again with config on boxes
I am using the expect module it is working but not doing what I want it to do
This is the code

name: backup of juniper using expect
hosts: juniper
connection: local
tasks:

This is what I expect it to do (copy config to ftp server)

ansible@ansible-new:~$ ssh admin@10.101.250.41
Password:
— JUNOS 12.1X46-D55.3 built 2016-07-08 18:46:54 UTC
admin@POD1_SRX210> ...ansible@10.241.11.237/juniper.conf.49.gz
Password for ansible@10.241.11.237:
ftp://ansible@10.241.11.237/juniper.conf.49.gz100% of 1035 B 549 kBps
admin@POD1_SRX210>

outcome of script
ansible@ansible-new:/etc/ansible$ ansible-playbook backup-juniper-expect.yml -vvv
ansible-playbook 2.4.2.0
config file = /etc/ansible/ansible.cfg
configured module search path = [u’/home/ansible/.ansible/plugins/modules’, u’/usr/share/ansible/plugins/modules’]
ansible python module location = /usr/lib/python2.7/dist-packages/ansible
executable location = /usr/bin/ansible-playbook
python version = 2.7.12 (default, Dec 4 2017, 14:50:18) [GCC 5.4.0 20160609]
Using /etc/ansible/ansible.cfg as config file
Parsed /etc/ansible/hosts inventory source with ini plugin
[WARNING]: While constructing a mapping from /etc/ansible/backup-juniper-
expect.yml, line 12, column 9, found a duplicate dict key (admin@POD1_SRX210>).
Using last defined value only.

PLAYBOOK: backup-juniper-expect.yml ********************************************
1 plays in backup-juniper-expect.yml
PLAY [backup of juniper using expect] ******************************************
TASK [Gathering Facts] *********************************************************
Using module file /usr/lib/python2.7/dist-packages/ansible/modules/system/setup.py
<10.201.250.41> ESTABLISH LOCAL CONNECTION FOR USER: ansible
<10.201.250.41> EXEC /bin/sh -c ‘echo ~ && sleep 0’
<10.201.250.41> EXEC /bin/sh -c ‘( umask 77 && mkdir -p “echo /home/ansible/.ansible/tmp/ansible-tmp-1520595523.63-216576413530619” && echo ansible-tmp-1520595523.63-216576413530619=“echo /home/ansible/.ansible/tmp/ansible-tmp-1520595523.63-216576413530619” ) && sleep 0’
<10.201.250.41> PUT /tmp/tmpeyrq7Y TO /home/ansible/.ansible/tmp/ansible-tmp-1520595523.63-216576413530619/setup.py
<10.201.250.41> EXEC /bin/sh -c ‘chmod u+x /home/ansible/.ansible/tmp/ansible-tmp-1520595523.63-216576413530619/ /home/ansible/.ansible/tmp/ansible-tmp-1520595523.63-216576413530619/setup.py && sleep 0’
<10.201.250.41> EXEC /bin/sh -c ‘/usr/bin/python /home/ansible/.ansible/tmp/ansible-tmp-1520595523.63-216576413530619/setup.py; rm -rf “/home/ansible/.ansible/tmp/ansible-tmp-1520595523.63-216576413530619/” > /dev/null 2>&1 && sleep 0’
ok: [10.201.250.41]
META: ran handlers
TASK [copy running config to ftp server] ***************************************
task path: /etc/ansible/backup-juniper-expect.yml:8
Using module file /usr/lib/python2.7/dist-packages/ansible/modules/commands/expect.py
<10.201.250.41> ESTABLISH LOCAL CONNECTION FOR USER: ansible
<10.201.250.41> EXEC /bin/sh -c ‘echo ~ && sleep 0’
<10.201.250.41> EXEC /bin/sh -c ‘( umask 77 && mkdir -p “echo /home/ansible/.ansible/tmp/ansible-tmp-1520595523.97-183325662742143” && echo ansible-tmp-1520595523.97-183325662742143=“echo /home/ansible/.ansible/tmp/ansible-tmp-1520595523.97-183325662742143” ) && sleep 0’
<10.201.250.41> PUT /tmp/tmpSdr_De TO /home/ansible/.ansible/tmp/ansible-tmp-1520595523.97-183325662742143/expect.py
<10.201.250.41> EXEC /bin/sh -c ‘chmod u+x /home/ansible/.ansible/tmp/ansible-tmp-1520595523.97-183325662742143/ /home/ansible/.ansible/tmp/ansible-tmp-1520595523.97-183325662742143/expect.py && sleep 0’
<10.201.250.41> EXEC /bin/sh -c ‘/usr/bin/python /home/ansible/.ansible/tmp/ansible-tmp-1520595523.97-183325662742143/expect.py; rm -rf “/home/ansible/.ansible/tmp/ansible-tmp-1520595523.97-183325662742143/” > /dev/null 2>&1 && sleep 0’
changed: [10.201.250.41] => {
“changed”: true,
“cmd”: “ssh admin@10.101.250.41”,
“delta”: “0:00:02.954460”,
“end”: “2018-03-09 11:38:47.026698”,
“invocation”: {
“module_args”: {
“chdir”: null,
“command”: “ssh admin@10.101.250.41”,
“creates”: null,
“echo”: false,
“removes”: null,
“responses”: {
“Password”: “xxxxxx”,
“Password for ansible@10.241.11.237”: “yyyyy”,
“admin@POD1_SRX210>”: [
“exit”
]
},
“timeout”: 30
}
},
“rc”: 0,
“start”: “2018-03-09 11:38:44.072238”,
“stdout”: “Password:\r\n— JUNOS 12.1X46-D55.3 built 2016-07-08 18:46:54 UTC\r\nadmin@POD1_SRX210> exit \r\n\r\nConnection to 10.101.250.41 closed.”,
“stdout_lines”: [
“Password:”,
“— JUNOS 12.1X46-D55.3 built 2016-07-08 18:46:54 UTC”,
"admin@POD1_SRX210> exit ",
“”,
“Connection to 10.101.250.41 closed.”
]
}
META: ran handlers
META: ran handlers
PLAY RECAP *********************************************************************
10.201.250.41 : ok=2 changed=1 unreachable=0 failed=0

You can only have one reposes with the same question/prompt, you have two of "admin@POD1_SRX210>" the last one will overwrite the fist one.
So you need to merge them and have two list elements.

   admin@POD1_SRX210>:
     - file copy /config/juniper.conf.49.gz
     - exit

Thanks

Changed code to this

Need more detailed information, run
ANSIBLE_STDOUT_CALLBACK=debug ansible-playbook -vvv <your-playbook> ...

Thanks

This is the debug mode of the playbook

ansible@ansible-new:/etc/ansible$ ANSIBLE_STDOUT_CALLBACK=debug ansible-playbook -vvv backup-juniper-expect.yml
ansible-playbook 2.4.2.0
config file = /etc/ansible/ansible.cfg
configured module search path = [u’/home/ansible/.ansible/plugins/modules’, u’/usr/share/ansible/plugins/modules’]
ansible python module location = /usr/lib/python2.7/dist-packages/ansible
executable location = /usr/bin/ansible-playbook
python version = 2.7.12 (default, Dec 4 2017, 14:50:18) [GCC 5.4.0 20160609]
Using /etc/ansible/ansible.cfg as config file
Parsed /etc/ansible/hosts inventory source with ini plugin
PLAYBOOK: backup-juniper-expect.yml ********************************************
1 plays in backup-juniper-expect.yml
PLAY [backup of juniper using expect] ******************************************
TASK [Gathering Facts] *********************************************************
Using module file /usr/lib/python2.7/dist-packages/ansible/modules/system/setup.py
<10.201.250.41> ESTABLISH LOCAL CONNECTION FOR USER: ansible
<10.201.250.41> EXEC /bin/sh -c ‘echo ~ && sleep 0’
<10.201.250.41> EXEC /bin/sh -c ‘( umask 77 && mkdir -p “echo /home/ansible/.ansible/tmp/ansible-tmp-1520608575.06-81868420467920” && echo ansible-tmp-1520608575.06-81868420467920=“echo /home/ansible/.ansible/tmp/ansible-tmp-1520608575.06-81868420467920” ) && sleep 0’
<10.201.250.41> PUT /tmp/tmpa6lrDQ TO /home/ansible/.ansible/tmp/ansible-tmp-1520608575.06-81868420467920/setup.py
<10.201.250.41> EXEC /bin/sh -c ‘chmod u+x /home/ansible/.ansible/tmp/ansible-tmp-1520608575.06-81868420467920/ /home/ansible/.ansible/tmp/ansible-tmp-1520608575.06-81868420467920/setup.py && sleep 0’
<10.201.250.41> EXEC /bin/sh -c ‘/usr/bin/python /home/ansible/.ansible/tmp/ansible-tmp-1520608575.06-81868420467920/setup.py; rm -rf “/home/ansible/.ansible/tmp/ansible-tmp-1520608575.06-81868420467920/” > /dev/null 2>&1 && sleep 0’
ok: [10.201.250.41]
META: ran handlers
TASK [copy running config to ftp server] ***************************************
task path: /etc/ansible/backup-juniper-expect.yml:8
Using module file /usr/lib/python2.7/dist-packages/ansible/modules/commands/expect.py
<10.201.250.41> ESTABLISH LOCAL CONNECTION FOR USER: ansible
<10.201.250.41> EXEC /bin/sh -c ‘echo ~ && sleep 0’
<10.201.250.41> EXEC /bin/sh -c ‘( umask 77 && mkdir -p “echo /home/ansible/.ansible/tmp/ansible-tmp-1520608575.4-159624176973195” && echo ansible-tmp-1520608575.4-159624176973195=“echo /home/ansible/.ansible/tmp/ansible-tmp-1520608575.4-159624176973195” ) && sleep 0’
<10.201.250.41> PUT /tmp/tmpFl10kg TO /home/ansible/.ansible/tmp/ansible-tmp-1520608575.4-159624176973195/expect.py
<10.201.250.41> EXEC /bin/sh -c ‘chmod u+x /home/ansible/.ansible/tmp/ansible-tmp-1520608575.4-159624176973195/ /home/ansible/.ansible/tmp/ansible-tmp-1520608575.4-159624176973195/expect.py && sleep 0’
<10.201.250.41> EXEC /bin/sh -c ‘/usr/bin/python /home/ansible/.ansible/tmp/ansible-tmp-1520608575.4-159624176973195/expect.py; rm -rf “/home/ansible/.ansible/tmp/ansible-tmp-1520608575.4-159624176973195/” > /dev/null 2>&1 && sleep 0’
The full traceback is:
File “/tmp/ansible_lyzE9I/ansible_module_expect.py”, line 109, in wrapped
return next(resp_gen)
fatal: [10.201.250.41]: FAILED! => {
“changed”: false,
“invocation”: {
“module_args”: {
“chdir”: null,
“command”: “ssh admin@10.101.250.41”,
“creates”: null,
“echo”: false,
“removes”: null,
“responses”: {
“Password”: “xxxxxx”,
“Password for ansible@10.241.11.237”: “yyyyy”,
“admin@POD1_SRX210>”: [
“file copy /config/juniper.conf.49.gz ftp://ansible@10.241.11.237/juniper.conf.49.gz”,
“exit”
]
},
“timeout”: 30
}
}
}
MSG:
No remaining responses for ‘admin@POD1_SRX210>’, output was ’ …er.conf.49.gz fadmin@POD1_SRX210>‘.11.237/juniper.co
[WARNING]: Could not create retry file ‘/etc/ansible/backup-juniper-
expect.retry’. [Errno 13] Permission denied: u’/etc/ansible/backup-
juniper-expect.retry’

PLAY RECAP *********************************************************************
10.201.250.41 : ok=1 changed=0 unreachable=0 failed=1

This code works when I comment out the following in red

  • name: backup of juniper using expect
    hosts: juniper
    connection: local
    tasks:
  • name: copy running config to ftp server
    expect:
    command: ssh admin@10.101.250.41
    responses:
    Password: xxxxxx
    admin@POD1_SRX210>:

- file copy /config/juniper.conf.49.gz ftp://ansible@10.241.11.237/juniper.conf.49.gz

  • exit

Password for ansible@10.241.11.237: yyyyyyy

outcome is
changed: [10.201.250.41] => {
“changed”: true,
“cmd”: “ssh admin@10.101.250.41”,
“delta”: “0:00:02.905152”,
“end”: “2018-03-09 15:22:42.046306”,
“invocation”: {
“module_args”: {
“chdir”: null,
“command”: “ssh admin@10.101.250.41”,
“creates”: null,
“echo”: false,
“removes”: null,
“responses”: {
“Password”: “xxxxxx”,
“admin@POD1_SRX210>”: [
“exit”
]
},
“timeout”: 30
}
},
“rc”: 0,
“start”: “2018-03-09 15:22:39.141154”,
“stdout”: “Password:\r\n— JUNOS 12.1X46-D55.3 built 2016-07-08 18:46:54 UTC\r\nadmin@POD1_SRX210> exit \r\n\r\nConnection to 10.101.250.41 closed.”,
“stdout_lines”: [
“Password:”,
“— JUNOS 12.1X46-D55.3 built 2016-07-08 18:46:54 UTC”,
"admin@POD1_SRX210> exit ",
“”,
“Connection to 10.101.250.41 closed.”

I was hoping the -vvv would give some more information that this.

Since it's reporting No remaining responses for 'admin@POD1_SRX210>' could you add a few exit to the admin@POD1_SRX210> and try to run it again.

Thanks

I added one more exit making it two exit at the same place and still error message

The full traceback is:
File “/tmp/ansible_BvrW7j/ansible_module_expect.py”, line 109, in wrapped
return next(resp_gen)
fatal: [10.201.250.41]: FAILED! => {
“changed”: false,
“invocation”: {
“module_args”: {
“chdir”: null,
“command”: “ssh admin@10.101.250.41”,
“creates”: null,
“echo”: false,
“removes”: null,
“responses”: {
“Password”: “xxxxxx”,
“Password for ansible@10.241.11.237”: “yyyyy”,
“admin@POD1_SRX210>”: [
“file copy /config/juniper.conf.49.gz ftp://ansible@10.241.11.237/juniper.conf.49.gz”,
“exit”,
“exit”
]
},
“timeout”: 30
}
},
“msg”: “No remaining responses for ‘admin@POD1_SRX210>’, output was ’ for ansible@10.241.11.237:\r\nfetch: ftp://ansible@10.241.11.237/juniper.conf.49.gz: Not logged in\r\nerror: put-file failed\r\nerror: could not send local copy of file\r\n\r\nadmin@POD1_SRX210>'”
}
[WARNING]: Could not create retry file ‘/etc/ansible/backup-juniper-
expect.retry’. [Errno 13] Permission denied: u’/etc/ansible/backup-
juniper-expect.retry’

PLAY RECAP *********************************************************************
10.201.250.41 : ok=1 changed=0 unreachable=0 failed=1
ansible@ansible-new:/etc/ansible$

Another issue is that manually after copying file before it exits
it displays this message
ftp://ansible@10.241.11.237/juniper.conf.49.gz100% of 1035 B 317 kBps
I am not sure if this matters

Thanks

I added one more exit making it two exit at the same place and still error
message

I would suggest you add more exit to see if you get anywhere.

    "msg": "No remaining responses for 'admin@POD1_SRX210>', output was '
for ansible@10.241.11.237:\r\nfetch:
ftp://ansible@10.241.11.237/juniper.conf.49.gz: Not logged in\r\nerror:
put-file failed\r\nerror: could not send local copy of
file\r\n\r\nadmin@POD1_SRX210>'"

Now you have a message "Not logged in", maybe something goes wrong the second password entry.

Another issue is that manually after copying file before it exits
it displays this message
ftp://ansible@10.241.11.237/juniper.conf.49.gz100% of 1035 B 317 kBps
I am not sure if this matters

I wouldn't think that it should cause any problems but it might since I think it the percentages is counting up.
To overwrite the percentage on update it need to send some control characters like backspace to go back and overwrite, in you second output it showed a lot of \b maybe that's backspaces.

Is it possible to turn that output off?

Thanks

I am sorry not sure of how to write this backspace
can you give an example please

That is not what I tried to say, I was trying to explain what is happening and why it might fails.
So I rephrase, maybe I become more understandable.

You said that Juniper write this line

   ftp://ansible@10.241.11.237/juniper.conf.49.gz100% of 1035 B 317 kBps

I have never used Juniper, but I guess that the percentage is counting upwards from 0% to 100% on that line while the copying is in progress.
For Juniper to do this one line it need to delete the 0% and write 10%, one way to do that is for Juniper to send backspace to the terminal to delete the text.
When it has deleted 0% it can write out 10%, the it send more backspaces to delete 10% for so to write out 20% and so on to 100%.

The reason I think that Juniper might be doing this is the output you had

     "msg": "No remaining responses for 'admin@POD1_SRX210>', output was '
     ...er.conf.49.gz ftp://ansible@10.24
     \b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b1.11.237/juniper.co\radmin@POD1_SRX210>'"

\b is backspace, so Juniper at some point is sending backspace, probably to delete some text in the output.
This might be a problem for expect, I have never used devices that sends backspaces to the console so I don't know.

Your best bet is to find a way to turn of the progress printing that Juniper is doing.