Home>

Create a new group x and users a and b on the target server (192.168.100.251) 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)
  

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

/tmp/group_vars/users.yml
  

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

/tmp/setup.yml
  

-hosts: 192.168.100.251
  remote_user: root
  become: true
  roles:
  '-setup
  vars_files:
  '-group_vars/user_groups.yml
  '-group_vars/users.yml

/tmp/roles/useradd/task/create_user_group.yml
  

'-hosts: 192.168.100.251
  remote_user: root
  become: true
  tasks:
  '-name: create group
  group:
  name: "{{item.name}}"
  gid: "{{item.gid}}"
  state: present
  with_items:
  '-"{{user_groups}}"

  

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

Execution result

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

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

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

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

PLAY RECAP****************************************************** ************************************************** ********************************
192.168.100.251: 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.