Ansible 循环直到条件匹配. [英] Ansible loop till the condition matches.

查看:27
本文介绍了Ansible 循环直到条件匹配.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想进行一系列 API 调用,在每次调用后检查结果中的特定参数,如果它大于特定值,则将其保存在寄存器中并继续进一步执行剧本.

I want to make the series of API calls, after each call check for a particular parameter in the result, if it greater than particular value save it in register and continue further playbook execution.

基本上,我正在对 RHEV 进行 API 调用以检查存储域.然后我想检查存储域是否有足够的空间,如果是,则将该存储域ID存储在寄存器中以在存储域上创建磁盘.

Basically I am making an API call to RHEV to check the storage domain. Then I want to check if storage domain have the sufficient space, if yes, store that storage domain id in register to create the disk on the storage domain.

下面是我如何对存储域进行单个 API 调用的片段.

Below is the snippet how I do it for single API call to storagedomain.

 - name: Get Free Storage Domain On RHEV
  uri:
    url: "{{ rhevurl }}/storagedomains/7649aea2-d87c-4066-acca-4399d5261ade"
    method: GET
    user: "{{ rhevusername }}"
    password: "{{ rhevpassword }}"
    return_content: yes
    headers:
      Version: 4
      Accept: "application/xml"
      Content-type: "application/xml"
  register: storagedomain
  tags: storagedomain


- name: Retriving size.
  xml:
    xmlstring: "{{ storagedomain.content }}"
    xpath: /storage_domain/available
    content: text
  register: availablesize
  tags: storagedomain


- name: storage_domain size
  debug:
    var: availablesize.matches[0].available
  tags: storagedomain

现在我想对多个存储域执行此过程,当它获得具有可用空间的存储域时,循环应该会中断.

Now I want to do this process for multiple storage domain, and loop should break when it gets storagedomain with available space.

类似于下面的内容.

- name: Get Free Storage Domain On RHEV
  uri:
    url: "{{ rhevurl }}/storagedomains/{{ item }}"
    method: GET
    user: "{{ rhevusername }}"
    password: "{{ rhevpassword }}"
    return_content: yes
    headers:
      Version: 4
      Accept: "application/xml"
      Content-type: "application/xml"
  loop:
    - 7649aea2-d87c-4066-acca-4399d5261ade
    - 40cceee7-a8d3-45af-a2d0-70c414be32cc
    - a81411b0-4ddb-4467-a4c6-ac9364905248
    - b288c547-231c-44b9-8329-98adcbdfc726 
    - 8cdef991-3edc-4c35-9228-feeef8f29004
    - 837a2e1b-6365-4309-a526-0cd05801fe43
    - 8981bf82-a1da-405e-a7f5-d84f2c94d71d
    - 7a9e3904-e37b-48fd-b850-0f026dc5cde9

在循环中我应该如何使用 xml 模块解析 xml 然后检查可用空间的条件大于特定大小

In the loop how should I parse xml using xml module and then check for condition of available space greater the particular size

推荐答案

您不能中断循环,但如果您的条件满足任何一项,则可以跳过循环执行.看看下面的例子.

You cannot break the loop, but can skip the loop execution if your condition satisfy at any item. check out the below example.

test.yml 这个剧本将执行 shell 模块并忽略错误 &设置 var1 变量.但是 block 模块只会在 var1 没有定义之前执行.

test.yml this playbook will execute shell module with ignore errors & set var1 variable. but the block module will only execute until var1 is not defined.

- block:
  - shell: expr {{item}} + 1
    ignore_errors: yes
    register: cmd_stat

  - set_fact: var1={{item}}
    when: cmd_stat.rc == 0
  when: var1 is not defined

sites.yml 此游戏根据您的循环项目多次包含 test.yml 手册.

sites.yml this play includes test.yml playbook multiple times based on your loops items.

---
- hosts: localhost
  connection: local
  vars:
  tasks:
    - include: test.yml
      loop: ["abc","def", "ghi",1, "jkl"]

    - name: increase var1 variable by 1 and write to text file
      shell: expr {{var1}} + 1 > text

因此,使用您可以在剧本中实现的相同逻辑.就像如果 url 获得 200 状态然后设置 storage 变量 &在 when 条件下使用它.

So, using the same logic you can implement in your playbook. like if url get 200 status then set storage variable & use it with when condition.

我希望你能理解这个例子.

I Hope you'll be able to understand the example.

这篇关于Ansible 循环直到条件匹配.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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