【问题标题】:Using regex_replace to remove character in ansible playbook使用 regex_replace 删除 ansible playbook 中的字符
【发布时间】:2020-05-09 12:45:35
【问题描述】:

我在 ansible 中运行一个剧本,它采用 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 }}"

但到目前为止我还没有运气。

非常感谢任何帮助

【问题讨论】:

    标签: regex replace ansible amazon-cloudformation


    【解决方案1】:

    从您的示例中,您的 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 }}"
    

    【讨论】:

    • 我这解决了你的问题,你应该接受答案,以便其他人知道有一个可行的解决方案,并最终投票给答案,这是表达谢谢的方式。
    • 完成。感谢您解释该过程,我是新手。
    猜你喜欢
    • 1970-01-01
    • 2016-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-07
    • 1970-01-01
    • 1970-01-01
    • 2017-09-16
    相关资源
    最近更新 更多