下面是我检查操作系统和装载点的操作手册。获取以下错误,无法调试它。问题似乎与后面的部分有关"验证安装点";
---
- hosts: all
gather_facts: true
vars:
M1: ['RedHat','7']
M2: ['/u01','/u02']
tasks:
- name: "Verify OS"
when: >
not ((ansible_os_family == M1[0] and ansible_distribution_major_version == M1[1]))
fail:
msg: "OS not supported"
- name: "Verify Mount Points"
with_items: ansible_mounts
when: >
not ((item.mount == M2[0] and item.mount == M2[1]))
fail:
msg: "Mount Point doesn't exist"
输出:ansible playbook-i dbservers test.yml
PLAY [all] **********************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************
ok: [192.168.0.107]
ok: [192.168.0.109]
TASK [Verify OS] ****************************************************************************************************************************************
skipping: [192.168.0.109]
skipping: [192.168.0.107]
TASK [Verify Mount Points] ******************************************************************************************************************************
fatal: [192.168.0.109]: FAILED! => {"msg": "The conditional check 'not ((item.mount == M2[0] and item.mount == M2[1]))n' failed. The error was: error while evaluating conditional (not ((item.mount == M2[0] and item.mount == M2[1]))n): 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'mount'nnThe error appears to be in '/etc/ansible/test.yml': line 14, column 7, but maynbe elsewhere in the file depending on the exact syntax problem.nnThe offending line appears to be:nnn - name: "Verify Mount Points"n ^ heren"}
fatal: [192.168.0.107]: FAILED! => {"msg": "The conditional check 'not ((item.mount == M2[0] and item.mount == M2[1]))n' failed. The error was: error while evaluating conditional (not ((item.mount == M2[0] and item.mount == M2[1]))n): 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'mount'nnThe error appears to be in '/etc/ansible/test.yml': line 14, column 7, but maynbe elsewhere in the file depending on the exact syntax problem.nnThe offending line appears to be:nnn - name: "Verify Mount Points"n ^ heren"}
PLAY RECAP **********************************************************************************************************************************************
192.168.0.107 : ok=1 changed=0 unreachable=0 failed=1 skipped=1 rescued=0 ignored=0
192.168.0.109 : ok=1 changed=0 unreachable=0 failed=1 skipped=1 rescued=0 ignored=0
首先,使用ansible定义的变量时需要设置gather_facts: true
。
第二,试着集中讨论这里的主要问题。沿着这些路线的一些东西应该起作用:
---
- hosts: all
gather_facts: true
vars:
M1: ['RedHat','7']
M2: ['/u01','/u02']
tasks:
- name: "Verifying os and mount points"
debug:
msg: "{{ item }}"
loop:
- ansible_os_family
- ansible_distribution_major_version
- ansible_mounts
when:
- not item.ansible_os_family == 'M1[0]' # you could also replace 'M1[0]' with 'Redhat'.
- item.ansible_distribution_major_version == 'M1[1]'. # you could also replace 'M1[1]' with '7'.
- fail:
msg: "mount points not correctly"
loop: ansible_mounts
when:
- item.mount is defined
- item.mount != 'M2[0]'
- item.mount == 'M2[1]'
您不应该在一个任务中指定多个fails
,因为失败是它自己的任务。