Ansible - 使用 with_items 和条件时 [英] Ansible - using with_items and when conditional to

查看:29
本文介绍了Ansible - 使用 with_items 和条件时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆服务器,上面有四个物理驱动器(/dev/sda、sdb、sdc 和 sdd).sda 上安装了操作系统.

I have a bunch of servers that have four physical drives on them (/dev/sda, sdb, sdc, and sdd). sda has the OS installed on it.

我需要格式化除 sda 之外的每个驱动器.我需要检查每个驱动器上是否有数据.如果是这样,那么我不应该对其进行格式化.

I need to format each drive except sda. I need to check if each drive has data on it. If it does, then I shouldn't format it.

# This will get all physical disks (sda, sdb, sdc, etc) and assign them to disk_var
- name: Get disks
  set_fact: disk_var="{{hostvars[inventory_hostname]["ansible_devices"].keys()|list}}"

- name: Check if the disk is partitioned and also ignore sda
  stat: path=/dev/{{item}}1
  with_items: disk_var
  when: item != 'sda'
  register: base_secondary_partition_{{item}}

- name: Create GPT partition table
  command: /sbin/parted -s /dev/{{item}} mklabel gpt
  with_items: disk_var
  when: item != 'sda' and base_secondary_partition_{{item}}.stat.exists == false

格式化这些驱动器显然涉及更多步骤,但在创建 GPT 分区表时在最后一个任务中失败.

There's clearly more steps involved into formatting these drives but it fails at the last task when creating the GPT partition table.

这是它运行时的样子.你会看到它在最后一个任务中失败了:

Here's what it looks like when it runs. You'll see that it fails at the last task:

TASK: [role | Get disks] ******************************************************
ok: [server1.com]

TASK: [role | Check if the disk is partitioned] *******************************
skipping: [server1.com] => (item=sda)
ok: [server1.com] => (item=sdd)
ok: [server1.com] => (item=sdb)
ok: [server1.com] => (item=sdc)

TASK: [role | Create GPT partition table] *************************************
fatal: [server1.com] => error while evaluating conditional: base_secondary_partition_sdd.stat.exists == false

FATAL: all hosts have already failed -- aborting

知道如何检查条件 base_secondary_partition_{{item}}.stat.exists 吗?我需要确保如果驱动器上有数据,则不会对其进行格式化.

Any idea how I can check the conditional base_secondary_partition_{{item}}.stat.exists? I need to make sure that if there's data on the drive, it will not format it.

推荐答案

你不需要用 item salt 注册你的结果.当您注册循环的结果(例如 with_items)时,注册的值将包含一个键 results,其中包含循环所有结果的列表.(请参阅文档)

You do not need to register your result with the item salt. When you register the result of a loop (e.g. with_items) the registered value will contain a key results which holds a list of all results of the loop. (See docs)

您可以遍历第一个任务的注册结果,而不是遍历原始设备列表:

Instead of looping over your original device list, you can loop over the registered results of the first task then:

- name: Check if the disk is partitioned and also ignore sda
  stat: path=/dev/{{item}}1
  with_items: disk_var
  when: item != 'sda'
  register: device_stat

- name: Create GPT partition table
  command: /sbin/parted -s /dev/{{ item.item }} mklabel gpt
  with_items: "{{ device_stat.results }}"
  when:
    - not item | skipped
    - item.stat.exists == false

条件 not item |skipped 处理在原始循环 (sda) 中过滤的元素将不会被处理.

The condition not item | skipped takes care of that elements which have been filtered in the original loop (sda) will not be processed.

虽然这可能是您问题的解决方案,但您的问题非常有趣.Jinja2 中似乎没有 eval 功能.虽然您可以连接字符串,但不能使用该字符串作为变量名来获取其值...

While that might be a solution to your problem, your question is very interesting. There seems to be no eval feature in Jinja2. While you can concatenate strings you can not use that string as a variable name to get to its value...

这篇关于Ansible - 使用 with_items 和条件时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆