Ansible剧本,用于检查操作系统和给定的挂载点

时间:2020-09-15 08:22:35

标签: 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

1 个答案:

答案 0 :(得分: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指定为fail is it's own task