# Task to input fields into a interactive database script.

**URL:** <https://forum.ansible.com/t/task-to-input-fields-into-a-interactive-database-script/33668>\
**Category:** Ansible Developer\
**Created:** [August 24, 2020, 4:30pm UTC](https://forum.ansible.com/t/task-to-input-fields-into-a-interactive-database-script/33668 "2020-08-24T16:30:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jonin](https://avatars.discourse-cdn.com/v4/letter/j/87869e/32.png) [@Jonin](https://forum.ansible.com/u/Jonin)\
**Post date:** [August 24, 2020, 4:30pm UTC](https://forum.ansible.com/t/task-to-input-fields-into-a-interactive-database-script/33668/1 "2020-08-24T16:30:43Z")

</div>

Hi,

I am looking to automate this task within ansible and have no idea really how to do this.

I run the set up script manually on the host(s) like this:

I call the “setup” script below with npm run options:

npm run setup

The script then prompts for my user input to provide the following details:

- Postgresql Superuser (for database creation) [default: postgres]:postgres
- New Database Username (for readonly access) [default: postcodesio]:postcodesio
- Password for new user [default: secret]:secret
- Postgresql Host [default: localhost]:localhost
- Database Name [default: postcodesiodb]:postcodesiodb
- Postgresql Port: [default: 5432]:5432

Once these have been provided the script completes. Pleas help in advising how I can go about automating this in ansible.

Thanks

---

<div class="post-metadata">

**Author:** ![Karol\_Czeryna](https://avatars.discourse-cdn.com/v4/letter/k/ed655f/32.png) [@Karol\_Czeryna](https://forum.ansible.com/u/Karol_Czeryna)\
**Post date:** [August 24, 2020, 4:34pm UTC](https://forum.ansible.com/t/task-to-input-fields-into-a-interactive-database-script/33668/2 "2020-08-24T16:34:45Z")

</div>

Hi,

I think you can use [https://docs.ansible.com/ansible/latest/modules/expect\_module.html](https://docs.ansible.com/ansible/latest/modules/expect_module.html)

Best,  
Karol

---

<div class="post-metadata">

**Author:** ![Jonin](https://avatars.discourse-cdn.com/v4/letter/j/87869e/32.png) [@Jonin](https://forum.ansible.com/u/Jonin)\
**Post date:** [August 25, 2020, 9:52pm UTC](https://forum.ansible.com/t/task-to-input-fields-into-a-interactive-database-script/33668/3 "2020-08-25T21:52:20Z")

</div>

Thanks Karol,

I’ve given that a go. Initially wasn’t working because I didn’t have the pexpect module installed but I think I have that sorted with the following dependencies installed now: epel, pip and pexpect \> version 3.3.

I have tried running this task but it just hangs for the entire timeout duration I have set.

- name: Setup postcodesiodb  
expect:  
echo: yes  
chdir: “/apps/was/[postcodes.io](http://postcodes.io)”  
command: npm run setup  
timeout: “10”  
responses:  
Question:
- Postgresql Superuser (for database creation) [default’:’ postgres]: “postgres”
- New Database Username (for readonly access) [default’:’ postcodesio]: “postcodesio”
- Password for new user [default’:’ secret]: “secret”
- Postgresql Host [default’:’ localhost]: “localhost”
- Database Name [default’:’ postcodesiodb]: “postcodesiodb”
- Postgresql Port [default’:’ 5432]: “5432”  
tags:
- setup
- install

I have also tried with no space after the final colon but that then changes the font to all orange which doesn’t seem right. Regardless, i tried it but the error was still exactly the same as below:

Fatal: [hostname]: FAILED! =\> {“changed”: true, “cmd”: “npm run setup”, “delta”: “0:02:00.145566”, “end”: “2020-08-25 21:33:22.107526”, “msg”: “command exceeded timeout”, “rc”: null, “start”: “2020-08-25 21:31:21.961960”,  
…  
“Postgresql Superuser (for database creation) [default: postgres]:”]}

This is the only error logging I have but it’s failed at the first input I am attempting to populate here the postgres Super user. Any ideas how I need to pass in these answers and or variables if I don’t want to hard code? Currently it looks like it is picking up a corrupt / blank value as shown by those chatacters in the green section below.  
“Postgresql Superuser (for database creation) [default: postgres]:”]}

The wording / syntax of the questions when the script is called is exactly like this:  
Postgresql Superuser (for database creation) [default: postgres]:

Any ideas much appreciated.

Regards,

S

---

<div class="post-metadata">

**Author:** ![Jonin](https://avatars.discourse-cdn.com/v4/letter/j/87869e/32.png) [@Jonin](https://forum.ansible.com/u/Jonin)\
**Post date:** [August 26, 2020, 4:20pm UTC](https://forum.ansible.com/t/task-to-input-fields-into-a-interactive-database-script/33668/4 "2020-08-26T16:20:41Z")

</div>

Is it just a question of how I frame my inputs or am I missing the point here completely?

I’ve tried the likes of below but still the same error

- name: Setup postcodesiodb  
expect:  
echo: yes  
chdir: “/apps/was/postcodes.io”  
command: npm run setup  
timeout: “10”  
responses:  
Question:
- ‘[postgres]’
- ‘[postcodesio]’
- ‘[secret]’
- ‘[localhost]’
- ‘[postcodesiodb]’
- ‘[5432]’  
tags:
- setup
- install
- test

Error: “Postgresql Superuser (for database creation) [default: postgres]:”]}

I am struggling to find any other working solutions for ansible to manage an interactive script such as this so any help much appreciated.
