Kathy_L
(Kathy L)
September 5, 2023, 12:22pm
1
We have a variable num_users_to add. I am trying to run a block of commands to generate users and passwords for the number of users the user has plugged in, using the with_sequence block. I am getting the error message “with_sequence is not a valid attribute for a Block.” How to I get around this?
Code:
name: Block to generate x number of users & passwords
block:
name: Generate user
set_fact:
username: “{{ lookup(‘ansible.builtin.password’, ‘/dev/null’, chars=[‘ascii_letters’, ‘digits’] length = 12 ) }}”
name: Generate password
set_fact:
passwd: "{{ lookup(‘ansible.builtin.password’, ‘/dev/null’, chars=[‘ascii_letters’, ‘digits’] length = 12 ) }}:
with_sequence: “count={{ num_users_to_add }}”
You have to apply it to each task. Blocks have almost no options.
Walter
Kathy_L
(Kathy L)
September 5, 2023, 1:38pm
3
Thanks. Any idea how I would then add these users to the system without using a block? I forgot to put this last part of the code I’d like to use in.
user:
name: “{{ username }}”
password: {{ “passwd” }}
shell: /bin/bash
You could put the tasks that you want to run in the loop in a separate tasks file, then use include_tasks with with_sequence
Kathy_L
(Kathy L)
September 5, 2023, 1:52pm
5
I’ll try that. When I use with_sequence, does it put the usernames and passwords in an array I can refer to?
Make sure you read thoroughly the module document for each task so you don’t miss any parameters.
Walter
Kathy_L
(Kathy L)
September 5, 2023, 2:59pm
7
That worked out well, thank you!