# Getting error while running ansible playbook

**URL:** <https://forum.ansible.com/t/getting-error-while-running-ansible-playbook/6119>\
**Category:** Get Help\
**Created:** [June 1, 2024, 7:50am UTC](https://forum.ansible.com/t/getting-error-while-running-ansible-playbook/6119 "2024-06-01T07:50:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Manikandan\_Muthiah](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/manikandan_muthiah/32/3629_2.png) [@Manikandan\_Muthiah](https://forum.ansible.com/u/Manikandan_Muthiah)\
**Post date:** [June 1, 2024, 7:50am UTC](https://forum.ansible.com/t/getting-error-while-running-ansible-playbook/6119/1 "2024-06-01T07:50:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [June 1, 2024, 8:38am UTC](https://forum.ansible.com/t/getting-error-while-running-ansible-playbook/6119/2 "2024-06-01T08:38:57Z")

</div>

Could you provide the results from:

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

```

That would help people here solve your issue.

---

<div class="post-metadata">

**Author:** ![Manikandan\_Muthiah](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/manikandan_muthiah/32/3629_2.png) [@Manikandan\_Muthiah](https://forum.ansible.com/u/Manikandan_Muthiah)\
**Post date:** [June 1, 2024, 9:30am UTC](https://forum.ansible.com/t/getting-error-while-running-ansible-playbook/6119/3 "2024-06-01T09:30:23Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [June 1, 2024, 7:18pm UTC](https://forum.ansible.com/t/getting-error-while-running-ansible-playbook/6119/4 "2024-06-01T19:18:37Z")

</div>

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

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

```txt
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:

```txt
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).

---

<div class="post-metadata">

**Author:** ![Manikandan\_Muthiah](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/manikandan_muthiah/32/3629_2.png) [@Manikandan\_Muthiah](https://forum.ansible.com/u/Manikandan_Muthiah)\
**Post date:** [June 2, 2024, 5:35am UTC](https://forum.ansible.com/t/getting-error-while-running-ansible-playbook/6119/5 "2024-06-02T05:35:49Z")

</div>

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 }}”

* * *

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’”}

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [June 2, 2024, 12:16pm UTC](https://forum.ansible.com/t/getting-error-while-running-ansible-playbook/6119/6 "2024-06-02T12:16:48Z")

</div>

> [@Manikandan\_Muthiah](#):
>
> still getting same error

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`:

```yml
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:

```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 }}."

```

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!
