I want to create 3 Users for each of the 40 VMs using Ansible. VM IP will be entered in inventory.
looks like
VM1 : User1, User2, User3 VM2 : User4, User5, User6 VM3 : User7, User8, User9 .....
- hosts: win10
tasks:
- name: Create new htuser01
win_user:
name: htuser01
password: Demopass123
groups:
- Users
- Remote Desktop Users
update_password: always
password_never_expires: yes
- name: Create new htuser02
win_user:
name: htuser02
password: Demopass123
groups:
- Users
- Remote Desktop Users
update_password: always
password_never_expires: yes
- name: Create new htuser03
win_user:
name: htuser03
password: Demopass123
groups:
- Users
- Remote Desktop Users
update_password: always
password_never_expires: yes
The code above causes all VMs to create the same user name. Is there a way to create 3 different users for each VM?
CodePudding user response:
References to read:
Given the following test_inventory.yml
---
win10:
hosts:
vm1:
system_users:
- user1
- user2
- user3
vm2:
system_users:
- user4
- user5
- user6
vm3:
system_users:
- user7
- user8
- user9
the following test_playbook.yml
---
- name: inventory and loop demo
hosts: win10
gather_facts: false
tasks:
- name: Fake creating a user
vars:
demo_msg:
- I would play a task to create a win user with the following parameters
- "win_user:"
- " name: '{{ item }}'"
- " password: Demopass123"
- " groups:"
- " - Users"
- " - Desktop Users"
- " update_password: always"
- " password_never_expires: yes"
debug:
msg: "{{ demo_msg }}"
loop: "{{ system_users }}"
gives
$ ansible-playbook -i test_inventory.yml test_playbook.yml
PLAY [inventory and loop demo] *********************************************************************************************************************************************************************************************************
TASK [Fake creating a user] ************************************************************************************************************************************************************************************************************
ok: [vm1] => (item=user1) => {
"msg": [
"I would play a task to create a win user with the following parameters",
"win_user:",
" name: 'user1'",
" password: Demopass123",
" groups:",
" - Users",
" - Desktop Users",
" update_password: always",
" password_never_expires: yes"
]
}
ok: [vm2] => (item=user4) => {
"msg": [
"I would play a task to create a win user with the following parameters",
"win_user:",
" name: 'user4'",
" password: Demopass123",
" groups:",
" - Users",
" - Desktop Users",
" update_password: always",
" password_never_expires: yes"
]
}
ok: [vm1] => (item=user2) => {
"msg": [
"I would play a task to create a win user with the following parameters",
"win_user:",
" name: 'user2'",
" password: Demopass123",
" groups:",
" - Users",
" - Desktop Users",
" update_password: always",
" password_never_expires: yes"
]
}
ok: [vm2] => (item=user5) => {
"msg": [
"I would play a task to create a win user with the following parameters",
"win_user:",
" name: 'user5'",
" password: Demopass123",
" groups:",
" - Users",
" - Desktop Users",
" update_password: always",
" password_never_expires: yes"
]
}
ok: [vm3] => (item=user7) => {
"msg": [
"I would play a task to create a win user with the following parameters",
"win_user:",
" name: 'user7'",
" password: Demopass123",
" groups:",
" - Users",
" - Desktop Users",
" update_password: always",
" password_never_expires: yes"
]
}
ok: [vm1] => (item=user3) => {
"msg": [
"I would play a task to create a win user with the following parameters",
"win_user:",
" name: 'user3'",
" password: Demopass123",
" groups:",
" - Users",
" - Desktop Users",
" update_password: always",
" password_never_expires: yes"
]
}
ok: [vm2] => (item=user6) => {
"msg": [
"I would play a task to create a win user with the following parameters",
"win_user:",
" name: 'user6'",
" password: Demopass123",
" groups:",
" - Users",
" - Desktop Users",
" update_password: always",
" password_never_expires: yes"
]
}
ok: [vm3] => (item=user8) => {
"msg": [
"I would play a task to create a win user with the following parameters",
"win_user:",
" name: 'user8'",
" password: Demopass123",
" groups:",
" - Users",
" - Desktop Users",
" update_password: always",
" password_never_expires: yes"
]
}
ok: [vm3] => (item=user9) => {
"msg": [
"I would play a task to create a win user with the following parameters",
"win_user:",
" name: 'user9'",
" password: Demopass123",
" groups:",
" - Users",
" - Desktop Users",
" update_password: always",
" password_never_expires: yes"
]
}
PLAY RECAP *****************************************************************************************************************************************************************************************************************************
vm1 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
vm2 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
vm3 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0