I'm trying to add users to a Windows AD through Ansible and for that I need to prompt the user for the new users' information (username, password and AD groups).
This is my current playbook but this is prompting an error beacuse apparently I cant use vars_prompt inside a task. So what I want to know is: How can I do loop where the user inputs new information each iteration.
Thanks
vars_prompt:
- name: num_loops
prompt: Enter number of users you want to add
private: no
tasks:
- name: create local Users
loop: "{{ range(1, num_loops|int + 1) | list }}"
vars_prompt:
- name: wu_group
prompt: "Enter the user's group"
private: no
- name: wu_username
prompt: "Enter the username for user {{ item }}"
private: no
- name: wu_password
prompt: "Enter the password for user {{ item }}"
private: yes
ansible.windows.win_user:
name: "{{ wu_username }}"
password: "{{ wu_password }}"
groups: "{{ wu_group }}"
update_password: on_create
password_expired: true
Get the users' names and passwords in the loop
- pause:
prompt: "Enter group"
register: out_group
- pause:
prompt: "{{ item }}. Enter user,password"
register: out_users
loop: "{{ range(1, num_loops|int + 1) | list }}"
Declare the variables
wu_group: "{{ out_group.user_input }}"
wu_users: "{{ out_users.results|map(attribute='user_input')|
map('split', ',') }}
gives, for example
wu_group: my_group
wu_users:
- [alice, '123']
- [bob, '456']
- [carol, '789']
Example of the task (not tested)
- ansible.windows.win_user:
name: "{{ item.0 }}"
password: "{{ item.1 }}"
groups: "{{ wu_group }}"
update_password: on_create
password_expired: true
loop: "{{ wu_users }}"
Example of a complete playbook for testing
shell> cat pb.yml
- hosts: localhost
vars_prompt:
- name: num_loops
prompt: Enter number of users you want to add
private: no
vars:
wu_group: "{{ out_group.user_input }}"
wu_users: "{{ out_users.results|map(attribute='user_input')|
map('split', ',') }}"
tasks:
- pause:
prompt: "Enter group"
register: out_group
- pause:
prompt: "{{ item }}. Enter user,password"
register: out_users
loop: "{{ range(1, num_loops|int + 1) | list }}"
- debug:
var: wu_group
- debug:
var: wu_users|to_yaml
- debug:
msg: |
{% filter from_yaml|to_yaml %}
group: {{ wu_group }}
users:
{% for i in wu_users %}
- {name: {{ i.0 }}, pswd: {{ i.1 }}}
{% endfor %}
{% endfilter %}
gives
shell> ansible-playbook pb.yml
Enter number of users you want to add: 3
PLAY [localhost] ******************************************************************************
TASK [pause] **********************************************************************************
[pause]
Enter group:
my_group^Mok: [localhost]
TASK [pause] **********************************************************************************
[pause]
1. Enter user,password:
alice,123^Mok: [localhost] => (item=1)
[pause]
2. Enter user,password:
bob,456^Mok: [localhost] => (item=2)
[pause]
3. Enter user,password:
carol,789^Mok: [localhost] => (item=3)
TASK [debug] **********************************************************************************
ok: [localhost] =>
wu_group: my_group
TASK [debug] **********************************************************************************
ok: [localhost] =>
wu_users|to_yaml: |-
- [alice, '123']
- [bob, '456']
- [carol, '789']
TASK [debug] **********************************************************************************
ok: [localhost] =>
msg: |-
group: my_group
users:
- {name: alice, pswd: 123}
- {name: bob, pswd: 456}
- {name: carol, pswd: 789}
PLAY RECAP ************************************************************************************
localhost: ok=5 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0