Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using validation with ansible's expect variables

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?

like image 920
turkus Avatar asked Jan 05 '23 11:01

turkus


1 Answers

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
like image 101
Konstantin Suvorov Avatar answered Jan 14 '23 13:01

Konstantin Suvorov