首页 > 解决方案 > 不同设备名称上的 ansible 循环,以根据其他全局任务的结果在块中运行任务

问题描述

团队,我有一种情况,只有在满足块条件之外的两个任务时,我才想运行一个任务块,我在 block 下指定WHEN。但我收到错误,有什么提示吗?它在第一个任务本身上失败了apt-install parted

我希望为我的值文件中列出的每个设备运行任务,所以我使用循环。希望它是正确的?

值.yml


local_volume_mount_disks:
  - device: /dev/nvme2n1
    partitions:

  - device: /dev/nvme1n1
    partitions:

  - device: /dev/sdd
    partitions:

任务

 - name: Check device existence on node
  shell: "lsblk -p -l | grep {{ item.0.device }}"
  loop:  "{{ local_volume_mount_disks|subelements('partitions') }}"
  register: device_exists
  ignore_errors: yes

- name: Check if device is mounted on ROOT /
  shell: lsblk -l -p | grep -w "{{ item.0.device }}" | grep -w /
  loop:  "{{ local_volume_mount_disks|subelements('partitions') }}"
  register: is_device_root
  ignore_errors: yes

- name: local_volume_mount partition format and mount devices
  block:
  - name: apt-install parted
    apt:
      name: parted=3.2*
      update_cache: yes

  when:
    - local_volume_mount_enabled|bool
    - device_exists.rc == 0
    - is_device_root.rc != 0

输出

 TASK [local_volume_mount : Check device existence on node] *********************
....postgres'}])

 
TASK [local_volume_mount : Check if device is mounted on ROOT /] ***************
Thursday 15 April 2021  00:20:58 +0000 (0:00:05.007)       0:00:05.148 ******** 

"stderr_lines": [], "stdout": "", "stdout_lines": []}
  ...ignoring
  TASK [local_volume_mount : apt-install parted] *********************************
   Thursday 15 April 2021  00:21:02 +0000 (0:00:03.476)       0:00:08.625 ******** 
   fatal: [node1]: FAILED! => {"msg": "The conditional check 'device_exists.rc == 0' failed. The error was: error while evaluating conditional (device_exists.rc == 0): 'dict object' has no attribute 'rc'\n\nThe error appears to be in '/ansible-managed/jenkins-slave/slave0/workspace/team/run_ansible_playbook/k8s/baremetal/roles/local_volume_mount/tasks/main.yml': line 18, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n  block:\n  - name: apt-install parted\n    ^ here\n"}
   

标签: ansibleansible-inventoryansible-facts

解决方案


推荐阅读