-1

我对 apache 有这个简单的角色(在 CentOS7 中):

角色/apache/tasks/main.yml

---
- name: Add epel-release repo
  yum:
    name: epel-release
    state: present

- name: Install Apache2
  yum:
    name: httpd
    state: present

- name: Insert Index Page
  copy:
    src: index.html
    dest: /var/www/html/index.html

角色/apache/handlers/main.yml

---
- name: Start Apache
  service: name=httpd state=started

- name: verify that the web service is running
  command: systemctl status httpd
  register: status_result

- name: debug
  debug: var=status_result

with-roles.yml - 与“角色”目录相同级别的剧本

---
- name: Install apache2 in CentOS 7
  hosts: 1.23.4.56
  become: true
  roles:
    - apache

然后我按如下方式运行剧本:

$ ansible-playbook -u root --private-key ~/.ssh/this_key.ppk with-roles.yml -i "1.23.4.56" -vvvv

这是屏幕上详细输出的结尾部分:

    ...
    ...
    "mode": "0644", 
    "owner": "root", 
    "path": "/var/www/html/index.html", 
    "size": 11, 
    "state": "file", 
    "uid": 0
}
META: ran handlers
META: ran handlers

PLAY RECAP *******************************************************************************************************************
1.23.4.56              : ok=4    changed=0    unreachable=0    failed=0   

但是当我登录到 1.23.4.56 时,机器安装了 httpd 但已停止(这意味着处理程序没有运行)。我究竟做错了什么?

4

2 回答 2

1

处理程序仅在收到通知时才会执行。

请参阅处理程序:在更改时运行操作

于 2017-11-02T10:07:53.847 回答
0

The answer turns out to be in https://serverfault.com/questions/617548/always-trigger-handler-execution-in-ansible as per hints from René Pijl.

Specifically, I had to add this to the bottom of roles/apache/tasks/main.yml

...
...
- name: Apache Starter
  command: /bin/true
  notify: Start Apache
于 2017-11-02T10:50:11.383 回答