Failed logic with when statement

18 views
Skip to first unread message

John Harmon

unread,
Apr 17, 2018, 4:21:35 PM4/17/18
to Ansible Project
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



John Harmon

unread,
Apr 17, 2018, 4:41:36 PM4/17/18
to Ansible Project
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

John Harmon

unread,
Apr 17, 2018, 4:45:10 PM4/17/18
to Ansible Project
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

John Harmon

unread,
Apr 17, 2018, 4:46:55 PM4/17/18
to Ansible Project
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

John Harmon

unread,
Apr 17, 2018, 5:00:45 PM4/17/18
to Ansible Project
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
Reply all
Reply to author
Forward
0 new messages