I’m trying to pass a generic date to the expires key in the user module and convert it inline so that our inventory doesn’t have to include this same jinja over and over:
users:
name: asmith
comment: Adam Smith
key_exclusive: true
key: |
abcdefghijklmnopqrstuvwxyz
create_users:
- >
{{ users['asmith'] | combine(
{'expires':("2022-07-12 00:00:00" | to_datetime).strftime("%s")
,'state':'present'
})
}}
My try thus far is
- name: Create/Remove users
user:
name: "{{ item.name }}"
update_password: "{{ item.update_password | default('always') }}"
password: "{{ item.password | default(omit) }}"
state: "{{ item.state | default('present') }}"
remove: "{{ item.remove | default('no') }}"
shell: "{{ item.shell | default('/bin/bash') }}"
comment: "{{ item.comment | default(omit) }}"
# account expires on whole day only
expires: "{% if 'expires' in item %}{{ ((item.expires + ' 00:00:00') | to_datetime).strftime('%s') | int }}{% else %}-1{% endif %}"
group: "{{ item.name }}"
groups: "{{ item.groups | default(omit) }}"
append: "{{ item.append | default('no') }}" # "no" = always remove from unspecified groups
loop: "{{ create_users }}"
loop_control:
label: "{{ item.name }}"
when:
- create_users is defined
I call ansible-playbook with env var ANSIBLE_JINJA2_NATIVE=True.
However, I get the error:
failed: [node1] (item=asmith) => changed=false
ansible_loop_var: item
item:
comment: Adam Smith
groups:
- admin
key: |-
abcdefghijklmnopqrstuvwxyz
name: asmith
msg: 'argument expires is of type <type ''str''> and we were unable to convert to float: <type ''str''> cannot be converted to a float'