如何在没有模板错误的情况下正确使用 Ansible jinja2 map() 和 join() 过滤器?
How do I use Ansible jinja2 map() and join() filters correctly without template error?
http://docs.ansible.com/ansible/playbooks_filters.html 网站上说我可以执行以下操作:
# 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
root@ip-10-200-241-204:/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
}
在使用 var
时,ansible 2.2.0 中不需要花括号。试试这个:
- debug: var=ansible_mounts|map(attribute='mount')|join(', ')
注意在ansible 2.2.0中使用msg
时需要花括号。试试这个:
- debug: msg={{ansible_mounts|map(attribute='mount')|join(', ')}}
http://docs.ansible.com/ansible/playbooks_filters.html 网站上说我可以执行以下操作:
# 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
root@ip-10-200-241-204:/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
}
在使用 var
时,ansible 2.2.0 中不需要花括号。试试这个:
- debug: var=ansible_mounts|map(attribute='mount')|join(', ')
注意在ansible 2.2.0中使用msg
时需要花括号。试试这个:
- debug: msg={{ansible_mounts|map(attribute='mount')|join(', ')}}