I have a playbook in which part of the variables is obtained by typing from the keyboard, in particular, I need to check for the maximum number of characters for the hostnamevar variable (no more than 15 characters).

I tried this design:


vars_prompt: - name: "hostnamevar" prompt: "Enter hostname (less than 15 characters)" failed_when: hostnamevar | length > 15 private: no 


I tried this:

 vars_prompt: - name: "hostnamevar" prompt: "Enter hostname (less than 15 characters)" private: no pre_tasks: - assert: that: - 'hostnamevar|length > 15' msg: "'hostnamevar' should be less or equal 15 symbols" 


I tried to leave it as tasks instead of pre_tasks. Absolutely no difference and no input restrictions. Dear, advise how to still implement such a test?

    1 answer 1

    The first example will not do anything, because prompts have no failed_when .

    The second example works correctly, but I suspect that you wanted to write either:

     - assert: that: hostnamevar|length < 15 

    or

     - fail: when: hostnamevar|length >= 15 

    In assert should be an "as necessary" condition, in fail should be an "as not necessary" condition.