Failed logic with when statement

I have a variable ‘ntp_timezone’ set to UTC by default. I am trying to override it in certain situation, but this ALWAYS overrides it. I think something with my logic is wrong and could use another pair of eyes:
NOTE: ansible-rhel7 has a current hostname of ansible-rhel7ebs

`

  • debug:
    var: ntp_timezone
    verbosity: 2

  • name: Set timezone variable for non-UTC servers (Overriding UTC as default)
    set_fact:
    ntp_timezone: America/Denver
    when: item not in ansible_hostname | lower
    with_items:

  • agl

  • ascp

  • dmtr

  • ebs

  • vtx

  • debug:
    var: ntp_timezone
    verbosity: 2

`

Results:

`
TASK [ntp : debug] ***********************************************************************************************************************************************************************************************************************
task path: /etc/ansible/roles/ntp/tasks/main.yml:27
ok: [ansible-rhel7] => {
“ansible_hostname”: “ansible-rhel7ebs”
}

TASK [ntp : debug] ***********************************************************************************************************************************************************************************************************************
task path: /etc/ansible/roles/ntp/tasks/main.yml:27
ok: [ansible-rhel7] => {
“ntp_timezone”: “UTC”
}

TASK [ntp : Set timezone variable for non-UTC servers (Overriding UTC as default)] *******************************************************************************************************************************************************
task path: /etc/ansible/roles/ntp/tasks/main.yml:31
ok: [ansible-rhel7] => (item=agl) => {“ansible_facts”: {“ntp_timezone”: “America/Denver”}, “changed”: false, “item”: “agl”}
ok: [ansible-rhel7] => (item=ascp) => {“ansible_facts”: {“ntp_timezone”: “America/Denver”}, “changed”: false, “item”: “ascp”}
ok: [ansible-rhel7] => (item=dmtr) => {“ansible_facts”: {“ntp_timezone”: “America/Denver”}, “changed”: false, “item”: “dmtr”}
skipping: [ansible-rhel7] => (item=ebs) => {“changed”: false, “item”: “ebs”, “skip_reason”: “Conditional result was False”}
ok: [ansible-rhel7] => (item=vtx) => {“ansible_facts”: {“ntp_timezone”: “America/Denver”}, “changed”: false, “item”: “vtx”}

TASK [ntp : debug] ***********************************************************************************************************************************************************************************************************************
task path: /etc/ansible/roles/ntp/tasks/main.yml:42
ok: [ansible-rhel7] => {
“ntp_timezone”: “America/Denver”
}
META: ending play

`

Ah, I think I see my problem… it works, but then gets checked against vtx and gets set… I will have to try something else

I was wrong… I changed the order and it made no difference. I also have a similar test in another playbook that has always worked just fine… so still looking for info from anybody that can help me.

Reordered:

`
ask path: /etc/ansible/roles/ntp/tasks/main.yml:27
ok: [ansible-rhel7] => {
“ansible_hostname”: “ansible-rhel7ebs”
}

TASK [ntp : debug] ***********************************************************************************************************************************************************************************************************************
task path: /etc/ansible/roles/ntp/tasks/main.yml:30
ok: [ansible-rhel7] => {
“ntp_timezone”: “UTC”
}

TASK [ntp : Set timezone variable for non-UTC servers (Overriding UTC as default)] *******************************************************************************************************************************************************
task path: /etc/ansible/roles/ntp/tasks/main.yml:34
ok: [ansible-rhel7] => (item=agl) => {“ansible_facts”: {“ntp_timezone”: “America/Denver”}, “changed”: false, “item”: “agl”}
ok: [ansible-rhel7] => (item=ascp) => {“ansible_facts”: {“ntp_timezone”: “America/Denver”}, “changed”: false, “item”: “ascp”}
ok: [ansible-rhel7] => (item=dmtr) => {“ansible_facts”: {“ntp_timezone”: “America/Denver”}, “changed”: false, “item”: “dmtr”}
ok: [ansible-rhel7] => (item=vtx) => {“ansible_facts”: {“ntp_timezone”: “America/Denver”}, “changed”: false, “item”: “vtx”}
skipping: [ansible-rhel7] => (item=ebs) => {“changed”: false, “item”: “ebs”, “skip_reason”: “Conditional result was False”}

TASK [ntp : debug] ***********************************************************************************************************************************************************************************************************************
task path: /etc/ansible/roles/ntp/tasks/main.yml:45
ok: [ansible-rhel7] => {
“ntp_timezone”: “America/Denver”
}

`

With it as the only option it does work… so now I can see my flawed logic.

`
ok: [ansible-rhel7] => {
“ntp_timezone”: “UTC”
}

TASK [ntp : Set timezone variable for non-UTC servers (Overriding UTC as default)] *******************************************************************************************************************************************************
task path: /etc/ansible/roles/ntp/tasks/main.yml:34
skipping: [ansible-rhel7] => (item=ebs) => {“changed”: false, “item”: “ebs”, “skip_reason”: “Conditional result was False”}

TASK [ntp : debug] ***********************************************************************************************************************************************************************************************************************
task path: /etc/ansible/roles/ntp/tasks/main.yml:45
ok: [ansible-rhel7] => {
“ntp_timezone”: “UTC”
}

`

OK, Here are my working results should anybody else come looking and find this:

Play:

`

  • name: Set timezone variable for non-UTC servers (Overriding UTC as default)
    set_fact:
    ntp_timezone: America/Denver
    when: ansible_hostname|lower is not search (“agl|ascp|ebs|dmtr|vtx”)
    `

Result with ebs in name:

`
ok: [ansible-rhel7] => {
“ansible_hostname”: “ansible-rhel7ebs”
}

TASK [ntp : debug] ***********************************************************************************************************************************************************************************************************************
task path: /etc/ansible/roles/ntp/tasks/main.yml:30
ok: [ansible-rhel7] => {
“ntp_timezone”: “UTC”
}

TASK [ntp : Set timezone variable for non-UTC servers (Overriding UTC as default)] *******************************************************************************************************************************************************
task path: /etc/ansible/roles/ntp/tasks/main.yml:34
skipping: [ansible-rhel7] => {“changed”: false, “skip_reason”: “Conditional result was False”}

TASK [ntp : debug] ***********************************************************************************************************************************************************************************************************************
task path: /etc/ansible/roles/ntp/tasks/main.yml:39
ok: [ansible-rhel7] => {
“ntp_timezone”: “UTC”
}

`

Result WITHOUT ebs in name:

`
ok: [ansible-rhel7] => {
“ansible_hostname”: “ansible-rhel7”
}

TASK [ntp : debug] ***********************************************************************************************************************************************************************************************************************
task path: /etc/ansible/roles/ntp/tasks/main.yml:30
ok: [ansible-rhel7] => {
“ntp_timezone”: “UTC”
}

TASK [ntp : Set timezone variable for non-UTC servers (Overriding UTC as default)] *******************************************************************************************************************************************************
task path: /etc/ansible/roles/ntp/tasks/main.yml:34
ok: [ansible-rhel7] => {“ansible_facts”: {“ntp_timezone”: “America/Denver”}, “changed”: false}

TASK [ntp : debug] ***********************************************************************************************************************************************************************************************************************
task path: /etc/ansible/roles/ntp/tasks/main.yml:39
ok: [ansible-rhel7] => {
“ntp_timezone”: “America/Denver”
}

`

Hope it helps somebody