Getting error while running ansible playbook

While executing below code, getting error in ansible
{{ ec2_info.instances |
selectattr(‘state.name’, ‘equalto’, ‘running’) |
selectattr(‘launch_time’, ‘lt’, (current_time_seconds - uptime_threshold_seconds) | int) |
list }}

Below is the errror i am getting

fatal: [localhost]: FAILED! => {“msg”: "template error while templating string: expected token ‘end of print statement’, got ‘list’. String: {{ ec2_info.instances |\n selectattr(‘state.name’, ‘equalto’, ‘running’) |\n selectattr(‘launch_time’, ‘lt’, (current_time_seconds - uptime_threshold


Can anyone help as i am new to ansible

Could you provide the results from:

- name: Debug ec2_info.instances
  ansible.builtin.debug:
    var: ec2_info.instances

That would help people here solve your issue.

Due to security constraint, i can’t post entire output.
Below is for launchtime…let me know if you are looking for specific details
“launch_time”: “2024-06-01T06:47:48+00:00”,
“maintenance_options”: {
“auto_recovery”: “default”

In what context are you using this Jinja2 expression? By which I mean something like

  loop: "{{ … }}"

or is it in an ansible.builtin.set_fact task, etc.?


This may not be related to the particular problem you asked about, but

selectattr(‘launch_time’, ‘lt’, (current_time_seconds - uptime_threshold_seconds) | int)

doesn’t seem right to me. You’re taking an integer number of seconds, and comparing it the a string that looks like %Y-%m-%dT%H:%M:%S%z. That isn’t going to work. You’ll need to convert the calculated time in seconds into the same format as the launch_time attribute, like this:

selectattr(‘launch_time’, ‘lt’, '%Y-%m-%dT%H:%M:%S%z' | strftime((current_time_seconds - uptime_threshold_seconds) | int)) |

with the caveat that I was not able to get the time zone to play nicely with that of the launch_time attribute, which is UTC. I’m not sure of the correct solution to that issue, but I was about to consider subtracting 4 hours worth of seconds from the difference (my local time being -04:00 relative to UTC).

1 Like

Thanks a lot for your help. I tried as per your suggestion , But still getting same error
Below is the code modified line as per your inputs. But no luck

  • name: Ensure uptime_threshold_days is an integer
    set_fact:
    uptime_threshold_seconds: “{{ (uptime_threshold_days | int) * 86400 }}”

    • name: Print current time
      debug:
      msg:
      currentime: “{{current_time_seconds}}”

    • name: Print uptime_threshold_days
      debug:
      msg:
      uptime: “{{uptime_threshold_seconds}}”

    • name: Filter instances based on launch time
      set_fact:
      recent_instances: >-
      {{ ec2_info.instances |
      selectattr(‘state.name’, ‘equalto’, ‘running’) |
      selectattr(‘launch_time’, ‘lt’, ‘%Y-%m-%dT%H:%M:%S%z’ | strftime((current_time_seconds - uptime_threshold_seconds) | int)) |
      list }}


Error i am getting
fatal: [localhost]: FAILED! => {“msg”: “Unexpected templating type error occurred on ({{ ec2_info.instances |\n selectattr(‘state.name’, ‘equalto’, ‘running’) |\n selectattr(‘launch_time’, ‘lt’, ‘%Y-%m-%dT%H:%M:%S%z’ | strftime((current_time_seconds - uptime_threshold_seconds) | int)) |\n list }}): unsupported operand type(s) for -: ‘AnsibleUnsafeText’ and ‘str’”}

No, that’s a much different error: “unsupported operand type(s) for -: ‘AnsibleUnsafeText’ and ‘str’”.

At least one of the operands is of type AnsibleUnsafeText. If the problem text is the output of the strftime() function, then you can explicitly re-cast it as str:

strftime() | str

Although, if that’s the problem, then one or both of current_time_seconds and uptime_threshold_seconds must be one of Ansible’s “Unsafe” types, too.

You can see the type of various variables by using the type_debug filter in a debug step:

- name: Show var types
  ansible.builtin.debug:
    msg:
      - "current_time_seconds is a {{ current_time_seconds | type_debug }}."
      - "uptime_threshold_seconds is a {{ uptime_threshold_seconds | type_debug }}."

If the problem datum is in your ec2_info.instances, um, I don’t know how you’d fix that.

In general, if you’re trying to use a datum in a Jinja2 expression, and it’s of one of Ansible’s “Unsafe” types, you have to explicitly cast it wherever you use it.


By the way, you can make your posts to this Forum much easier to read by enclosing your code blocks in “tripple-ticks”, possibly with a type hint, like this:

```yaml
- name: Show var types
  ansible.builtin.debug:
    msg:
       - "current_time_seconds is a {{ current_time_seconds | type_debug }}."
       - "uptime_threshold_seconds is a {{ uptime_threshold_seconds | type_debug }}."
```    

Now you know. You can be a good citizen and help “future you” and others by editing your prior posts to include the opening and closing “tripple-ticks” before and after your code blocks. Note: they should be on lines all by themselves.

Good luck!

2 Likes