gpt4 book ai didi

Ansible 标准输出行结果不带括号和引号

转载 作者:行者123 更新时间:2023-12-02 18:13:45 25 4
gpt4 key购买 nike

如何从不带括号和引号的剧本 stdout_lines 返回?

我读到的任何有关此问题的信息都不起作用,

这是我的代码:

  - name: Tested unit information
command: cat "{{ location }}/info.txt"
register: sys_info
- debug:
msg: "{{ sys_info.stdout_lines }}"

这是我的输出:

    "msg": [
"Serial Number: Test12345",
"Technician Name: Team",
"Custom Serial Number: Test12345",
"System Part Number: xxxx",
"Rev: A10",
"Last Rev: A10",
"Paka Number: Auto",
"Ip Address: 1.1.1.1"
]
}

这就是我想要得到的:

msg:
Serial Number: Test12345
Technician Name: Team
Custom Serial Number: Test12345
System Part Number: xxxx
Rev: A10
Last Rev: A10
Paka Number: Auto
Ip Address: 1.1.1.1

如果您提供有关如何执行此操作的信息或建议,我将不胜感激

最佳答案

简短回答

使用stdout而不是stdout_lines并使用yaml回调而不是默认。然后

    - debug:
msg: "{{ sys_info.stdout }}"

给你想要的东西

  msg: |-
Serial Number: Test12345
Technician Name: Team
Custom Serial Number: Test12345
System Part Number: xxxx
Rev: A10
Last Rev: A10
Paka Number: Auto
Ip Address: 1.1.1.1

详细信息


给定文件

shell> cat info.txt
Serial Number: Test12345
Technician Name: Team
Custom Serial Number: Test12345
System Part Number: xxxx
Rev: A10
Last Rev: A10
Paka Number: Auto
Ip Address: 1.1.1.1

和剧本

shell> cat test.yml
- hosts: localhost
tasks:
- name: Tested unit information
command: cat info.txt
register: sys_info
- debug:
msg: "{{ sys_info.stdout_lines }}"

您所看到的内容取决于 DEFAULT_STDOUT_CALLBACK默认回调格式为 JSON

shell> ANSIBLE_STDOUT_CALLBACK=default ansible-playbook test.yml

TASK [debug] ***************************************************************
ok: [localhost] => {
"msg": [
"Serial Number: Test12345",
"Technician Name: Team",
"Custom Serial Number: Test12345",
"System Part Number: xxxx",
"Rev: A10",
"Last Rev: A10",
"Paka Number: Auto",
"Ip Address: 1.1.1.1"
]
}

如果您想从列表中创建纯文本,请使用 Jinja ,例如

    - debug:
msg: |-
{% for line in sys_info.stdout_lines %}
{{ line }}
{% endfor %}

,或者简单地加入

    - debug:
msg: "{{ sys_info.stdout_lines|join('\n') }}"

两个选项给出相同的结果

shell> ANSIBLE_STDOUT_CALLBACK=default ansible-playbook test.yml

TASK [debug] ***************************************************************
ok: [localhost] => {
"msg": "Serial Number: Test12345\nTechnician Name: Team\nCustom Serial Number: Test12345\nSystem Part Number: xxxx\nRev: A10\nLast Rev: A10\nPaka Number: Auto\nIp Address: 1.1.1.1\n"
}

使用回调yaml来获取你想要的

shell> ANSIBLE_STDOUT_CALLBACK=yaml ansible-playbook test.yml

TASK [debug] ***************************************************************
ok: [localhost] =>
msg: |-
Serial Number: Test12345
Technician Name: Team
Custom Serial Number: Test12345
System Part Number: xxxx
Rev: A10
Last Rev: A10
Paka Number: Auto
Ip Address: 1.1.1.1

关于Ansible 标准输出行结果不带括号和引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71921814/

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