0

I want to maintain users with ansible. I use user module but I want to have some non-unique user ids. Now I have a dictionary like this:

users:
    user: { password: "HASH", shell: "/bin/zsh", home: "/home/user", uid: -1, non-unique: "no" }
    userroot: { password: "HASH1", shell: "/bin/zsh", home: "/home/userroot", uid: 0, non-unique: "yes" }

I'm creating users like that:

- name: Create users
  user: name={{ item.key }} createhome=yes shell={{ item.value.shell }} password={{ item.value.password }} home="{{ item.value.home }}" append=yes state=present 
  with_dict: "{{users}}"

Question is how to add a logic that will specify uid argument when it is set to value greater or equal than 0?

1

I think the only way how to decide this by greater than/equal uid number is to have two different resources with conditional when.

But if you don't need to have uid: -1, something like this can work. Just get rid of uid and non_unique values in user variables:

- name: Create users
    user:
      name={{ item.key }}
      createhome=yes
      shell={{ item.value.shell }}
      password={{ item.value.password }}
      home="{{ item.value.home }}"
      append=yes
      state=present
      uid="{{ item.value.uid | default(omit) }}"
      non_unique="{{ item.value.non_unique | default('no') }}" 
    with_dict: "{{users}}"

And than not use uid and non_unique (pay attantion to this variable, there have to be _ instead of -) in common users:

user: { password: "HASH", shell: "/bin/zsh", home: "/home/user" }
userroot: { password: "HASH1", shell: "/bin/zsh", home: "/home/userroot", uid: 0, non_unique: "yes" }
  • Thanks! filter default(omit) is exactly what I was looking for! More info here. – Kalavan Nov 10 '16 at 13:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.