当'value'不在ansible事实中时,如何运行任务

时间:2017-06-30 18:48:49

标签: ansible

我一直无法找到事实陈述如何与之匹配。

这是我到目前为止所做的:

这是任务:

- 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'

两台服务器的安装模块显示:

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

目标是执行此活动,在以下情况下仅针对'teachphp01.my-domain.net'使用相同的库存清单。

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)

当我测试它时,它会在两台服务器上运行,而不是跳过nfsutil01并在teachphp01上运行。

2 个答案:

答案 0 :(得分:1)

“if not in list”成语应该解决这个问题:

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

答案 1 :(得分:0)

嗯,我想这是布尔表达式的问题,而不是ansible。

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

评估为true,因为nfsutil02不在"nfsutil01" ...

也许你想做

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

顺便说一句,以这种方式使用in意味着您正在搜索子字符串,这可能不是您想要的。

相关问题