3

In ansible I am trying to get user input.

First set of input asks user to make a choice from the given input where as the other prompts for username and password.

Tasks

vars_prompt:
    - name: logging_endpoint_selected
      prompt : " Confirm the endpoint to forward logs.\n 
                    1. test1 \n
                    2. test2 \n
                 * Please input either 1 or 2 based on your selection "
      private: no

    - name: "Username"
      prompt:  " Splunk software must create an administrator account during startup. Otherwise, you cannot log in.\n Create credentials for the administrator account.\n
                Characters do not appear on the screen when you type in credentials\n
                Please enter an adminstrator username"
      private: no

    - name: "Password"
      prompt: " Password must contain atleast:\n * 8 total printable ASCII character(s).\n Please enter a new password"
      private: yes

    - name: "Confirm_Password"
      prompt: " Please confirm new password"
      private: yes

Output

 Confirm the endpoint to forward logs .
 1. Test1 
 2. Test2
 * Please input either 1 or 2 based on your selection : 1
 Splunk software must create an administrator account during startup. Otherwise, you cannot log in.
 Create credentials for the administrator account.
 Characters do not appear on the screen when you type in credentials
 Please enter an administrator username:

There is no new line between first set of prompt and second.

When we run the ansible script, I want the var prompt to be shown this way :

Confirm the endpoint to forward logs .
 1. Test1 
 2. Test2
 * Please input either 1 or 2 based on your selection : 1

You selected the option Test1 

 Splunk software must create an administrator account during startup. Otherwise, you cannot log in.
 Create credentials for the administrator account.
 Characters do not appear on the screen when you type in credentials
 Please enter an administrator username:

How can I do this? Immediately after user gives input, this must be shown: You selected the option Test1.

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
NVP
  • 103
  • 6

1 Answers1

1

You should use and abuse the YAML multi line syntax, for which I am not going to rewrite the already perfect question, here on StackOverflow, nor this super useful tool that shows you all the variant of it, in a more visual way.

Here is an example playbook where two of those syntaxes are fitted, and ending with an assert in a pre_tasks to confirm and validate the user choice:

- hosts: localhost
  gather_facts: no

  vars_prompt:
    - name: logging_endpoint_selected
      prompt : |-
        Confirm the endpoint to forward logs.
          1. test1
          2. test2
        * Please input either 1 or 2 based on your selection
      private: no

    - name: "Username"
      prompt: '

        Splunk software must create an administrator account 
        during startup.
        Otherwise, you cannot log in.

        Create credentials for the administrator account.

        Characters do not appear on the screen 
        when you type in credentials

        Please enter an administrator username'
      private: no

    - name: "Password"
      prompt: |-

        Password must contain at least:
          * 8 total printable ASCII character(s).
        Please enter a new password
      private: yes

    - name: "Confirm_Password"
      prompt: |-

        Please confirm new password
      private: yes

  pre_tasks:
    - assert:
        that: logging_endpoint_selected in ['1', '2']
        success_msg: >-
          You selected the option Test{{ logging_endpoint_selected }}

This playbook will run like this:

Confirm the endpoint to forward logs.
  1. test1
  2. test2
* Please input either 1 or 2 based on your selection: 1

Splunk software must create an administrator account during startup. Otherwise, you cannot log in.
Create credentials for the administrator account.
Characters do not appear on the screen when you type in credentials
Please enter an administrator username: 

Password must contain at least:
  * 8 total printable ASCII character(s).
Please enter a new password: 

Please confirm new password: 

PLAY [localhost] **************************************************************

TASK [assert] *****************************************************************
ok: [localhost] => changed=false 
  msg: You selected the option Test1  
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83