在ansible中使用动态键名设置事实 [英] Set fact with dynamic key name in ansible

查看:22
本文介绍了在ansible中使用动态键名设置事实的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试缩小几段类似的代码,如下所示:

I am trying to shrink several chunks of similar code which looks like:

- ... multiple things is going here
  register: list_register
- name: Generating list
  set_fact: my_list="{{ list_register.results | map(attribute='ansible_facts.list_item') | list }}"

# the same code repeats...

实际上,它们之间唯一的区别是我在这里使用了不同的列表名称而不是my_list

In fact, the only difference between them is that I am using different list names here instead of my_list

事实上我想这样做:

set_fact:
  "{{ some var }}" : "{{ some value }}"

我遇到了这个发布但在这里没有找到任何答案.

I came across this post but didn't find any answer here.

是否可以这样做或是否有任何解决方法?

推荐答案

看看这个示例剧本:

---
- hosts: localhost
  vars:
    iter:
      - key: abc
        val: xyz
      - key: efg
        val: uvw
  tasks:
    - set_fact: {"{{ item.key }}":"{{ item.val }}"}
      with_items: "{{iter}}"
    - debug: msg="key={{item.key}}, hostvar={{hostvars['localhost'][item.key]}}"
      with_items: "{{iter}}"

这篇关于在ansible中使用动态键名设置事实的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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