Skip to content

Instantly share code, notes, and snippets.

@bmannix
Last active January 12, 2018 05:10
Show Gist options
  • Save bmannix/00b9b2a7d95eb3f74a92 to your computer and use it in GitHub Desktop.
Save bmannix/00b9b2a7d95eb3f74a92 to your computer and use it in GitHub Desktop.
Ansible driven expect script
#!/usr/bin/expect
set timeout 30
#setup some abitrary commands
set cmdstr(0) "cd /opt/obb/server"
set cmdstr(1) ". build/setup"
set cmdstr(2) "rebuild_db"
spawn /bin/bash
#send the first command
send "$cmdstr(0)\n"
#wait for a prompt to appear
expect -re "\$"
send "$cmdstr(1)\n"
expect -re "\$"
send "$cmdstr(2)\n"
expect "Would you like to create one now? (yes/no):"
send "yes\n"
expect "Username (Leave blank to use 'obb_django'):"
#The CAPS lines below will be completely overwritten by the lineinfile command in the yml playbook
send "USERNAME\n"
expect "E-mail address:"
send "EMAIL\n"
expect "Password:"
send "PASSWORD\n"
expect "Password (again):"
send "AGAIN\n"
#Wait for last bit of output from commands
expect "Added default components"
expect -re "\r\n"
...
vars_prompt:
- name: "username"
prompt: "Enter the django auth system superuser username"
private: no
- name: "email"
prompt: "Enter the email address to use for the superuser"
private: no
- name: "password"
prompt: "Enter the password to use for the superuser account"
private: yes
...
- name: Copy exp file
copy: src=plays/auto_rebuild.exp dest={{ project_root }}/server
- name: Add user defined config to expect script
lineinfile: dest={{ project_root }}/server/auto_rebuild.exp
regexp='USERNAME'
line='send "{{ username }}\n"'
- name: Add user defined config to expect script
lineinfile: dest={{ project_root }}/server/auto_rebuild.exp
regexp='EMAIL'
line='send "{{ email }}\n"'
- name: Add user defined config to expect script
lineinfile: dest={{ project_root }}/server/auto_rebuild.exp
regexp='PASSWORD'
line='send "{{ password }}\n"'
- name: Add user defined config to expect script
lineinfile: dest={{ project_root }}/server/auto_rebuild.exp
regexp='AGAIN'
line='send "{{ password }}\n"'
- name: set perms exp
file: path={{ project_root }}/server/auto_rebuild.exp owner={{ dbuser }} group={{ dbuser }} mode=0750
- name: Rebuild w/ exp
sudo_user: "{{ dbuser }}
shell: "{{ project_root }}/server/auto_rebuild.exp"
- name: Remove expect script
file: path="{{ project_root }}/server/auto_rebuild.exp"
state=absent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment