gpt4 book ai didi

ansible - 如何在 Ansible 中将多行字符串转换为 dict?

转载 作者:行者123 更新时间:2023-12-01 23:37:53 26 4
gpt4 key购买 nike

我使用环境变量在 Ansible 中设置了一个事实 query('env', 'VARIABLE')
我的 VARIABLE是多行字符串(YAML 格式):

device: eth0
bootproto: static
address: 192.168.x.x
netmask: 255.255.255.0
gateway: 192.168.x.x

当我打印 VARIABLE使用 Ansible,我将其作为单个字符串使用 \n两行之间
"msg": ["device: eth0\nbootproto: static\naddress: 
192.168.x.x\nnetmask: 255.255.255.0\ngateway: 192.168.x.x"]

有没有方便的方法将其转换为dict?我需要稍后在我的任务中使用它,在配置机器的 NIC 时加载参数。

我曾尝试使用 Jinja2 过滤器 - debug: msg="{{ network_settings | from_yaml }}"没有成功。

最佳答案

an important note in the docs :

The difference between lookup and query is largely that query will always return a list.



所以:
  • 要么替换 query('env', 'VARIABLE')lookup('env', 'VARIABLE') :
    - debug:
    msg: "{{ lookup('env', 'VARIABLE') | from_yaml }}"
  • 或相应地处理列表(内容将在第一个也是唯一的元素中):
    - debug:
    msg: "{{ query('env', 'VARIABLE') | first | from_yaml }}"
  • 关于ansible - 如何在 Ansible 中将多行字符串转换为 dict?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50217227/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com