# Answering prompts from Ansible modules

**URL:** <https://forum.ansible.com/t/answering-prompts-from-ansible-modules/11193>\
**Category:** Get Help\
**Created:** [November 27, 2024, 7:50pm UTC](https://forum.ansible.com/t/answering-prompts-from-ansible-modules/11193 "2024-11-27T19:50:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shamilovstas](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/shamilovstas/32/5541_2.png) [@shamilovstas](https://forum.ansible.com/u/shamilovstas)\
**Post date:** [November 27, 2024, 7:50pm UTC](https://forum.ansible.com/t/answering-prompts-from-ansible-modules/11193/1 "2024-11-27T19:50:35Z")

</div>

In the module I’m developing I need to answer to a prompt while running a command using `AnsibleModule.run_command`. From what I have found so far, the most promising option is to use `data` parameter and send the answer to `stdin` of the command.

However, as far as I understand, it will only work in case if there is only one prompt. Consider the following bash script:

```bash
#!/usr/bin/env bash

read -p "Answer this (y/N) " ANSWER
read -p "Answer this #2 (y/N) " ANSWER2

RESULT=1
if ["$ANSWER" == "y"]
then
    echo "Answer #1 was: yes"
    RESULT=0
else
    echo "Answer #1 was: no"
    RESULT=1
fi
if ["$ANSWER2" == "y"]
then
    echo "Answer #2 was: yes"
    RESULT=0
else
    echo "Answer #2 was: no"
    RESULT=1
fi

exit $RESULT

```

In my module’s code I’m trying to execute this script, answering the script’s prompts:

```python
def main():
    module = AnsibleModule(
        argument_spec=dict(
            script=dict(type='str', required=True)
        )
    )

    script = module.params['script']

    rc, stdout, stderr = module.run_command(script, data='y')

    if rc == 0:
        module.exit_json()
    else:
        module.fail_json(msg="Prompt was not answered correctly")

```

If there was only one prompt in the bash script, the module would finish successfully. But since there are more, only there first prompt gets its answer sent through `data` parameter.

**Question**  
How to answer all prompts at once?

---

<div class="post-metadata">

**Author:** ![bvitnik](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bvitnik/32/4132_2.png) [@bvitnik](https://forum.ansible.com/u/bvitnik)\
**Post date:** [November 28, 2024, 12:25pm UTC](https://forum.ansible.com/t/answering-prompts-from-ansible-modules/11193/2 "2024-11-28T12:25:45Z")

</div>

Maybe use `expect` through Python `pexpect` library?

---

<div class="post-metadata">

**Author:** ![jrglynn2](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@jrglynn2](https://forum.ansible.com/u/jrglynn2)\
**Post date:** [December 2, 2024, 12:55pm UTC](https://forum.ansible.com/t/answering-prompts-from-ansible-modules/11193/3 "2024-12-02T12:55:24Z")

</div>

Wouldn’t it be better to just feed variables? So what used to be “prompts” in a Bash script would be variables in an ansible job?

`ansible-playbook/playbook.yml -e '{"prompt1":"answer1","prompt2":"answer2"}'`

Or feed it a variable file … this is what I do when I create a new server, I create a variable file with some variables defined (server name, subnet. etc.) - I do this for every server.

`ansible-playbook/playbook.yml -e "@config/FILENAME.yml"`
