Reputation: 595
I created an Ansible playbook script, to run it have need user interaction. Now I need to check if the user insert correctly the value when I prompt it.
vars_prompt:
- name: "vm_env"
prompt: "vm_env (values: PRD, TST, DEV)"
default: "DEV"
private: false
How can I check if the user insert correctly only one of these values (PRD, TST, DEV), and in case stop script?
Thanks for the support Marco
Upvotes: 0
Views: 2076
Reputation: 67984
Use pause if you want to give a user the chance to improve. However, there is no default.
- hosts: localhost
gather_facts: false
tasks:
- pause:
prompt: "vm_env (values: PRD, TST, DEV)"
register: result
until: result.user_input|default('') in ['PRD', 'TST', 'DEV']
retries: 3
delay: 0
- set_fact:
vm_env: "{{ result.user_input }}"
gives
PLAY [localhost] ****
TASK [pause] ****
[pause]
vm_env (values: PRD, TST, DEV):
ok: [localhost]
TASK [set_fact] ****
ok: [localhost]
TASK [debug] ****
ok: [localhost] => {
"vm_env": "PRD"
}
PLAY RECAP ****
localhost: ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Notes
- pause:
prompt: "vm_env (values: yes, no)"
register: result
- debug:
msg: |-
result.user_input: {{ result.user_input }}
result.user_input|type_debug: {{ result.user_input|type_debug }}
- set_fact:
vm_env: "{{ result.user_input }}"
- debug:
msg: |-
vm_env: {{ vm_env }}
vm_env|type_debug: {{ vm_env|type_debug }}
gives the output below for 'yes'
TASK [pause] ********************************************************
[pause]
vm_env (values: yes, no):
yes^Mok: [localhost]
TASK [debug] ********************************************************
ok: [localhost] =>
msg: |-
result.user_input: yes
result.user_input|type_debug: AnsibleUnsafeText
TASK [set_fact] *****************************************************
ok: [localhost]
TASK [debug] ********************************************************
ok: [localhost] =>
msg: |-
vm_env: True
vm_env|type_debug: bool
and similar output for 'no'
TASK [pause] ********************************************************
[pause]
vm_env (values: yes, no):
no^Mok: [localhost]
TASK [debug] ********************************************************
ok: [localhost] =>
msg: |-
result.user_input: no
result.user_input|type_debug: AnsibleUnsafeText
TASK [set_fact] *****************************************************
ok: [localhost]
TASK [debug] ********************************************************
ok: [localhost] =>
msg: |-
vm_env: False
vm_env|type_debug: bool
- pause:
prompt: "vm_env (values: yes, no)"
register: result
until: result.user_input|default('') in ['yes', 'no']
retries: 3
delay: 0
- debug:
msg: "User entered: {{ result.user_input|bool|ternary('yes', 'no') }}"
See:
Upvotes: 1
Reputation: 7340
There is an Ansible assert module to validate things and fail with appropriate error message if condition is not matched.
Example:
vars_prompt:
- name: "vm_env"
prompt: "vm_env (values: PRD, TST, DEV)"
default: "DEV"
private: false
tasks:
# "|lower" filter used to fix any case inconsistency, not required if case should match
- assert:
that:
- vm_env|lower in [ 'prd', 'tst', 'dev' ]
fail_msg: "VM environment should be one of: PRD, TST, DEV"
Upvotes: 1