How to prompt in ansible Playbook | Ansible prompt

To get dynamic value during execution of playbook you need user's interaction... Attributes of prompt are... Pass prompt value using CLI....

What is prompt?

        It Takes user input during the execution of the playbook. To get dynamic value during the execution of the playbook you need user interaction. You will achieve it using the "vars_prompt" module. The second option is to give arguments using CLI. 

Official Page: Ansible Prompts

Insights: Try to avoid this to achieve a better user experience and speed.

Attributes of the prompt:

[Required Attributes]*
name: "variable_name"
prompt: "Enter The prompt text here"
private: yes/no

[Optional Attributes]
default: Some Default Value
encrypt: bcrypt / md5_crypt / sha256_crypt / sha512_crypt (These are some default encryption Algorithm to use other algorithm install PassLib library)
unsafe: yes/no (Unsafe attributes allow us to use special symbols(!,@,#,$,^,*,?,{,},|,etc) in playbook).
salt_size: number (Default salt size is 8).
confirm: yes/no (This ask you to re-enter the input and match both input automatically).

Now Let's Create Playbook

Playbook 1: Create a playbook with the required prompt attribute and display the input value.

Step 1: Create a file at /etc/ansible/play1.yml

---
nameGetting Some User Input
  hostslocalhost
#Prompt Starts
  vars_prompt:
    - name"username"
      prompt"Enter The User Name: "
      privateno
#Prompt ends
  tasks:
    - namePrint The username
      debug:
        msg"You have entered: {{ username  }}"


Output:


Playbook 2: Creating a playbook with user input in encrypted mode and recheck the entered value.

---
nameCreating some prompt
  hostslocalhost
  vars_prompt:
    - name"password"
      prompt"Enter The Password: "

      #Set value to yes to hide password.
      privateno

      #This is encryption algorithm
      encryptsha512_crypt

      #Confirm attribute reconfirm the entered password
      confirmyes
      salt_size7
  tasks:
    - namePrint The password
      debug:
        #As the password is encrypted you see only hash.
        msg"You have entered: {{ password  }}"


Output:


How to skip user prompt and pass prompt value using CLI.

Append below line during Execution of playbook

Syntax,
-e "variable_name='value' variable_name2='value'"

Example,
ansible-playbook playbookName.yml -e "username='John_bhai' variable_name2='andy'"


Example: Compare the below output with playbook 1.



Post a Comment