我有一个应该在指定的IP上配置的剧本,而不是连接到这个应用程序来配置里面的东西.
我遇到了一个问题:我需要在应用配置中更改任何内容后重新启动应用程序,如果我没有重新启动应用程序,则连接失败(没有连接,因为应用程序对新配置的新配置一无所知我'我试图访问).
我目前的剧本:
tasks:
- name: Configure app
template: src=app.conf.j2 dest=/etc/app.conf
notify: restart app
- name: Change data in app
configure_app: host={{new_ip}} data={{data}}
handlers:
- name: restart app
service: name=app state=restarted
Run Code Online (Sandbox Code Playgroud)
我需要强制处理程序configure_app
在执行'更改应用程序中的数据'之前运行.
我想在整个剧本中只运行一次处理程序.
我尝试在playbook文件中使用下面的include语句,但这导致处理程序多次运行,每次播放一次:
- name: Configure common config
hosts: all
become: true
vars:
OE: "{{ ansible_hostname[5] }}"
roles:
- { role: common }
handlers:
- include: handlers/main.yml
- name: Configure metadata config
hosts: metadata
become: true
vars:
OE: "{{ ansible_hostname[5] }}"
roles:
- { role: metadata }
handlers:
- include: handlers/main.yml
Run Code Online (Sandbox Code Playgroud)
这是handlers/main.yml的内容:
- name: restart autofs
service:
name: autofs.service
state: restarted
Run Code Online (Sandbox Code Playgroud)
以下是通知处理程序的任务之一的示例:
- name: Configure automount - /opt/local/xxx in /etc/auto.direct
lineinfile:
dest: /etc/auto.direct
regexp: "^/opt/local/xxx"
line: "/opt/local/xxx -acdirmin=0,acdirmax=0,rdirplus,rw,hard,intr,bg,retry=2 nfs_server:/vol/xxx"
notify: restart …
Run Code Online (Sandbox Code Playgroud) 我正在通过 ansible 为多个应用程序添加 JAVA_OPTS 作为环境变量,如果 JAVA_OPTS 更改,我想重新启动应用程序。
我现在拥有的是每个应用程序添加环境变量的任务和重启应用程序的通知,例如:
- 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
Run Code Online (Sandbox Code Playgroud)
因为我有很多应用程序这样做意味着有很多任务。我想要的是有一个任务来循环使用with_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: …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Ansible 自动执行一些任务。在我的剧本中,我有一个复制任务,然后更改文件的权限。我需要在执行此任务后重新启动该服务。我包括了通知并且还声明了我的处理程序,但奇怪的是这个处理程序永远不会被调用。
摘自我的剧本
- name: Configure Audit Log Purge Scheduler
copy:
src: "Scheduler-Log-Purge.config"
dest: "{{ crx_dir }}install/com.adobe.cq.audit.purge.Scheduler-LogPurge.config"
become: true
tags: aem
- name: Change Permissions of the Log Purge Scheduler config File
file:
path: "{{ crx_dir }}install/com.adobe.cq.audit.purge.Scheduler-LogPurge.config"
owner: crx
group: crx
become: true
notify: restart aem
tags: aem
- name: Pause the execution for cq5 to come up
pause:
minutes: 5
tags: aem
Run Code Online (Sandbox Code Playgroud)
这是我的处理程序文件内容。
---
- name: restart aem
service: name=cq5 state=restarted
become: yes
Run Code Online (Sandbox Code Playgroud)
运行剧本后的o/p
gparasha-macOS:TLTD gparasha$ ansible-playbook -i …
Run Code Online (Sandbox Code Playgroud)