我正在 ansible 中运行一个 playbook,它采用 shell 模块输出的 stdout_lines 并为我提供以下格式:
StackNames.stdout_lines:
- "["
- ' "SHSD-CZWV-ami-automation-WIN2012R2-NONPROD-ramirja-119",'
- ' "SHSD-CZWV-ami-automation-WIN2012R2-NONPROD-ramirja-118",'
- ' "SHSD-CZWV-ami-automation-WIN2012R2-NONPROD-ramirja-117"'
- "]"
我正在尝试获取可在循环中使用的这些值的列表,以便可以删除堆栈,但首先我需要删除所有引号、dbl 引号和空格,以便正确格式化cloudformation ansible 模块接收 stack_name 参数值。
我尝试设置一个具有正确格式的新变量,例如:
- set_fact:
stack_list: "{{ StackNames.stdout_lines | replace('"','') |trim }}"
但到目前为止我还没有运气。
非常感谢任何帮助
最佳答案
从您的示例中,您的 shell 命令返回 json 可解析结果。只需使用 from_json
filter 解析变量内的全局输出 (stdout
)你就完成了:
- set_fact:
stack_list: "{{ StackNames.stdout | from_json }}"
- name: Make sure the above is working with your specific output
debug:
var: item
loop: "{{ stack_list }}"
关于regex - 使用 regex_replace 删除 ansible playbook 中的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61696920/