我想控制目录中仅链接文件。我基于变量链接文件,内容:
TASK [icinga2 : debug]
ok: [ansible-role-development] => {
"icinga_features": [
"checker",
"mainlog",
"notification"
]
}
此后使用 find 来注册目录中的文件。我手动添加了一个文件进行测试。
- name: Recursively find /tmp files older than 2 days
ansible.builtin.find:
paths: "{{ icinga_basedir }}/features-enabled/"
file_type: link
register: __features
这些文件在目录中具有扩展名.conf,当我链接它们时我添加它们,所以它是checker.conf或mainlog.conf。为了使其与 icinga_features 列表相当,我使用 regex_replace 过滤器,但它什么也不做。任务不会失败并表示一切正常,尽管 gelf.conf 不在列表中
- name: features | Disable features
ansible.builtin.file:
path: "{{ icinga_basedir }}/features-enabled/{{ item }}.conf"
state: absent
with_items:
- "{{ __features.files }}"
when: item|regex_replace('^(.*)\\.conf$', '\\1') not in icinga_features
有人可以澄清我错在哪里吗...
我通过过滤 find 的输出找到了解决方案:
- name: features | Gather enabled features to compare against icinga_features variable
ansible.builtin.find:
paths: "{{ icinga_basedir }}/features-enabled/"
file_type: link
register: __features
- name: features | Extract feature names
ansible.builtin.set_fact:
linked_features: "{{ __features.files | map(attribute='path') | map('basename') | map('regex_replace', '^(.*)\\.conf$', '\\1') | list }}"
- name: features | Disable features not activated via ansible
ansible.builtin.file:
path: "{{ icinga_basedir }}/features-enabled/{{ item }}.conf"
state: absent
with_items:
- "{{ linked_features }}"
notify:
- Icinga configcheck
- Restart icinga
when: item not in icinga_features