I'm using ansible to automate project deploy on machines. In one case I'm using ansible's expect
option.
I have in my:
# initdb.yml
- hosts: all
vars_prompt:
- name: "username"
prompt: "Username"
private: no
- name: "email"
prompt: "Email"
private: no
- name: "password"
prompt: "Password"
private: yes
roles:
- initdb
and in:
# roles/.../main.yml
- expect:
echo: yes
command: 'env/bin/python manage.py createsuperuser'
timeout: 5
responses:
'Username .*': '{{ username }}'
'Email .*': '{{ email }}'
'Password': '{{ password }}'
'Password (again)': '{{ password }}'
args:
chdir: '{{ repo.path }}'
tags:
- superuser
It's for creating a superuser after applying django init migrations.
Now let's go to the point:
When I run this and somebody will type in for example too short password, whole process hangs up and I have to run it again using tags
option.
Question:
Is there a way to attach validation to ansible's expect that it will just prompt in console that it's incorrect value and force user to type in correct value?
No, expect
module can't do validation for you.
You should avoid any interactivity in your playbooks.
If you need to validate parameters, do it manually at the very beginning of your playbook with assert
:
- hosts: all
vars_prompt:
- name: "password"
prompt: "Password"
private: yes
pre_tasks:
- assert:
that: password | length > 6
roles:
- initdb
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With