using with_sequence with a block

68 views
Skip to first unread message

Kathy L

unread,
Sep 5, 2023, 8:22:11 AM9/5/23
to Ansible Project
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 }}"

Rowe, Walter P. (Fed)

unread,
Sep 5, 2023, 8:37:33 AM9/5/23
to ansible...@googlegroups.com
You have to apply it to each task. Blocks have almost no options.

Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/1c72e363-9a1c-4e3b-9768-5fae67d7b5een%40googlegroups.com.

Kathy Lyons

unread,
Sep 5, 2023, 9:39:08 AM9/5/23
to ansible...@googlegroups.com
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
  


You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/Cy006u7szBo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/3C3CB63B-6456-4720-9C58-DC5DBDD4FF51%40nist.gov.

Richard Megginson

unread,
Sep 5, 2023, 9:44:41 AM9/5/23
to ansible...@googlegroups.com
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

Message has been deleted

Kathy L

unread,
Sep 5, 2023, 9:52:50 AM9/5/23
to Ansible Project
I'll try that. When I use with_sequence, does it put the usernames and passwords in an array I can refer to?

Rowe, Walter P. (Fed)

unread,
Sep 5, 2023, 9:53:27 AM9/5/23
to 'Rowe, Walter P. (Fed)' via Ansible Project
Make sure you read thoroughly the module document for each task so you don't miss any parameters.


Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

On Sep 5, 2023, at 9:47 AM, Kathy L <lyon...@gmail.com> wrote:

ok, will do that.  Thanks!

On Tuesday, September 5, 2023 at 9:44:41 AM UTC-4 Richard Megginson wrote:

Kathy Lyons

unread,
Sep 5, 2023, 10:59:28 AM9/5/23
to ansible...@googlegroups.com
That worked out well, thank you!

Reply all
Reply to author
Forward
0 new messages