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

查看:518
本文介绍了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天全站免登陆