Hi,
Am writing a logic to get the list of servers running more than n number of day’s. I tried to store the result in a set_fact variable but it’s not storing the values properly mean it including the skipped values in the set_facts. How to fix this?
- name: Compare the times
set_fact:
temp: “{{ ec2.instances | map(attribute=‘instance_id’) | list }}”
when: ((ansible_date_time.iso8601[:19] | to_datetime(fmt) - item.launch_time[:19] | to_datetime(fmt)).days >= 1)
with_items: “{{ ec2.instances }}”
vars:
fmt: “%Y-%m-%dT%H:%M:%S”
register: timestamp1
loop_control:
label: “{{ item.instance_id }}”
Below is the ouput:
`TASK [Compare the times] *********************************************************************************************************************************************************************************************************************
skipping: [localhost] => (item=Prod-scoring-ami-machine)
ok: [localhost] => (item=stage-scoring-202007241050087142)
ok: [localhost] => (item=stage-scoring-202007241051329817)
ok: [localhost] => (item=stage-scoring-202007241129464655)
ok: [localhost] => (item=stage-scoring-202007241123295962)
ok: [localhost] => (item=stage-scoring-202008261826362494)
skipping: [localhost] => (item=prod-scoring-202008282138514763)
skipping: [localhost] => (item=prod-scoring-202008282300303548)
TASK [Debug the output] **********************************************************************************************************************************************************************************************************************
ok: [localhost] => {
“msg”: [
“Prod-scoring-ami-machine”,
“stage-scoring-202007241050087142”,
“stage-scoring-202007241051329817”,
“stage-scoring-202007241129464655”,
“stage-scoring-202007241123295962”,
“stage-scoring-202008261826362494”,
“prod-scoring-202008282138514763”,
“prod-scoring-202008282300303548”
]
}
Expected output:
TASK [Compare the times] *********************************************************************************************************************************************************************************************************************
skipping: [localhost] => (item=Prod-scoring-ami-machine)
ok: [localhost] => (item=stage-scoring-202007241050087142)
ok: [localhost] => (item=stage-scoring-202007241051329817)
ok: [localhost] => (item=stage-scoring-202007241129464655)
ok: [localhost] => (item=stage-scoring-202007241123295962)
ok: [localhost] => (item=stage-scoring-202008261826362494)
skipping: [localhost] => (item=prod-scoring-202008282138514763)
skipping: [localhost] => (item=prod-scoring-202008282300303548)
TASK [Debug the output] **********************************************************************************************************************************************************************************************************************
ok: [localhost] => {
“msg”: [
“stage-scoring-202007241050087142”,
“stage-scoring-202007241051329817”,
“stage-scoring-202007241129464655”,
“stage-scoring-202007241123295962”,
“stage-scoring-202008261826362494”,
]
}
The above code returns all the values rather than condition met values. i mean it includes the skipped values. How to get only matched or task is ok values.
Any ideas much appreciated.
Thanks !