ANSIBLE_JINJA2_NATIVE not working in user module for expires key

61 views
Skip to first unread message

senorsmile

unread,
Jul 11, 2022, 3:07:34 PM7/11/22
to Ansible Project


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

```yaml
- 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'
```


senorsmile

unread,
Jul 11, 2022, 5:35:13 PM7/11/22
to Ansible Project
It looks like I had a typo as everything is now working.  
Reply all
Reply to author
Forward
0 new messages