ansible:根据ansible_os_family设置variables

使用安全的2.0.2.0,我想部署一个文件到目标服务器。 Debian和RedHat系列中的目标文件夹不同。

我已经使用了set_fact,但似乎它使用了最后定义的,忽略了when:选项。

我不想使用variables文件,因为这个variables只用在这个特定的剧本中

将复制任务复制到RedHat和Debian中,尽pipe可能,但将来会使维护复杂化。

当对Ubuntu服务器执行时,下面的剧本将失败,因为目的地被扩展成为/etc/nrpe.d,这是RedHat

- set_fact: destination: "/etc/nagios/nrpe.d/" when: ansible_os_family == "Debian" - set_fact: destination: "/etc/nrpe.d/" when: ansible_os_family == "RedHat" - name: Ensure Nagios custom checks directory exists file: path=/usr/local/lib/nagios/plugins state=directory mode=0755 - name: Install check_cpu_steal copy: src=eprepo/sysadmin/nagios_checks/check_cpu_steal dest=/usr/local/lib/nagios/plugins/check_cpu_steal mode=0755 owner=root group=root - name: Install check_cpu_steal command to /etc/nrpe.d copy: src=eprepo/sysadmin/files/check_cpu_steal.conf dest="{{ destination }}/check_cpu_steal.conf mode=0644 owner=root group=root" 

我已经解决了我自己的问题。

本质上,你可以根据os_family设置variables,但是你必须正确地做。

看到我下面的固定剧本:

 --- - name: Set fact for Debian set_fact: destination: "/etc/nagios/nrpe.d/" nrpe_server: "nagios-nrpe-server" when: ansible_os_family == "Debian" - name: Set fact for RedHat set_fact: destination: "/etc/nrpe.d/" nrpe_server: "nrpe" when: ansible_os_family == "RedHat" - name: Ensure Nagios custom checks directory exists file: path=/usr/local/lib/nagios/plugins state=directory mode=0755 - name: Install check_cpu_steal nagios check copy: src=eprepo/sysadmin/nagios_checks/check_cpu_steal dest=/usr/local/lib/nagios/plugins/check_cpu_steal mode=0755 owner=root group=root - name: Install check_cpu_steal nrpe config copy: src=eprepo/sysadmin/files/check_cpu_steal.conf dest="{{ destination }}/check_cpu_steal.cfg" mode=0644 owner=root group=root - name: Restart nrpe daemon service: name={{ nrpe_server }} state=restarted