【问题标题】:Ansible: multi line over playbookAnsible:剧本上的多行
【发布时间】:2016-04-18 14:32:50
【问题描述】:

我有一个关于多行和 Ansible 剧本的问题:

我创建了一个非常大的线的剧本,我需要剪掉这条线以便更好地阅读。我该怎么做?

  - name: 'Create VM Azure :-P '
    shell: if ! grep {{ item }} /tmp/vm_{{ rgName }}; then azure vm create --vm-size {{ groups['item'][vmsize] }} --resource-group {{ rgName }} --name {{ item }} --location {{ location }} --admin-username {{ username }} --ssh-publickey-file {{ sshfile }} --storage-account-name {{ rgName | lower }} --os-type {{ groups['item'][type_os] }} --image-urn {{ image }} --data-disk-size {{ disksize }} --subnet-id {{ subnetid_key }} --nic-names {{ item }}; fi
    with_items: groups['test']

我想按照以下方式进行,但是执行剧本时出现了一些错误

      - name: 'Create VM Azure :-P '
        shell: if ! grep {{ item }} /tmp/vm_{{ rgName }}; then 
                azure vm create --vm-size {{ groups['item'][vmsize] }} 
                --resource-group {{ rgName }} --name {{ item }} 
                --location {{ location }} --admin-username {{ username }} 
                --ssh-publickey-file {{ sshfile }} --storage-account-name {{ rgName | lower }} 
                --os-type {{ groups['item'][type_os] }} --image-urn {{ image }} 
                --data-disk-size {{ disksize }} --subnet-id {{ subnetid_key }} 
                --nic-names {{ item }}; fi
        with_items: groups['test']

错误:

错误!加载 YAML 时出现语法错误。

错误似乎出现在“/home/pvillarruel/docker/azure-ansible/data/playbook.yml”中:第 79 行,第 1 列,但可能 根据确切的语法问题,位于文件中的其他位置。

违规行似乎是:

shell: if ! grep {{ item }} /tmp/vm_{{ rgName }}; then azure vm create --vm-size
   {{ groups['item'][vmsize] }} --resource-group {{ rgName }} --name {{ item }}

^ 这里 我们可能是错的,但这个看起来可能是一个问题 缺少引号。总是引用模板表达式括号 开始一个值。例如:

with_items:
  - {{ foo }}

应该写成:

with_items:
  - "{{ foo }}"

谢谢

【问题讨论】:

    标签: yaml ansible ansible-playbook multiline


    【解决方案1】:

    使用 YAML 块标量,它们就是专门为这种事情设计的:

      shell: >
        if ! grep {{ item }} /tmp/vm_{{ rgName }}; then 
        azure vm create --vm-size {{ groups['item'][vmsize] }} 
        --resource-group {{ rgName }} --name {{ item }} 
        --location {{ location }} --admin-username {{ username }} 
        --ssh-publickey-file {{ sshfile }}
        --storage-account-name {{ rgName | lower }} 
        --os-type {{ groups['item'][type_os] }} --image-urn {{ image }} 
        --data-disk-size {{ disksize }} --subnet-id {{ subnetid_key }} 
        --nic-names {{ item }}; fi
    

    > 表示后面的块是一个标量并且行应该被折叠,即换行符将被转换为单个空格。您也不必担心特殊字符,所有内容都将按字面意思在折叠块标量中获取。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-08
      • 1970-01-01
      • 2017-09-26
      • 1970-01-01
      • 1970-01-01
      • 2016-10-01
      • 1970-01-01
      相关资源
      最近更新 更多