首页 > 解决方案 > 用于检查操作系统和给定挂载点的 Ansible 剧本

问题描述

下面是我检查以验证操作系统和挂载点的剧本。低于错误,无法调试。问题似乎出在后面的“验证挂载点”部分

---
 - 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'\n\nThe error appears to be in '/etc/ansible/test.yml': line 14, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: \"Verify Mount Points\"\n      ^ here\n"}
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'\n\nThe error appears to be in '/etc/ansible/test.yml': line 14, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: \"Verify Mount Points\"\n      ^ here\n"}

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首先,使用ansible定义变量时需要设置。

其次,试着把注意力集中在这里的主要问题上。这些方面的东西应该起作用:

---
 - 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在单个任务中指定多个,因为失败是它自己的任务


推荐阅读