我有一个 Ansible 剧本,可以在一组服务器中安装可变数量的应用程序。要安装应用程序,必须运行许多顺序任务,并且由于可能有多个应用程序,我循环通过它们with_items:
我还以这样的方式注册本地事实中的任何更改,如果在应用程序 A 上执行三个任务,则标记应用程序 A。
我遇到了处理程序的问题。它应该读取这些本地事实并重新启动任何已标记的应用程序,但我未能实现这一点。我的处理程序只是跳过,但调试显示带有标志的本地事实。
我的剧本与此类似:
---
- name: Ensure the application's jar file exists
copy:
src: '{{ item.appName }}/{{ item.jarName }}'
dest: '{{ AppsRootFolder }}/{{ item.appName }}/{{ item.jarName }}'
register: task
with_items: '{{ deployApp }}'
notify: Restart application
- name: Registering App for later restart
set_fact:
myapps_toberestarted_{{ item.item.appName }}: "{{ item.changed }}"
with_items: "{{ task.results }}"
when: "{{ item.changed }}"
- name: Ensure the application's conf file exists
template:
src: '{{ item.confName }}.j2'
dest: '{{ AppsRootFolder }}/{{ item.appName }}/{{ item.confName }}'
register: task
with_items: '{{ deployApp }}'
notify: Restart application
- name: Registering App for later restart
set_fact:
myapps_toberestarted_{{ item.item.appName }}: "{{ item.changed }}"
with_items: "{{ task.results }}"
when: "{{ item.changed }}"
我需要帮助的处理程序如下。它正在跳过“重新启动应用程序”任务:
- name: Restart application
debug: var=myapps_toberestarted_{{ item.appName }}
with_items: "{{ deployApp }}"
when: myapps_toberestarted_{{ item.appName }} == 'true'
最后我的group_vars
AppsRootFolder: /opt/Apps
deployApp:
- { appName: "API", jarName: "api.jar", confName: "api.conf" }
- { appName: "Demo", jarName: "demo.jar", confName: "demo.conf" }
- { appName: "Another", jarName: "another.jar", confName: "another.conf" }