ansible配置nginx

nginx实例,可应用于生产

---
- name: tags playbook example
  hosts: webservers
  gather_facts: no
  vars:
    createuser:
      - tomcat
      - www
      - mysql
  tasks:
   #利用循环创建多个用户
    - name: create user
      user: name={{item}} state=present
      with_items: "{{createuser}}"
      
   #安装nginx
    - name: yum nginx webserver
      yum: name=nginx state=present
      
   #更新nginx主配置文件
    - name: update nginx main config
      copy: src=nginx.conf dest=/etc/nginx/
      tags: updateconfig
      
   #更新nginx配置文件
    - name: add virtualhost config
      copy: src=my.conf dest=/etc/nginx/conf.d/
      tags: updateconfig
      
   #检查nginx是否运行
    - name: check nginx running
      stat: path=/var/run/nginx.pid
      register: nginxrunning
      tags: updateconfig
      
   #检查nginx配置文件是否有问题
    - name: check nginx syntax
      shell: /usr/sbin/nginx -t
      register: nginxsyntax
      tags: updateconfig
   #删除上一步产生的pid
    - name: delete pid
      shell: rm -rf /var/run/nginx.pid
   #debug  nginx状态
    - name: print nginx syntax
      debug: var=nginxsyntax
      
   #重载nginx
    - name: reload nginx server
      service: name=nginx state=reloaded
      when: nginxsyntax.rc == 0 and nginxrunning.stat.exists == true
      tags: updateconfig
      
   #启动nginx
    - name: start nginx server
      service: name=nginx state=started
      when: 
        - nginxsyntax.rc == 0
        - nginxrunning.stat.exists == false
      tags: updateconfig
...

单步分析

若nginx以启动,exists=true

[root@localhost ~]# ansible dbservers -i hosts -m stat -a "path=/var/run/nginx.pid"
192.168.80.101 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "stat": {
        "atime": 1691162288.7590837, 
        "attr_flags": "", 
        "attributes": [], 
        "block_size": 4096, 
        "blocks": 8, 
        "charset": "us-ascii", 
        "checksum": "c0f0780ef0229cf881b2b5db604106810be3579b", 
        "ctime": 1691162288.7550838, 
        "dev": 20, 
        "device_type": 0, 
        "executable": false, 
        "exists": true, 

若nginx未启动,exists=false

[root@localhost ~]# ansible dbservers -i hosts -m stat -a "path=/var/run/nginx.pid"
192.168.80.101 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "stat": {
        "exists": false
    }
}

配置文件没问题

[root@localhost ~]# ansible dbservers -i hosts -m shell -a "/usr/sbin/nginx -t"
192.168.80.101 | CHANGED | rc=0 >>
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

配置文件有问题

[root@localhost ~]# ansible dbservers -i hosts -m shell -a "/usr/sbin/nginx -t"
192.168.80.101 | FAILED | rc=1 >>
nginx: [emerg] unknown directive "xxxxx" in /etc/nginx/conf.d/default.conf:2
nginx: configuration file /etc/nginx/nginx.conf test failednon-zero return code

执行

[root@localhost ~]# ansible-playbook -i hosts mynginx.yaml 

PLAY [tags playbook example] ******************************************************************************

TASK [create user] ****************************************************************************************
ok: [192.168.80.102] => (item=tomcat)
ok: [192.168.80.102] => (item=www)
ok: [192.168.80.102] => (item=mysql)

TASK [yum nginx webserver] ********************************************************************************
ok: [192.168.80.102]

TASK [update nginx main config] ***************************************************************************
ok: [192.168.80.102]

TASK [add virtualhost config] *****************************************************************************
ok: [192.168.80.102]

TASK [check nginx running] ********************************************************************************
ok: [192.168.80.102]

TASK [check nginx syntax] *********************************************************************************
changed: [192.168.80.102]

TASK [delete pid] *****************************************************************************************
[WARNING]: Consider using the file module with state=absent rather than running 'rm'.  If you need to use
command because file is insufficient you can add 'warn: false' to this command task or set
'command_warnings=False' in ansible.cfg to get rid of this message.
changed: [192.168.80.102]

TASK [print nginx syntax] *********************************************************************************
ok: [192.168.80.102] => {
    "nginxsyntax": {
        "changed": true, 
        "cmd": "/usr/sbin/nginx -t", 
        "delta": "0:00:00.021776", 
        "end": "2023-08-05 12:47:32.267317", 
        "failed": false, 
        "rc": 0, 
        "start": "2023-08-05 12:47:32.245541", 
        "stderr": "nginx: the configuration file /etc/nginx/nginx.conf syntax is ok\nnginx: configuration file /etc/nginx/nginx.conf test is successful", 
        "stderr_lines": [
            "nginx: the configuration file /etc/nginx/nginx.conf syntax is ok", 
            "nginx: configuration file /etc/nginx/nginx.conf test is successful"
        ], 
        "stdout": "", 
        "stdout_lines": []
    }
}

TASK [reload nginx server] ********************************************************************************
skipping: [192.168.80.102]

TASK [start nginx server] *********************************************************************************
changed: [192.168.80.102]

PLAY RECAP ************************************************************************************************
192.168.80.102             : ok=9    changed=3    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   

tag标签可以单独使用

更新nginx主配置文件
更新nginx配置文件
检查nginx是否运行
检查nginx配置文件是否有问题
重载nginx

使用标签

[root@localhost ~]# ansible-playbook -i hosts mynginx.yaml -t updateconfig

PLAY [tags playbook example] ******************************************************************************

TASK [update nginx main config] ***************************************************************************
ok: [192.168.80.102]

TASK [add virtualhost config] *****************************************************************************
ok: [192.168.80.102]

TASK [check nginx running] ********************************************************************************
ok: [192.168.80.102]

TASK [check nginx syntax] *********************************************************************************
changed: [192.168.80.102]

TASK [reload nginx server] ********************************************************************************
changed: [192.168.80.102]

PLAY RECAP ************************************************************************************************
192.168.80.102             : ok=5    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   


posted @ 2023-08-05 12:56  Bre-eZe  阅读(110)  评论(0)    收藏  举报