如何让Ansible多线与>忽略行中的空格?

时间:2018-01-06 05:39:48

标签: ansible 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 }}"

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

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

1 个答案:

答案 0 :(得分:5)

这是一个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 }}"