kubect-6ren">
gpt4 book ai didi

ansible - 如何让 Ansible 多行 > 忽略行中的空格?

转载 作者:行者123 更新时间:2023-12-01 21:24:21 26 4
gpt4 key购买 nike

问题

要在一行命令中进行缩进,需要在将命令分成多行的后续行中放置空格。

- 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"
],

最佳答案

这是 YAML 语法,YAML 中的缩进很重要。对于标有 > 的 block 标量,您应该使用单个缩进级别(否则 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 }}"

关于ansible - 如何让 Ansible 多行 > 忽略行中的空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48124622/

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