Create a new group x and users a and b on the target server ( using Ansible,
I want to attach a and b to x, but I get an error. The OS is CentOS7 for both Ansible operation terminal and target server.

/tmp/group_vars/user_groups.yml (* Please ignore the "'" before the hyphen)

  '-{name:' x ', gid: 100}


  '-{name:' a ', uid: 123, group:' x '}
  '-{name:' b ', uid: 124, group:' x '}


  remote_user: root
  become: true


  remote_user: root
  become: true
  '-name: create group
  name: "{{item.name}}"
  gid: "{{item.gid}}"
  state: present


    '-name: create user
  name: "{{item.name}}"
  uid: "{{item.uid}}"
  groups: "{{item.group}}"

Execution result

[root @ HOGE tmp] # ansible-playbook /tmp/roles/useradd/task/create_user_group.yml

PLAY []****************************************************** ************************************************** ********************  

TASK [Gathering Facts]************************************************** ************************************************** ********************
Enter passphrase for key '/root/.ssh/id_rsa':
ok: []

TASK [create group]************************************************** ************************************************** ***********************
fatal: []: FAILED! =>{"msg": "'user_groups' is undefined"}
to retry, use: --limit @/tmp/roles/useradd/task/create_user_group.retry

PLAY RECAP****************************************************** ************************************************** ******************************** ok = 1 changed = 0 unreachable = 0 failed = 1

I fixed it.
I would appreciate it if you could teach me. Thank you.

  • Answer # 1

    How do you load addgroup.yml and adduser.yml?

  • Answer # 2

    There may be a problem with the code indentation, so could you please correct the above code in Markdown format?

    The values ​​of groups in the user module must be an array, please check.