简体   繁体   English

ansible - 遍历事实并处理每组值

[英]ansible - loop through fact and process each set of values

I have set a fact我已经设定了一个事实

set_fact:
  props: "{{ parse_result.stdout | from_json }}"

The fact looks something like this:事实看起来是这样的:

{
    "changed": false,
    "ansible_facts": {
        "props": [
            {
                "build_number": "1.0.0.2",
                "build_name": "AppXYZ"
            },
            {
                "build_number": "1.2.0.2",
                "build_name": "AppABC"
            }
        ]
    },
    "_ansible_no_log": false
}

I want to loop through the fact and process each set of build_name and build_number.我想遍历事实并处理每组 build_name 和 build_number。 I have tried the below code, but sometimes it would throw me an error like 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'build_definition_name'.我已经尝试了下面的代码,但有时它会给我一个错误,比如“ansible.utils.unsafe_proxy.AnsibleUnsafeText 对象”没有属性“build_definition_name”。 What am I doing wrong?我究竟做错了什么?

my_deploy_module:
  build_name: "{{ item.build_name }}"
  build_number: "{{ item.build_number }}"
with_items: "{{ props }}"

I came to find answer to my own question in this post:我在这篇文章中找到了我自己问题的答案:

https://serverfault.com/questions/927855/ansible-loop-over-custom-facts https://serverfault.com/questions/927855/ansible-loop-over-custom-facts

Here is the syntax:这是语法:

my_deploy_module:
  build_name: "{{ item.build_name }}"
  build_number: "{{ item.build_number }}"
with_items: "{{ props | json_query('[*]') | flatten }}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在ansible中循环遍历字典值 - How to loop through dictionary values in ansible Ansible:json对象上的set_fact - Ansible: set_fact on a json object Ansible - 注册变量,然后将变量搜索到 set_fact (Cisco Aci) - Ansible - Register variable and then search the variable to set_fact (Cisco Aci) 如何通过.each循环将json数据处理到html表中 - how to process json data through an .each loop into html table 遍历JSON响应-Ansible - Loop through JSON response - Ansible 提交表单转发器值作为数组并遍历每个 - submit form repeater values as array and loop through each 如何将 set_fact 设置的 json 变量传递给 ansible shell 任务中的 jq 管道? - How can I pass a json variable set by set_fact to jq pipeline in ansible shell task? 如何使用调试或设置事实模块在 ansible 中使用 Jinja2 获取 dict 键值格式 - How to get dict key value format using Jinja2 in ansible using debug or set fact module 如何使用 set_fact 打印数组变量以在 ansible add_host 模块中使用 - How to print array variable with set_fact to use in ansible add_host module 使用“ from_json”之类的过滤器从json文件中获取一个“ set_fact”存储库网址 - Ansible “set_fact” repository url from json file using filters like “from_json”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM