带\u项的Ansible通知处理程序
我通过ansible为多个应用程序添加JAVA_选项作为环境变量,如果JAVA_选项发生更改,我希望重新启动一个应用程序 我现在的任务是为每个应用程序添加环境变量,并通知重新启动应用程序,如:带\u项的Ansible通知处理程序,ansible,ansible-handlers,Ansible,Ansible Handlers,我通过ansible为多个应用程序添加JAVA_选项作为环境变量,如果JAVA_选项发生更改,我希望重新启动一个应用程序 我现在的任务是为每个应用程序添加环境变量,并通知重新启动应用程序,如: - name: Add variable1 become: yes lineinfile: dest=/etc/environment regexp='^VARIABLE1=' line='VARIABLE1={{VARIABLE1}}' notify: restart application
- name: Add variable1
become: yes
lineinfile: dest=/etc/environment regexp='^VARIABLE1=' line='VARIABLE1={{VARIABLE1}}'
notify: restart application1
- name: restart application1
become: yes
command: restart application1
因为我有很多应用程序这样做意味着有很多任务。我希望有一个任务,使用和\u items
循环应用程序。我不明白的是如何让一个处理程序任务重新启动。是否可以将需要重新启动的应用程序传递给处理程序?比如:
- name: add variables
become: yes
lineinfile: dest=/etc/environment regexp='^{{item.app_name}}='
line='{{item.app_name}}={{ item.variable }}'
notify: restart apps #pass app_name to handler somehow
with_items:
- { variable: "FIRST", app_name: "APP1"}
- { variable: "SECOND", app_name: "APP2"}
- { variable: "THIRD", app_name: "APP3"}
- name: restart apps
become: yes
command: restart {{app_name}}
您可以通过注册值并在后续任务(第二个任务可能定义为处理程序,也可能不定义为处理程序)中循环来模拟处理程序功能:
您可以通过注册值并在后续任务(第二个任务可能定义为处理程序,也可能不定义为处理程序)中循环来模拟处理程序功能:
我在你的代码中没有看到任何处理程序。但您可能希望看到类似的问题:我在代码中没有看到任何处理程序。但您可能希望看到类似的问题:ansible lint警告[ANSIBLE0016]更改时运行的任务可能是处理程序,ansible Galaxy语法评分报告E503:更改时运行的任务可能是处理程序该任务应放在处理程序部分。ansible lint警告[ANSIBLE0016]更改时运行的任务可能是处理程序,ansible Galaxy语法评分报告E503:更改时运行的任务可能是处理程序该任务应放入处理程序部分。
- name: add variables
lineinfile:
dest: ./testfile
regexp: '^{{item.app_name}}='
line: '{{item.app_name}}={{ item.variable }}'
register: add_variables
with_items:
- { variable: "FIRST", app_name: "APP1"}
- { variable: "SECOND", app_name: "APP2"}
- { variable: "THIRD", app_name: "APP3"}
- name: restart apps
become: yes
command: restart {{item.item.app_name}}
when: item.changed
with_items: "{{ add_variables.results }}"