如何让Ansible多行使用>忽略行中的空格?(How to let Ansible multiline with > ignore spaces in the lines?)

编程入门 行业动态 更新时间:2024-10-25 04:20:18
如何让Ansible多行使用>忽略行中的空格?(How to let Ansible multiline with > ignore spaces in the lines?)

要在行线命令中进行缩进,希望将命令的后续行中的空格分成多行。

- name: "Update kubeconfig to set cluster" shell: > kubectl config set-cluster {{ item.cluster }} --kubeconfig=~{{ item.account }}/.kube/config --server={{ K8S_MASTER_HOSTNAME }}:{{ K8S_API_SERCURE_PORT }} --embed-certs=true --certificate-authority={{ K8S_CA_HOME }}/ca.crt with_items: "{{ k8s_users }}"

有没有办法允许这个? 它会导致错误,并将每一行视为一个单独的命令。

"stderr": "/bin/sh: line 1: --kubeconfig=~centos/.kube/config: No such file or directory\n/bin/sh: line 2: --server=ip-172-31-4-117.us-west-1.compute.internal:6443: command not found\n/bin/sh: line 3: --embed-certs=true: command not found\n/bin/sh: line 4: --certificate-authority=/etc/kubernetes/pki/ca.crt: No such file or directory", "stderr_lines": [ "/bin/sh: line 1: --kubeconfig=~centos/.kube/config: No such file or directory", "/bin/sh: line 2: --server=ip-172-31-4-117.us-west-1.compute.internal:6443: command not found", "/bin/sh: line 3: --embed-certs=true: command not found", "/bin/sh: line 4: --certificate-authority=/etc/kubernetes/pki/ca.crt: No such file or directory" ],

Question

To make indentations in a line line command, would like to put spaces at the subsequent lines of a command split into multiple lines.

- name: "Update kubeconfig to set cluster" shell: > kubectl config set-cluster {{ item.cluster }} --kubeconfig=~{{ item.account }}/.kube/config --server={{ K8S_MASTER_HOSTNAME }}:{{ K8S_API_SERCURE_PORT }} --embed-certs=true --certificate-authority={{ K8S_CA_HOME }}/ca.crt with_items: "{{ k8s_users }}"

Is there a way to allow this? It causes an error and regard each line as a separate command.

"stderr": "/bin/sh: line 1: --kubeconfig=~centos/.kube/config: No such file or directory\n/bin/sh: line 2: --server=ip-172-31-4-117.us-west-1.compute.internal:6443: command not found\n/bin/sh: line 3: --embed-certs=true: command not found\n/bin/sh: line 4: --certificate-authority=/etc/kubernetes/pki/ca.crt: No such file or directory", "stderr_lines": [ "/bin/sh: line 1: --kubeconfig=~centos/.kube/config: No such file or directory", "/bin/sh: line 2: --server=ip-172-31-4-117.us-west-1.compute.internal:6443: command not found", "/bin/sh: line 3: --embed-certs=true: command not found", "/bin/sh: line 4: --certificate-authority=/etc/kubernetes/pki/ca.crt: No such file or directory" ],

最满意答案

这是YAML语法,YAML中的缩进很重要。 对于标有>的块标量,您应该使用单个缩进级别(否则YAML解析器将缩进行视为单独的缩进行):

- name: "Update kubeconfig to set cluster" shell: > kubectl config set-cluster {{ item.cluster }} --kubeconfig=~{{ item.account }}/.kube/config --server={{ K8S_MASTER_HOSTNAME }}:{{ K8S_API_SERCURE_PORT }} --embed-certs=true --certificate-authority={{ K8S_CA_HOME }}/ca.crt with_items: "{{ k8s_users }}"

或者您可以使用普通样式流标量:

- name: "Update kubeconfig to set cluster" shell: kubectl config set-cluster {{ item.cluster }} --kubeconfig=~{{ item.account }}/.kube/config --server={{ K8S_MASTER_HOSTNAME }}:{{ K8S_API_SERCURE_PORT }} --embed-certs=true --certificate-authority={{ K8S_CA_HOME }}/ca.crt with_items: "{{ k8s_users }}"

This is a YAML syntax and indentation in YAML is significant. For a block scalar marked with > you should use a single indentation level (otherwise YAML parser treats the indented lines as separate ones):

- name: "Update kubeconfig to set cluster" shell: > kubectl config set-cluster {{ item.cluster }} --kubeconfig=~{{ item.account }}/.kube/config --server={{ K8S_MASTER_HOSTNAME }}:{{ K8S_API_SERCURE_PORT }} --embed-certs=true --certificate-authority={{ K8S_CA_HOME }}/ca.crt with_items: "{{ k8s_users }}"

Or you can use plain style flow scalar:

- name: "Update kubeconfig to set cluster" shell: kubectl config set-cluster {{ item.cluster }} --kubeconfig=~{{ item.account }}/.kube/config --server={{ K8S_MASTER_HOSTNAME }}:{{ K8S_API_SERCURE_PORT }} --embed-certs=true --certificate-authority={{ K8S_CA_HOME }}/ca.crt with_items: "{{ k8s_users }}"

更多推荐

本文发布于:2023-08-06 03:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1443623.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:空格   multiline   Ansible   lines   spaces

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!