在Ansible中避免重复的项目列表可以通过以下几种方式实现:
- name: My Playbook
hosts: all
vars:
projects:
- project1
- project2
- project3
tasks:
- name: Add projects to list
set_fact:
project_list: "{{ project_list | default([]) + [item] }}"
loop: "{{ projects }}"
when: item not in project_list
- name: My Playbook
hosts: all
vars:
projects:
- project1
- project2
- project3
tasks:
- name: Generate project list
template:
src: project_list.j2
dest: /path/to/project_list.txt
project_list.j2 模板文件内容如下:
{% set project_list = [] %}
{% for project in projects %}
{% if project not in project_list %}
{{ project }}
{% set _ = project_list.append(project) %}
{% endif %}
{% endfor %}
- name: My Playbook
hosts: all
vars:
projects:
- project1
- project2
- project3
tasks:
- name: Remove duplicate projects
remove_duplicates:
projects: "{{ projects }}"
register: result
- name: Print project list
debug:
var: result.project_list
以上是三种常见的避免Ansible中重复项目列表的方法,根据具体情况选择适合的方式来实现。
领取专属 10元无门槛券
手把手带您无忧上云