如何正确访问Ansible Facts中mounts的子属性(如fstype)?
问题解决:Ansible Facts中mounts属性的正确访问方式
ansible_facts['mounts']是一个列表类型的系统事实,里面的每一个元素才是包含fstype、mount、size_available等属性的字典对象。直接用ansible_facts['mounts']['fstype']这种字典键访问的方式是错误的,因为列表无法通过字符串键来取值。
如果要在when条件中判断挂载点的属性,正确的做法是遍历mounts列表,通过每个列表项(即item)来访问对应的子属性。
比如要检查/boot挂载点的可用空间,满足条件时更新内核,可以这样实现:
--- - name: Conditionals test hosts: dev tasks: - name: Update the kernel if sufficient space package: name: kernel state: latest loop: "{{ ansible_facts['mounts'] }}" when: item.mount == "/boot" and item.size_available > 20000000
这段代码通过loop遍历所有挂载项,在when条件里先通过item.mount == "/boot"定位到目标挂载点,再判断其size_available是否符合要求,只有满足条件时才执行内核更新操作。
内容的提问来源于stack exchange,提问作者SwiperNo
相关产品推荐
相关产品推荐

