vars_prompt in Ansible

Hi All,

I want to create Ansible script which will create user on linux servers, so I use vars_propt for entering values of username, groupname, shell. For groupname variable I want to use username’s value as a default value, but when I wrote default: {{username}} in my script I got syntax error, (I tried to write default: “{{username}}” , default: ‘{{username}}’ but in this cases script doesn’t understand that this is variable.

  • hosts: servers
    vars_prompt:

  • name: “username”
    prompt: “Enter new username”
    private: no

  • name: “groupname”
    prompt: “Please enter group name for user”
    private: no
    default: {{username}}

  • name: “usershell”
    prompt: “Please enter shell name for user”
    private: no
    default: “/bin/bash”

Hi,

I'm not getting a syntax error with

default: "{{ username }}"

It works (the default value is assigned if no input),
but the default value is not templated in the prompt.
This is printed instead:

Please enter group name for user: [{{ username }}]:

I believe it's related to this issue:
https://github.com/ansible/ansible/issues/16675

Cheers,
Marko
CONFIDENTIALITY NOTICE: This message is the property of International Game Technology PLC and/or its subsidiaries and may contain proprietary, confidential or trade secret information. This message is intended solely for the use of the addressee. If you are not the intended recipient and have received this message in error, please delete this message from your system. Any unauthorized reading, distribution, copying, or other use of this message or its attachments is strictly prohibited.

Hi Marko,

When script printed "Please enter group name for user: [{{ username }}]:" I thought that it understood {{ username }} as a string, but after your reply I tried to debug groupname value it works thank you, but now I have other issue I want to use {{ username }} in my group's prompt msg, when I wrote:

- name: "groupname"
       prompt: "Please enter group name for user {{ username }}"
       private: no
       default: "{{ username }}"

msg of promt is:

Please enter group name for user {{ username }} [{{ username }}]:

I want to see typed user name instead of {{ username }}

You don't have hostvars available at that point, you can use extra_vars though.

If you want to default to the user currently running Ansible, you can do this:

default: "{{lookup('env', 'USER')}}"