ansible - 用于检查操作系统和给定挂载点的 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
解决方案
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
在单个任务中指定多个,因为失败是它自己的任务。
推荐阅读
- javascript - 打字稿 | 关于缺少函数返回类型 ESLint 的警告
- apache-spark - 为什么没有 collect() 对累加器的操作不起作用?
- javascript - Knockout.Js 中的多个 afterAdd 调用
- javascript - 如何从从 nodejs 服务器获取的缓冲区数据中显示视频
- git - 仅将 TFS 存储库的一部分迁移到 Azure DevOps 上的 Git
- python - 当并非所有行都包含结束日期时,对数据框中的开始日期和结束日期进行数学运算
- java - 在 Java 中使用 SecureRandom
- amazon-web-services - 通过 CLI 删除 S3 存储桶中的空文件夹
- firebase - 服务器端迁移到 FCM,但客户端将在 2019 年 4 月 11 日之后将 GCM 与 InstanceID.getToken() 一起使用
- excel - Excel 查找和替换无法按预期工作