如何在Ansible Playbook中有条件的列表中使用列表

发布于 2025-02-13 01:15:04 字数 1432 浏览 1 评论 0原文

我正在尝试检查是否添加了某些安装点,但只想看到我添加的那些存储在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北城孤痞 2025-02-20 01:15:04

您已经在评论中有一个提示,可以在循环中解决您的直接问题。

同时,在这种情况下,您甚至不需要条件。您可以使用selectAttr过滤器直接使用列表从源中选择现有的安装座。有关参考,请参见 nofollow noreferrer“> data Manipulation” 几乎与您的问题)。

这是一个示例:

- name: Show only Mount point and device info
  debug:
    msg: "{{ item.mount }} - {{ item.device }}"
  loop: "{{ ansible_facts.mounts | selectattr('device', 'in', added_mounts) | list }}"
  loop_control:
    label: "{{ item.mount }} - {{ item.device }}"

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:

- name: Show only Mount point and device info
  debug:
    msg: "{{ item.mount }} - {{ item.device }}"
  loop: "{{ ansible_facts.mounts | selectattr('device', 'in', added_mounts) | list }}"
  loop_control:
    label: "{{ item.mount }} - {{ item.device }}"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文