You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Ansible中使用>的多行命令忽略行空格不被拆分?

Fixing Multi-Line Shell Command Execution in Ansible

I see the issue you're facing here—your multi-line kubectl command is being split into separate shell commands instead of running as a single unified command. Let's break down why this happens and how to fix it while keeping your desired indentation formatting.

Why This Error Occurs

When you use YAML's > folded scalar, it’s supposed to convert newlines into spaces to create a single command string. But your error shows each line is being executed independently, which means YAML isn’t parsing the multi-line block correctly. This usually happens due to inconsistent indentation (like mixing tabs and spaces) or incorrect nesting under the shell: key.

Solutions to Preserve Indentation and Execute as a Single Command

Option 1: Use | with Shell Line Continuations

This method keeps your clean multi-line indentation in the Playbook, while using backslashes to tell the shell to treat the entire block as one command:

- 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 }}"

The | preserves newlines in the Playbook, and the backslashes ensure the shell interprets the entire block as a single command.

Option 2: Correct the Folded Scalar (>) Indentation

If you prefer using > instead, make sure all subsequent lines are indented one level deeper than the shell: key, with consistent spacing (no tabs!). Here's the corrected version:

- 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 }}"

Double-check that the lines starting with --kubeconfig match the indentation of the kubectl command line—YAML requires this consistency to fold the lines into a single string.

Option 3: Explicitly Use the cmd Parameter

For extra clarity, you can explicitly define the cmd parameter under the shell module, eliminating any parsing ambiguity:

- name: "Update kubeconfig to set cluster"
  shell:
    cmd: >
      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 makes it explicitly clear that all lines under cmd belong to a single command string.


内容的提问来源于stack exchange,提问作者mon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:28:26