如何从ansible输出中删除前缀

huangapple go评论90阅读模式
英文:

How to remove prefix from ansible output

问题

- name: "重新索引 {{item}} 到本地服务器"
  ignore_errors: yes
  uri:
    url: "{{proto}}://{{new_server}}:{{es_api_port}}/_reindex?wait_for_completion=false"
    method: POST
    headers:
      Authorization: '基本认证=='
    body:
      source :
        index: "{{ item }}" 
      dest:
        index: "{{ item.split('reindex-') | to_nice_json}}"
    body_format: json
    with_items:
       - reindex-abc-def-ghi--2020-08-31
       - reindex-abc-def-ghi--2020-08-30

输出结果是:

"module_args": {
    "attributes": null,
    "body": {
        "dest": {
            "index": "abc-def-ghi-2020-08-31"
        },
        "source": {
            "index": "reindex-abc-def-ghi--2020-08-31"
        }
    },

所以,在输出中,dest 显示为 "abc-def-ghi-2020-08-31"

英文:

I have to parse a string in a ansible but when I try to use regex , it creates array.

here is the code

- name: "Reindexing {{item}} to local  server "
  ignore_errors: yes
  uri:
    url: "{{proto}}://{{new_server}}:{{es_api_port}}/_reindex?wait_for_completion=false"
    method: POST
    headers:
      Authorization: 'Basic auth=='
    body:
      source :
        index: "{{ item }}" 
      dest:
        index: "{{ item.split('reindex-') | to_nice_json}}" 
    body_format: json
    with_items:
       - reindex-abc-def-ghi--2020-08-31
       - reindex-abc-def-ghi--2020-08-30

output is:

        "module_args": {
        "attributes": null,
        "body": {
            "dest": {
                "index": "[\n    \"\",\n    \"abc-def-ghi-2020-08-31\"\n]"
            },
            "source": {
                "index": "reindex-abc-def-ghi--2020-08-31"
            }
        },

so, In output, dest is showing [\n "",\n "abc-def-ghi-2020-08-31"\n]. I am just looking for this "abc-def-ghi-2020-08-31"

Thanks!

答案1

得分: 1

In output, dest is showing ["", "abc-def-ghi-2020-08-31"].

That's because you're passing the output of item.split() to to_nice_json, which is for nicely formatting (e.g., with multiple lines and indentation) JSON documents. Why are you using that here?

The result of item.split('reindex-') is a two-element list, ["", "abc-def-ghi--2020-08-30"]. If you just want the second element, you can write:

dest:
  index: "{{ item.split('reindex').1 }}"
英文:

> In output, dest is showing [\n "",\n "abc-def-ghi-2020-08-31"\n].

That's because you're passing the output of item.split() to to_nice_json, which is for nicely formatting (e.g., with multiple lines and indentation) JSON documents. Why are you using that here?

The result of item.split('reindex-') is a two-element list, ["", "abc-def-ghi--2020-08-30"]. If you just want the second element, you can write:

dest:
  index: "{{ item.split('reindex').1 }}"

huangapple
  • 本文由 发表于 2023年8月4日 03:33:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76831142.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定