0

I have a list of dictionaries and for all items in this list, I want to convert one key-value pair epoch time to datetime but I'm getting an error

"JMESPathError in json_query filter plugin:\nUnknown function: strftime()"

Here is the code:


- name: Update list of dicts
  hosts: localhost
  gather_facts: false
  vars:
    var1: [ { "name": "abc", "id": "23213123", "epochTime": 1687505936 }, { "name": "def", "id": "9c5219430000005c", "epochTime": 1687505950 } ]

  tasks:

  - name: Convert epoch time to datetime for list of dictionaries 
    set_fact:
      list_dict_human_readable: "{{ var1 | json_query('[].{name: name, id: id, epochTime: epochTime | int | \"%Y-%m-%d %H:%M:%S\" | strftime(ansible_date_time.epoch) }') }}"

  - debug:
      msg: "{{ list_dict_human_readable }}"


Expected result:

list_dict_human_readable: [ { "name": "abc", "id": "23213123", "epochTime": "2023-06-23 07:38:56"}, { "name": "def", "id": "9c5219430000005c", "epochTime": "2023-06-23 07:39:10"} ]

Ansible version is 2.9

Any idea? Thanks.

LJS
  • 317
  • 1
  • 9

1 Answers1

1

Create the update

  var1_update: |
    {% filter from_yaml %}
    {% for i in var1 %}
    - {epochTime: "{{ '%Y-%m-%d %H:%M:%S'|strftime(i.epochTime) }}"}
    {% endfor %}
    {% endfilter %}

gives

  var1_update:
  - epochTime: '2023-06-23 09:38:56'
  - epochTime: '2023-06-23 09:39:10'

Update var1

    - set_fact:
        var1: "{{ var1|zip(var1_update)|map('combine') }}"

gives what you want

  var1:
  - epochTime: '2023-06-23 09:38:56'
    id: '23213123'
    name: abc
  - epochTime: '2023-06-23 09:39:10'
    id: 9c5219430000005c
    name: def

Example of a complete playbook for testing

- hosts: localhost

  vars:

    var1:
      - epochTime: 1687505936
        id: '23213123'
        name: abc
      - epochTime: 1687505950
        id: 9c5219430000005c
        name: def

    var1_update: |
      {% filter from_yaml %}
      {% for i in var1 %}
      - {epochTime: "{{ '%Y-%m-%d %H:%M:%S'|strftime(i.epochTime) }}"}
      {% endfor %}
      {% endfilter %}
        
  tasks:

    - debug:
        var: var1_update

    - set_fact:
        var1: "{{ var1|zip(var1_update)|map('combine') }}"
    - debug:
        var: var1
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
  • I've got this: sequence item 0: expected str instance, list found – LJS Jun 23 '23 at 12:28
  • I can't find any *item* in my answer. – Vladimir Botka Jun 23 '23 at 13:51
  • strange, because I literally copy/pasted your "Example of a complete playbook for testing" and got that error. – LJS Jun 23 '23 at 14:02
  • This is really strange. – Vladimir Botka Jun 23 '23 at 14:02
  • BTW I've got the same error on both Ansible Tower and on my locally installed Ansible... I first thought I did something wrong, so that is why I wanted to test it again with you example playbook (as is) on my local Ansible and got the same. AT is Ansible 2.9.13 and the local Ansible is 2.9.6. – LJS Jun 23 '23 at 14:07
  • 2.9 is [EOL](https://docs.ansible.com/ansible/latest/reference_appendices/release_and_maintenance.html#ansible-core-support-matrix) over a year. – Vladimir Botka Jun 23 '23 at 14:09
  • Company is using it, and I'm not the one in charge :( But we should migrate to I believe 2.12 or something like that in next few months. Until then, can this be done with selectattr or maybe different kind of loop? – LJS Jun 23 '23 at 14:11
  • Yes, you should migrate. – Vladimir Botka Jun 23 '23 at 14:12