hazzy
hazzy

Reputation: 107

taking user input during execution-ansible

I am new in Ansible. I googled and read documentation for Ansible. But I am not able to find solution for my problem.

My issue -

In my Ansible playbook, I am running a command and as a result of that command the system asks me to input the password. I am not sure how can I put in the password in that promt. I can save the password as var but not sure how to input it in that promt.

Any link to the documentation will be highly appreciated.

Upvotes: 0

Views: 1274

Answers (2)

rcastellcastell
rcastellcastell

Reputation: 417

You should consider using the expect modulue. Here, I have an example

- name: Executing RCU
  expect:
    command: >
      "{{ oracle_common }}/bin/rcu -silent -createRepository -connectString {{ connection_string }} 
       -dbUser {{ db_user  }} -dbRole {{ db_role }} -schemaPrefix {{ schema_prefix }} 
       -useSamePasswordForAllSchemaUsers {{ same_password|lower }} {{ list_components }}"
    responses:
      'Enter the database password': "{{ db_password }}"
      'Enter the schema password': "{{ schema_password }}"
    timeout: 1200 # Expect timeout by default is 30s. Here, I am using 20 minutes as RCU is slow
  no_log: yes
  register: rcu_output
  # RCU-6016 means the schemas with given prefix are in the database
  failed_when: "'ERROR - RCU' in rcu_output.stdout and 'CAUSE - RCU-6016' not in rcu_output.stdout"
  changed_when: false

In the previous example the command executed is asking for the database password, which is passed by using expect. What is more, it uses no_log: yes to avoid printing the password.

The documentation about expect module is available here

The example I used is available here

You should have pexpect on your machine to use expect.

Upvotes: 0

lxop
lxop

Reputation: 8595

Rather than messing about with expect, you can usually just use the stdin argument to the command or shell module:

- name: Do a thing
  command:
    cmd: myapp --flags --and --things
    stdin: "{{ the_password }}"

- name: Do a thing
  shell:
    cmd: myapp --flags --and --things
    stdin: "{{ the_password }}"

That should work for a lot of applications, but some will read from a controlling tty in preference to stdin (if they have a tty). For these, you can run them under setsid which prevents them from having a controlling tty, so they read from stdin instead:

- name: Do a thing
  command:
    cmd: setsid myapp --flags --and --things
    stdin: "{{ the_password }}"

(This is assuming you're running under Linux, I don't know the appropriate thing to use for Windows/Mac)

Upvotes: 1

Related Questions