Ansible:在使用带有with_items列表的service-method时,只重启Apache一次,而不是多次

时间:2017-04-12 12:46:48

标签: apache ansible

我有一个基本的部署手册,它使用Ansible复制了几个Apache配置文件和python文件,并且对于某些状态已注册的文件,所以如果文件被更改,我可以重新加载Apache。

我无法弄清楚如何设置重新加载Apache,因此如果更改了4个文件,Apache将只重新加载一次(目前每个更改的文件重新加载一次)。

Playbook I使用:

- name: Copy apache files
  become: yes
  become_method: sudo
  copy: src=../files/index.html dest=/var/www/docs/index.html owner=root  group=root mode=755

- name: Copy app variable file
  become: yes
  become_method: sudo
  copy: src=../files/app_vars dest=/etc/app owner=root group=root mode=644       

- name: Copy pyapp files
  become: yes
  become_method: sudo
  copy: src=../files/pyapp.py dest=/var/www/pyapp/pyapp.py owner=flask group=flask mode=755
  register: pyapp

- name: Copy wsgi files
  become: yes
  become_method: sudo
  copy: src=../files/pyapp.wsgi dest=/var/www/pyapp/pyapp.wsgi owner=flask group=flask mode=755
  register: wsgi

- name: Copy apache files
  become: yes
  become_method: sudo
  copy: src=../files/pyapp.conf dest=/etc/httpd/conf.d/pyapp.conf owner=root group=root mode=644
  register: pyapp_conf

- name: Copy apache files
  become: yes
  become_method: sudo
  copy: src=../files/docs.conf dest=/etc/httpd/conf.d/docs.conf owner=root group=root mode=644
  register: docs

- name: Reload Apache configs if pyapp or Apache - files are changed
  become: yes
  become_method: sudo
  service: name=httpd state=reloaded
  when: "{{ item }}"
  with_items:
    - pyapp.changed
    - wsgi.changed
    - pyapp_conf.changed
    - docs.changed

1 个答案:

答案 0 :(得分:1)

您可以通过以下方式更改剧本:

tasks:
    - name: copy files
      copy: src={{ item.src }} dest={{ item.dest }} owner={{ item.owner }} group={{ item.group }} mode={{ item.mode }}
      with_items:
      - {src: '../files/pyapp.wsgi', dest: '/var/www/pyapp/pyapp.wsgi', mode: 755, owner: 'flask', group: 'flask'}
      - ... <all other files you copy>
      notify: httpd restart

handlers:
    - name: httpd restart
      service: name=httpd state=reloaded

become:yes之后立即将hosts:移至Playbook的开头。