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).
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
---
- name: Getting Some User Input
hosts: localhost
#Prompt Starts
vars_prompt:
- name: "username"
prompt: "Enter The User Name: "
private: no
#Prompt ends
tasks:
- name: Print 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.---
- name: Creating some prompt
hosts: localhost
vars_prompt:
- name: "password"
prompt: "Enter The Password: "
#Set value to yes to hide password.
private: no
#This is encryption algorithm
encrypt: sha512_crypt
#Confirm attribute reconfirm the entered password
confirm: yes
salt_size: 7
tasks:
- name: Print 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'"
ansible-playbook playbookName.yml -e "username='John_bhai' variable_name2='andy'"
Example: Compare the below output with playbook 1.