Here I have creating script which execute the ansible-playbook, After the execute script which ask the Sudo password which one I have to send(sudo password) though expect and send but getting below error:

Script :

$ cat changedns

#!/usr/bin/expect -f
spawn ansible-playbook -i "$1," /home/admin/ansible/test.yml -K 
match_max 100000
expect "SUDO password:"
send -- "admin@123\r"
send -- "\r"
expect eof


$ ./changedns bn
can't read "1": no such variable
    while executing
"spawn ansible-playbook -i "$1," /home/adminto/ansible/test.yml -K "
    (file "./changedns" line 2)

Anyone have idea how to deal with this ?

  • 2
    1) That's an expect script, not a shell script; 2) You appear to have an extra newline at the start, which likely makes it into a shell script (if you spawn it from a shell), except that 3) those are tcl errors so it is running expect regardless. – Michael Homer Mar 3 '17 at 6:16
  • 4
    You can't reference command line arguments like that in an expect script: see for example How to pass argument in expect through command line in shell script – steeldriver Mar 3 '17 at 6:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.