
Ansible이 호스트에 대한 정보를 수집할 때 예를 들어 호스트의 모든 마운트를 가져옵니다.
"ansible_mounts": [
{
"block_available": 7800291,
"block_size": 4096,
"block_total": 8225358,
"block_used": 425067,
"device": "/dev/mapper/foobar",
"fstype": "xfs",
"inode_available": 16403366,
"inode_total": 16458752,
"inode_used": 55386,
"mount": "/",
"options": "rw,seclabel,relatime,attr2,inode64,noquota",
"size_available": 31949991936,
"size_total": 33691066368,
"uuid": "2ebc82cb-5bc2-4db9-9914-33d65ba350b8"
},
{
"block_available": 44648,
"block_size": 4096,
"block_total": 127145,
"block_used": 82497,
"device": "/dev/sda1",
"fstype": "xfs",
"inode_available": 255595,
"inode_total": 256000,
"inode_used": 405,
"mount": "/boot",
"options": "rw,seclabel,relatime,attr2,inode64,noquota",
"size_available": 182878208,
"size_total": 520785920,
"uuid": "c5f7eaf2-5b70-4f74-8189-a63bb4bee5f8"
},
등등. 따라서 제가 하려는 작업은 다음과 같습니다. 템플릿에서 배열의 모든 개체를 반복하고 각 "마운트" 키의 값을 출력하고 싶습니다.
나는 이것을 다음과 같이 시도합니다 :
(% for mounts in {{ ansible_mounts }} %)
Mountpoint: {{ ansible_mounts.mount }}
(% endfor %)
그러나 그것은 작동하지 않습니다. iteritems()와 같은 다른 것들을 사용해 보았지만 작동시킬 수 없습니다. 내가 아는 한, 누군가에게 도움이 된다면 Ansible의 출력은 json에 있습니다. 누군가 해결책을 알고 있습니까? 아니면 이것이 stackoverflow에 대한 질문에 더 가깝습니까?
답변해 주셔서 감사합니다.
답변1
간단한 구문 오류가 있습니다. 괄호 대신 중괄호를 사용해야 합니다.
귀하는 현재 다음을 보유하고 있습니다:
(% for mounts in {{ ansible_mounts }} %)
Mountpoint: {{ ansible_mounts.mount }}
(% endfor %)
이는 괄호가 아닌 중괄호여야 합니다. 즉, {%
및 입니다 %}
.
또한, 에서 선택한 변수 이름 for
은 mounts
이므로 각 개체를 가져오기 위해 루프 내에서 실제로 이 변수 이름을 사용해야 합니다.
마지막으로 for 루프의 변수 주위에 중괄호가 필요하지 않습니다.
이러한 오류를 수정하면 다음과 같은 결과가 나오며 정상적으로 작동합니다.
{% for mounts in ansible_mounts %}
Mountpoint: {{ mounts.mount }}
{% endfor %}