如何在Ansible Playbook中有条件的列表中使用列表
我正在尝试检查是否添加了某些安装点,但只想看到我添加的那些存储在add_mounts
中。
如何为变量中的列表馈送到示例下的有条件的列表?
对不起,如果我的措辞没有划痕,但我仍在学习。
我正在使用Ansible 2.9。
我尝试使用vars_files
并制作列表并将其放入./ host_vars/test/add_mounts
或./ vars/vars/add_mounts
中但这无济于事。
我当时想在有条件的情况下尝试一个循环,但下面是行不通的。
when: item.device == {{ item }}
loop: added_mounts
列表中的一个项目可行:
- name: "check mounted directories"
hosts: test
vars:
- added_mounts: '/dev/sda1'
tasks:
- name: Show only Mount point and device info
debug:
msg: "{{ item.mount }} - {{ item.device }}"
loop: "{{ ansible_facts.mounts }}"
loop_control:
label: "{{ item.mount }} - {{ item.device }}"
when: item.device == added_mounts
列表中的几个项目不起作用:
- name: "check mounted directories"
hosts: test
vars:
- added_mounts:
- '/dev/sda1'
- '/dev/mapper/vg_abc-lv_abc'
tasks:
- name: Show only Mount point and device info
debug:
msg: "{{ item.mount }} - {{ item.device }}"
loop: "{{ ansible_facts.mounts }}"
loop_control:
label: "{{ item.mount }} - {{ item.device }}"
when: item.device == added_mounts
如果有人有任何提示或指向显而易见的,我将最感激。
I'm trying to check if certain mount points have been added but only want to see those I added which are stored into added_mounts
.
How to feed a list which is in a variable to a when conditional as in underneath example?
Sorry if my wording is not up to scratch but I'm still learning.
I'm using Ansible 2.9.
I've tried to use vars_files
and make a list and place it in ./host_vars/test/added_mounts
or in ./vars/added_mounts
but it does not help.
I was thinking to try with a loop in the when conditional but underneath does not work.
when: item.device == {{ item }}
loop: added_mounts
One item in the list works:
- name: "check mounted directories"
hosts: test
vars:
- added_mounts: '/dev/sda1'
tasks:
- name: Show only Mount point and device info
debug:
msg: "{{ item.mount }} - {{ item.device }}"
loop: "{{ ansible_facts.mounts }}"
loop_control:
label: "{{ item.mount }} - {{ item.device }}"
when: item.device == added_mounts
Several items in the list doesn't work:
- name: "check mounted directories"
hosts: test
vars:
- added_mounts:
- '/dev/sda1'
- '/dev/mapper/vg_abc-lv_abc'
tasks:
- name: Show only Mount point and device info
debug:
msg: "{{ item.mount }} - {{ item.device }}"
loop: "{{ ansible_facts.mounts }}"
loop_control:
label: "{{ item.mount }} - {{ item.device }}"
when: item.device == added_mounts
If anyone has any hints or pointing to the obvious, I would be most grateful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已经在评论中有一个提示,可以在循环中解决您的直接问题。
同时,在这种情况下,您甚至不需要条件。您可以使用
selectAttr
过滤器直接使用列表从源中选择现有的安装座。有关参考,请参见 nofollow noreferrer“> data Manipulation” 几乎与您的问题)。这是一个示例:
You already have a hint in the comments to make a condition on your loop which answers your direct question.
Meanwhile you don't even need a condition in that case. You can select the existing mounts using your list directly from source using the
selectattr
filter. For a reference see Data manipulation in ansible (where you will actually find an example which almost perfectly matches your question).Here is an example: