user1601716
user1601716

Reputation: 1993

how to have a task run when 'value' not in ansible fact

I have not been able to find how do a when statement a fact does not match.

This is what i have so far:

Here is the task:

- name: Set Percentage value to "yes" to all attached disks, for non-'nfsutil' servers
  shell: |
    NUM=$(cat -n {{ file_path }} |\
          sed -n "/<disk>/,/<\/disk>/p" |\
          sed -n "/<alarm>/,/<\/alarm>/p" |\
          sed -n "/<fixed>/,/<\/fixed>/p" |\
          sed -n "/<{{ item }}>/,/<\/{{ item }}>/p" |\
          awk '/ percent = no/ {printf "%d", $1}')"s";
    if [ "${NUM}" != "s" ]; then
        sed -i "${NUM}/no/yes/g" {{ file_path }};
        echo "file_was_changed" ;
    fi
  with_items: "{{ nfsfs.stdout_lines }}"
  register: threshold_yes
  when: '"nfsutil01" not in ansible_hostname or "nfsutil02" not in ansible_hostname'
  changed_when: '"file_was_changed" in threshold_yes.stdout'

Where the setup module for both servers shows:

"ansible_hostname": "nfsutil01"
"ansible_hostname": "teachphp01"

The goal is to perform this activity, using the same inventory list on only for 'teachphp01.my-domain.net' in the below case.

TASK [Set Percentage value to "yes" to all attached disks, for  non-'nfsutil' servers]    
***************************************************************************    
***************************************************************************
ok: [nfsutil01.my-domain.net] => (item=#)
ok: [teachphp01.my-domain.net] => (item=#)
ok: [teachphp01.my-domain.net] => (item=#tmp)
ok: [nfsutil01.my-domain.net] => (item=#tmp)
ok: [teachphp01.my-domain.net] => (item=#boot)
ok: [nfsutil01.my-domain.net] => (item=#boot)

When I test this however it runs on both servers instead of skipping nfsutil01 and running on teachphp01.

Upvotes: 0

Views: 1661

Answers (2)

user140547
user140547

Reputation: 8200

Well I guess this is a problem with the boolean expression, not with ansible.

"nfsutil01" not in ansible_hostname or "nfsutil02" not in ansible_hostname'

evaluates to true because nfsutil02 is not in"nfsutil01"...

maybe you want to do

when: '"nfsutil01" not in ansible_hostname and "nfsutil02" not in ansible_hostname'

BTW, using in in that way means you are searching for substrings, which may not be what you want.

Upvotes: 0

Zlemini
Zlemini

Reputation: 4973

The "if not in list" idiom should solve this:

when: ansible_hostname not in ["nfsutil01", "nfsutil02"]

Upvotes: 1

Related Questions