# using with\_sequence with a block

**URL:** <https://forum.ansible.com/t/using-with-sequence-with-a-block/38399>\
**Category:** Ansible Project\
**Created:** [September 5, 2023, 12:22pm UTC](https://forum.ansible.com/t/using-with-sequence-with-a-block/38399 "2023-09-05T12:22:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kathy\_L](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kathy_l/32/2652_2.png) [@Kathy\_L](https://forum.ansible.com/u/Kathy_L)\
**Post date:** [September 5, 2023, 12:22pm UTC](https://forum.ansible.com/t/using-with-sequence-with-a-block/38399/1 "2023-09-05T12:22:11Z")

</div>

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 }}”

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [September 5, 2023, 12:37pm UTC](https://forum.ansible.com/t/using-with-sequence-with-a-block/38399/2 "2023-09-05T12:37:20Z")

</div>

You have to apply it to each task. Blocks have almost no options.

Walter

---

<div class="post-metadata">

**Author:** ![Kathy\_L](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kathy_l/32/2652_2.png) [@Kathy\_L](https://forum.ansible.com/u/Kathy_L)\
**Post date:** [September 5, 2023, 1:38pm UTC](https://forum.ansible.com/t/using-with-sequence-with-a-block/38399/3 "2023-09-05T13:38:46Z")

</div>

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.

- name: Add users

user:

name: “{{ username }}”

password: {{ “passwd” }}

shell: /bin/bash

---

<div class="post-metadata">

**Author:** ![Richard\_Megginson](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/richard_megginson/32/4540_2.png) [@Richard\_Megginson](https://forum.ansible.com/u/Richard_Megginson)\
**Post date:** [September 5, 2023, 1:44pm UTC](https://forum.ansible.com/t/using-with-sequence-with-a-block/38399/4 "2023-09-05T13:44:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kathy\_L](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kathy_l/32/2652_2.png) [@Kathy\_L](https://forum.ansible.com/u/Kathy_L)\
**Post date:** [September 5, 2023, 1:52pm UTC](https://forum.ansible.com/t/using-with-sequence-with-a-block/38399/5 "2023-09-05T13:52:49Z")

</div>

I’ll try that. When I use with\_sequence, does it put the usernames and passwords in an array I can refer to?

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [September 5, 2023, 1:53pm UTC](https://forum.ansible.com/t/using-with-sequence-with-a-block/38399/6 "2023-09-05T13:53:09Z")

</div>

Make sure you read thoroughly the module document for each task so you don’t miss any parameters.

Walter

---

<div class="post-metadata">

**Author:** ![Kathy\_L](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kathy_l/32/2652_2.png) [@Kathy\_L](https://forum.ansible.com/u/Kathy_L)\
**Post date:** [September 5, 2023, 2:59pm UTC](https://forum.ansible.com/t/using-with-sequence-with-a-block/38399/7 "2023-09-05T14:59:04Z")

</div>

That worked out well, thank you!
