无法使用ansible

时间:2018-05-13 07:29:45

标签: ansible

无法使用ansible启动或停止niginx。

yml文件如下所示,

---
- hosts: loadbalancer
  become: true
  tasks:
   - name: install nginx
     apt: name=nginx state=present update_cache=yes

   - name: ensure nginx is installed
     service: 
        name: nginx 
        state: stopped

我正在使用ansible-playbook 2.5.1,有趣的是安装命令有效,第二个任务也返回已执行更改但是启动或停止nginx。

我得到输出,

  

播放[loadbalancer]

           

任务[收集事实]   ************************************************** ******************************确定:[172.17.0.3]

     

任务[安装nginx]   ************************************************** ******************************确定:[172.17.0.3]

     

任务[确保安装nginx]   ************************************************** ****************************已更改:[172.17.0.3]

     

PLAY RECAP

           

172.17.0.3:ok = 3 changed = 1 unreachable = 0 failed = 0

任何解释和解决方案都有帮助。

提前致谢。

1 个答案:

答案 0 :(得分:0)

changed: [172.17.0.3]消息表示该服务处于已启动状态。事实上,在安装nginx时,如果您执行以下操作,则会启动默认状态:

   - name: install nginx
     apt: name=nginx state=present update_cache=yes

你会得到:

systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2018-05-13 09:00:00 UTC; 38s ago

这意味着在运行时:

   - name: ensure nginx is installed
     service: 
        name: nginx 
        state: stopped

您将停止服务(已更改为Ansible):

TASK [ensure nginx is installed] *****************************************************************************************************
changed: [ubuntu-1]

检查服务,它已停止:

systemctl status mginx
Unit mginx.service could not be found.
root@ubuntu-1:/home/vagrant# systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Sun 2018-05-13 09:01:28 UTC; 12s ago

除了所有这些解释之外,我建议安装Ansible 2.5.2,因为2.5.1存在严重的列表错误。