Ansible:多行超过剧本

时间:2016-04-18 14:32:51

标签: yaml ansible ansible-playbook multiline

我对多行和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']

我想按照以下方式制作,但是在执行playbook时我有一些错误

      - 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']

错误:

ERROR!加载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 }}"

由于

1 个答案:

答案 0 :(得分:5)

使用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

>表示以下块是标量,应折叠线,即换行符将转换为单个空格。你也不需要担心特殊字符,所有内容都将在折叠块标量中进行。