-1

I'm a total Ansible and Python beginner. I have a list:

my_items:
- name: item1
  unix_user: unixuser1
  homedir: /home/unixuser2
- name: item2
  unix_user: unixuser2
  homedir: /home/unixuser2

I would like to create a pureftpd virtual user for each of "my_items".

The role awaits a variable like this:

my_items:
- name: item1
  unix_user: unixuser1
  homedir: /home/unixuser2
  uid: 1001
  guid: 1001
- name: item2
  unix_user: unixuser2
  homedir: /home/unixuser2
  uid: 1002
  guid: 1002

I know how to retrieve the uid/guid (using getent). I can manage to add the new keys/values to each item:

- name: "Populate UID and GID in my_items"
  set_fact:
    item: "{{ item | combine( { 'uid': getent_passwd[item.unix_user][1], 'gid': getent_passwd[item.unix_user][2] }) }}"
  with_items: "{{ my_items }}"

But of course, the "global" my_items variable isn't updated.

I tried to create a new "dictionnary" but I can't get my head around so many concepts at the same time.

Any help would be appreciated!

1 Answer 1

0

I finally managed to make it work.

- name: "getent variables"
  getent:
    database: passwd
    
- name: Create Dictionary for FTP accounts
  set_fact:
    ftp_accounts: >
       {{ ftp_accounts | default([]) + [{
           'name': item.unix_user,
           'password': item.ftp_password,
           'homedir': item.path,
           'uid': getent_passwd[item.unix_user][1],
           'guid': getent_passwd[item.unix_user][2]
          } ] }}
  with_items:
    "{{ my_items }}"

Then, I can send my brand new dict to my role :

- name: gcoop-libre.pure-ftpd
  vars:
    pureftpd_virtual_users: "{{ ftp_accounts }}"

Be careful, I was working on localhost but the Unix users are (obviously) created only on the remote, so getent_passwd[item.unix_user] was undefined, leading to a cryptic error message (coercing to Unicode: need string or buffer, list found).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .