Ansible fact ansible_mounts 保存了挂载点的列表。让我们使用以下变量进行测试
widgets:
- /usr/local/apps/widget
- /usr/share/apps/widget
- /scratch/apps/widget
my_ansible_mounts:
- mount: /
- mount: /boot/efi
- mount: /usr
- mount: /usr/local
root:
- /
创建一个包含小部件和相关挂载点的字典,例如
- set_fact:
_mlist: []
- set_fact:
_mlist: "{{ _mlist + [{'dict': item.0, 'mount': item.1}] }}"
with_nested:
- "{{ widgets }}"
- "{{ my_ansible_mounts|map(attribute='mount')|difference(root) }}"
when: item.0|regex_search('^' ~ item.1) != None
- set_fact:
_mdict: {}
- set_fact:
_mdict: "{{ _mdict|combine({item.0: item.1|
map(attribute='mount')|
map('regex_replace', '/', '-')|
list}) }}"
loop: "{{ _mlist|groupby('dict') }}"
给予
_mdict:
/usr/local/apps/widget:
- -usr
- -usr-local
/usr/share/apps/widget:
- -usr
那么流程应该是微不足道的,例如
- debug:
msg: |-
{% if _mdict[item]|default([])|length > 0 %}
[Unit]
Description = Start the widget
After = network.target{% for i in _mdict[item] %} {{ i[1:] }}.mount{% endfor %}
{% endif %}
[Service]
Type = simple
ExecStart = {{ item }}
loop: "{{ widgets }}"
给予
ok: [localhost] => (item=/usr/local/apps/widget) =>
msg: |-
[Unit]
Description = Start the widget
After = network.target usr.mount usr-local.mount
[Service]
Type = simple
ExecStart = /usr/local/apps/widget
ok: [localhost] => (item=/usr/share/apps/widget) =>
msg: |-
[Unit]
Description = Start the widget
After = network.target usr.mount
[Service]
Type = simple
ExecStart = /usr/share/apps/widget
ok: [localhost] => (item=/scratch/apps/widget) =>
msg: |-
[Service]
Type = simple
ExecStart = /scratch/apps/widget
要测试真实的东西,在代码中,将 my_ansible_mounts 替换为 ansible_mounts 并适合 widgets 和 hosts 满足您的需求。