2016-12-13 34 views
0

http://docs.ansible.com/ansible/playbooks_filters.html網站,它說,我可以做到以下幾點:如何在沒有模板錯誤的情況下正確使用Ansible jinja2 map()和join()過濾器?

# get a comma-separated list of the mount points (e.g. "/,/mnt/stuff") on a host 
    {{ ansible_mounts|map(attribute='mount')|join(',') }} 

我這樣做有:

- debug: var="{{ ansible_mounts|map(attribute='mount')|join(', ') }}" 

這將導致以下每個錯誤...

FAILED! => {"failed": true, "msg": "template error while templating string: unexpected '/'. String: {{/, /homedata, /edbdata, /db1data, /db2data}}"} 

我是否缺少某種異常或默認語句?

打印出來ansible_mounts

[email protected]:/etc/ansible# ansible -u ubuntu -m setup 10.200.240.10 -a 'filter=ansible_mounts' 
10.200.240.10 | SUCCESS => { 
     "ansible_facts": { 
      "ansible_mounts": [ 
     { 
      "device": "/dev/xvda1", 
      "fstype": "ext4", 
      "mount": "/", 
      "options": "rw,noatime,data=ordered", 
      "size_available": 7024513024, 
      "size_total": 8318783488, 
      "uuid": "35634654356" 
     }, 
     { 
      "device": "/dev/xvdb", 
      "fstype": "xfs", 
      "mount": "/homedata", 
      "options": "rw,noatime,attr2,inode64,noquota", 
      "size_available": 13914439680, 
      "size_total": 13948157952, 
      "uuid": "345634564356" 
     }, 
     { 
      "device": "/dev/xvdc", 
      "fstype": "xfs", 
      "mount": "/edbdata", 
      "options": "rw,noatime,attr2,inode64,noquota", 
      "size_available": 16061923328, 
      "size_total": 16095641600, 
      "uuid": "23452345235" 
     }, 
     { 
      "device": "/dev/xvdc", 
      "fstype": "xfs", 
      "mount": "/db1data", 
      "options": "rw,noatime,attr2,inode64,noquota", 
      "size_available": 16061923328, 
      "size_total": 16095641600, 
      "uuid": 234523452" 
     }, 
     { 
      "device": "/dev/xvdd", 
      "fstype": "xfs", 
      "mount": "/db2data", 
      "options": "rw,noatime,attr2,inode64,noquota", 
      "size_available": 16061923328, 
      "size_total": 16095641600, 
      "uuid": "23423452" 
     } 
    ] 
}, 
"changed": false 

}

+0

對我來說工作很好。你使用哪個版本? – Shasha99

+0

ansible 2.2.0.0 – Cloudish123

+0

好的。你可以嘗試打印ansible_mounts的價值嗎? – Shasha99

回答

1

的同時使用var你不需要在ansible 2.2.0大括號。試試這個:

- debug: var=ansible_mounts|map(attribute='mount')|join(', ') 

請注意,在安全2.2.0中使用msg時需要大括號。試試這個:

- debug: msg={{ansible_mounts|map(attribute='mount')|join(', ')}} 
+0

非常感謝!有用 – Cloudish123

相關問題