6

I want to run a shell script using ansible but the shell script requires user input to execute successfully,

For example: my shell script asking the unique id for an ossec agent, through ansible I can able to predefined my unique id (user input).

1
  • expect module might help. Commented May 13, 2019 at 10:44

2 Answers 2

6

You need to use EXPECT module: https://docs.ansible.com/ansible/latest/modules/expect_module.html

- name: Case insensitive unique id string match
  expect:
    command: ./myscript.sh
    responses:
      "Please enter your uniqueid": "myID"
      # Your input in myscript.sh
  no_log: true

Requirements

The below requirements are needed on the host that executes this module.

python >= 2.6

pexpect >= 3.3

Sign up to request clarification or add additional context in comments.

2 Comments

it is possible to call a unique id from file? I had a 100 unique id
U can parse content from your text file to string variable and afterwards run expect&command with "with_items" option, example, if yours ids in file comma separated, then u can add: with_items: {{ myStringVar.split(',') }}, and in responses specify item: "Please enter your uniqueid": "{{ item }}"
0

This could help you as a starting point:

- hosts: localhost
  connection: local

  vars_prompt:
  - name: your_name
    prompt: "Enter your name"
    private: no

  tasks:
  - name: print name
    assert: that="your_name.strip()"
    args:
      msg: "The your_name argument shouldn't be empty!"

  - debug:
        var: your_name

Check more about prompts, vars_prompt here: https://docs.ansible.com/ansible/latest/user_guide/playbooks_prompts.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.