Ansible关于nginx的实验

安装nginx

指令如下:

1
2
3
4
5
6
7
8
9

ansible test -m yum -a name=nginx

# 安装前更新一下软件包
ansible test -m yum -a name=nginx update_cache=yes

# 重启nginx
ansible test -m service -a name=nginx

playbook中运行nginx的实验

如下playbook,在运行的时候始终无法正常的执行restart nginx,报错内容如下。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

---
- name: Configure webserver with nginx
  hosts: webservers
  sudo: False
  tasks:
    - name: install nginx
      yum: name=nginx
    - name: copy nginx config file
      copy: src=files/nginx.conf dest=/etc/nginx/sites-available/default/
    - name: create folder
      file: >
        path=/etc/nginx/sites-enabled
        state=directory        
    - name: enable configuration
      file: >
        dest=/etc/nginx/sites-enabled/default
        src=/etc/nginx/sites-available/default
        state=link        
    - name: copy index.html
      template: src=templates/index.html.j2 dest=/usr/share/nginx/html/index.html mode=0644
    - name: restart nginx
      service: name=nginx state=restarted

报错内容:


TASK: [restart nginx] ********************************************************* 
failed: [192.168.23.60] => {"failed": true}
msg: Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.


FATAL: all hosts have already failed -- aborting

PLAY RECAP ******************************************************************** 
           to retry, use: --limit @/home/junjie/web-notls.retry

192.168.23.60              : ok=6    changed=0    unreachable=0    failed=1 

之所以发生这个问题是因为我在安装nginx后,启动了该nginx,且在杀死nginx时由于nginx会自动重启,一直没有真正的杀死nginx进程,最后导致ansible无法正常启动nginx服务,杀死nginx后,该问题解决了。