ToB企服应用市场:ToB评测及商务社交产业平台

标题: Ansible运维实战-自动化安装nginx [打印本页]

作者: 美丽的神话    时间: 2024-12-15 02:30
标题: Ansible运维实战-自动化安装nginx
Ansible运维实战

1.Ansible自动化安装nginx

(1).情况预备

我们创建两台虚拟机分别为server节点、host1节点
两个节点根据节点规划来修改主机名
我们在server节点下安装ansible、其余节点不进行设置
节点

Ip地址

Server节点

192.168.77.171

Host1节点

192.168.77.172

我们先创建一个nginx脚色
  1. [root@server ~]# ansible-galaxy init /etc/ansible/roles/nginx
复制代码
将在当前目次下创建一个名为 nginx 的新目次,并添补尺度的脚色布局。脚色布局如下:

  1. [root@server ~]# tree /etc/ansible/roles/nginx/
  2. /etc/ansible/roles/nginx/
  3. ├── defaults
  4. │   └── main.yml
  5. ├── files
  6. ├── handlers
  7. │   └── main.yml
  8. ├── meta
  9. │   └── main.yml
  10. ├── README.md
  11. ├── tasks
  12. │   └── main.yml
  13. ├── templates
  14. ├── tests
  15. │   ├── inventory
  16. │   └── test.yml
  17. └── vars
  18.     └── main.yml
复制代码

我们先用wget下令在server节点上拉取nginx-1.9.6.tar.gz压缩包然后解压压缩包进行编译安装
  1. [root@server ~]# wget http://mirrors.sohu.com/nginx/nginx-1.9.6.tar.gz
  2. [root@server ~]# tar -zxvf nginx-1.9.6.tar.gz
  3. [root@server ~]# cd nginx-1.9.6
  4. [root@server nginx-1.9.6]# ./configure --prefix=/usr/local/nginx            #编译安装
  5. [root@server nginx-1.9.6]# make && make install
复制代码
将nginx-1.9.6.tar.gz复制到/etc/ansible/roles/nginx/files目次下
  1. [root@server ~]# cp nginx-1.9.6.tar.gz /etc/ansible/roles/nginx/files/
复制代码
(2).文件内容

定义设置文件

  1. [root@server ~]# vi /etc/ansible/roles/nginx/tasks/main.yml
  2. [root@server ~]# cat /etc/ansible/roles/nginx/tasks/main.yml
  3. - name: 创建 Nginx 用户
  4.   user:
  5.     name: "{{ nginx_user }}"
  6.     system: yes
  7.     shell: /sbin/nologin
  8.     state: present
  9. - name: 安装依赖包
  10.   yum:
  11.     name: zlib-devel,pcre-devel,gcc
  12.     state: present
  13. - name: 复制nginx压缩包
  14.   copy:
  15.     src: "{{ nginx_package_path }}"
  16.     dest: /root/nginx-1.9.6.tar.gz
  17.     owner: root
  18.     group: root
  19.     mode: 0644
  20. - name: 解压压缩包
  21.   unarchive:
  22.     src: "/root/nginx-1.9.6.tar.gz"
  23.     dest: "/root/"
  24.     remote_src: yes  # 如果文件已经在远程主机上,则设置为 no
  25. - name: 编译安装 Nginx
  26.   shell: >
  27.     cd /root/nginx-1.9.6 &&
  28.     ./configure --prefix=/usr/local/nginx &&
  29.     make && make install
  30. - name: 编写 Nginx 启动文件
  31.   template:
  32.     src: "{{ nginx_service_j2_file_path }}"
  33.     dest: /etc/systemd/system/nginx.service
  34.     owner: root
  35.     group: root
  36.     mode: '0755'
  37.   notify: daemon-reload
  38. - name: 编写 Nginx 配置文件
  39.   template:
  40.     src: "{{ nginx_conf_j2_file_path }}"
  41.     dest: /usr/local/nginx/conf/nginx.conf
  42.     owner: root
  43.     group: root
  44.     mode: '0644'
  45.   notify: reload nginx
  46. - name: 检查 Nginx 配置文件语法
  47.   command: /usr/local/nginx/sbin/nginx -t
  48.   register: nginx_test
  49.   changed_when: false
  50.   failed_when: "'test failed' in nginx_test.stdout"
  51. - name: 启动 Nginx 服务并设置开机自启
  52.   systemd:
  53.     name: nginx
  54.     state: started
  55.     enabled: true
  56.   when: nginx_test is succeeded
  57. - name: 删除 Nginx 压缩包
  58.   file:
  59.     path: /root/nginx-1.9.6.tar.gz
  60.     state: absent
复制代码
定义templates天生设置文件

Nginx设置文件
  1. [root@server ~]# vi /etc/ansible/roles/nginx/templates/nginx.conf.j2
  2. [root@server ~]# cat /etc/ansible/roles/nginx/templates/nginx.conf.j2
  3. user  {{ nginx_user }}; # 设置 Nginx 服务使用的系统用户
  4. worker_processes  {{ ansible_processor_vcpus }}; # 工作进程数
  5. error_log  /usr/local/nginx/logs/error.log warn; # Nginx 的错误日志
  6. pid        /usr/local/nginx/logs/nginx.pid; # Nginx 启动时的 PID 文件
  7. events {
  8.     worker_connections  1024; # 每个进程允许的最大连接数
  9. }
  10. http { # HTTP 请求配置,一个 http 可以包含多个 server
  11.     # 定义 Content-Type
  12.     include       /usr/local/nginx/conf/mime.types;
  13.     default_type  application/octet-stream;
  14.     # 日志格式
  15.     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  16.                       '$status $body_bytes_sent "$http_referer" '
  17.                       '"$http_user_agent" "$http_x_forwarded_for"';
  18.     # 访问日志
  19.     access_log  /usr/local/nginx/logs/access.log  main;
  20.     # 高效文件传输
  21.     sendfile        on;
  22.     keepalive_timeout  65;
  23.     server { # HTTP 服务配置
  24.         listen       {{ nginxport }};
  25.         server_name  localhost;
  26.         location / {
  27.             root   /usr/local/nginx/html; # 页面存放目录
  28.             index  index.html index.htm;
  29.         }
  30.         error_page   500 502 503 504  /50x.html;
  31.         location = /50x.html {
  32.             root   /usr/local/nginx/html;
  33.         }
  34.     }
  35.     include /usr/local/nginx/conf.d/*.conf;
  36. }
复制代码
Nginx启动脚本
  1. [root@server ~]# vi /etc/ansible/roles/nginx/templates/nginx.service.j2
  2. [root@server ~]# cat /etc/ansible/roles/nginx/templates/nginx.service.j2
  3. [Unit]
  4. Description=A high performance web server and a reverse proxy server
  5. After=network.target
  6. [Service]
  7. Type=forking
  8. PIDFile=/usr/local/nginx/logs/nginx.pid
  9. ExecStartPre=/usr/local/nginx/sbin/nginx -t -q -g 'daemon on; master_process on;'
  10. ExecStart=/usr/local/nginx/sbin/nginx -g 'daemon on; master_process on;'
  11. ExecReload=/usr/local/nginx/sbin/nginx -s reload
  12. ExecStop=/bin/kill -s QUIT $MAINPID
  13. PrivateTmp=true
  14. [Install]
  15. WantedBy=multi-user.target
复制代码
定义变量

  1. [root@server ~]# vi /etc/ansible/roles/nginx/vars/main.yml
  2. [root@server ~]# cat /etc/ansible/roles/nginx/vars/main.yml
  3. nginx_user: qiu
  4. nginx_package: nginx-1.9.6.tar.gz
  5. nginx_package_path: /etc/ansible/roles/nginx/files/nginx-1.9.6.tar.gz
  6. nginx_service_j2_file_path: /etc/ansible/roles/nginx/templates/nginx.service.j2
  7. nginx_conf_j2_file_path: /etc/ansible/roles/nginx/templates/nginx.conf.j2
  8. nginxport: 80
复制代码
定义触发

由于上面通知已经定义,以是,还必要定义一个触发
  1. [root@server ~]# vi /etc/ansible/roles/nginx/handlers/main.yml
  2. [root@server ~]# cat /etc/ansible/roles/nginx/handlers/main.yml
  3. - name: daemon-reload
  4.   systemd: daemon-reload=yes
  5. - name: reload nginx
  6.   systemd: name=nginx state=reloaded
复制代码
定义剧本文件

  1. [root@server ~]# vi /etc/ansible/roles/nginx/install.yml
  2. [root@server ~]# cat /etc/ansible/roles/nginx/install.yml
  3. ---
  4. - hosts: host1
  5.   remote_user: root
  6.   roles:
  7.     - nginx
复制代码
定义之后的脚色布局

  1. [root@server ~]# tree /etc/ansible/roles/nginx/
  2. /etc/ansible/roles/nginx/
  3. ├── defaults
  4. │?? └── main.yml
  5. ├── files
  6. │?? └── nginx-1.9.6.tar.gz
  7. ├── handlers
  8. │?? └── main.yml
  9. ├── install.yml
  10. ├── meta
  11. │?? └── main.yml
  12. ├── README.md
  13. ├── tasks
  14. │?? └── main.yml
  15. ├── templates
  16. │?? ├── nginx.conf.j2
  17. │?? └── nginx.service.j2
  18. ├── tests
  19. │?? ├── inventory
  20. │?? └── test.yml
  21. └── vars
  22.     └── main.yml
复制代码

(3).实行文件

查抄yml文件语法是否正确

  1. [root@server ~]# ansible-playbook --syntax-check /etc/ansible/roles/nginx/install.yml
  2. # 检查install.yml会自动去检查其他的yml文件的语法。
复制代码

实行roles.yml文件

  1. [root@server ~]# ansible-playbook /etc/ansible/roles/nginx/install.yml
  2. PLAY [host1] *****************************************************************************************************************************
  3. TASK [Gathering Facts] *******************************************************************************************************************
  4. ok: [192.168.77.172]
  5. TASK [nginx : 创建 Nginx 用户] ***************************************************************************************************************
  6. changed: [192.168.77.172]
  7. TASK [nginx : 安装依赖包] *********************************************************************************************************************
  8. changed: [192.168.77.172]
  9. TASK [复制nginx压缩包] ************************************************************************************************************************
  10. changed: [192.168.77.172]
  11. TASK [nginx : 解压压缩包] *********************************************************************************************************************
  12. changed: [192.168.77.172]
  13. TASK [nginx : 编译安装 Nginx] ****************************************************************************************************************
  14. changed: [192.168.77.172]
  15. TASK [nginx : 编写 Nginx 启动文件] *************************************************************************************************************
  16. changed: [192.168.77.172]
  17. TASK [nginx : 编写 Nginx 配置文件] *************************************************************************************************************
  18. changed: [192.168.77.172]
  19. TASK [nginx : 检查 Nginx 配置文件语法] ***********************************************************************************************************
  20. ok: [192.168.77.172]
  21. TASK [nginx : 启动 Nginx 服务并设置开机自启] ********************************************************************************************************
  22. changed: [192.168.77.172]
  23. TASK [nginx : 删除 Nginx 压缩包] **************************************************************************************************************
  24. changed: [192.168.77.172]
  25. RUNNING HANDLER [nginx : daemon-reload] **************************************************************************************************
  26. ok: [192.168.77.172]
  27. RUNNING HANDLER [reload nginx] ***********************************************************************************************************
  28. changed: [192.168.77.172]
  29. PLAY RECAP *******************************************************************************************************************************
  30. 192.168.77.172             : ok=13   changed=10   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
复制代码

检察服务启动状态

  1. [root@host1 ~]# systemctl status nginx
复制代码

2.管理设置文件

生产情况中大多时间是必要管理设置文件的,安装软件包只是在初始化情况的时间用一下。下面我们来写个管理nginx设置文件的playbook。
(1).情况预备

创建脚色目次布局
  1. [root@server ~]# mkdir -p /etc/ansible/nginx_config/roles/{new,old}/{files,handlers,vars,tasks}
复制代码

(2).文件内容

定义设置文件

new目次
  1. [root@server ~]# vi /etc/ansible/nginx_config/roles/new/tasks/main.yml
  2. [root@server ~]# cat /etc/ansible/nginx_config/roles/new/tasks/main.yml
  3. - name: copy conf file
  4.   copy: src="{{ item.src }}" dest="{{ nginx_basedir }}/{{ item.dest }}" backup=yes owner=root group=root mode=0644
  5.   with_items:
  6.     - { src: '/etc/ansible/nginx_config/roles/new/files/nginx.conf' , dest: 'conf/nginx.conf' }
  7.     - { src: '/etc/ansible/nginx_config/roles/new/files/vhosts' , dest: 'conf/vhosts' }
  8.   notify: restart nginx
复制代码

old目次
  1. [root@server ~]# vi /etc/ansible/nginx_config/roles/old/tasks/main.yml
  2. [root@server ~]# cat /etc/ansible/nginx_config/roles/old/tasks/main.yml
  3. - name: copy conf file
  4.   copy: src="{{ item.src }}" dest="{{ nginx_basedir }}/{{ item.dest }}" backup=yes owner=root group=root mode=0644
  5.   with_items:
  6.     - { src: '/etc/ansible/nginx_config/roles/old/files/nginx.conf' , dest: 'conf/nginx.conf' }
  7.     - { src: '/etc/ansible/nginx_config/roles/old/files/vhosts' , dest: 'conf/vhosts' }
  8.   notify: restart nginx
复制代码

定义files目次下内容

new目次和old目次都要设置
把nginx.conf和vhosts目次放到files目次下面
  1. [root@server ~]# cd /usr/local/nginx/conf/
  2. [root@server conf]# cp -r nginx.conf vhosts /etc/ansible/nginx_config/roles/new/files/
  3. [root@server conf]# ls /etc/ansible/nginx_config/roles/new/files/
  4. nginx.conf  vhosts
复制代码

定义变量

new目次和old目次都要设置
  1. [root@server ~]# vi /etc/ansible/nginx_config/roles/new/vars/main.yml
  2. [root@server ~]# cat /etc/ansible/nginx_config/roles/new/vars/main.yml
  3. nginx_basedir: /usr/local/nginx
复制代码

定义触发

  1. [root@server ~]# vi /etc/ansible/nginx_config/roles/new/handlers/main.yml
  2. [root@server ~]# cat /etc/ansible/nginx_config/roles/new/handlers/main.yml
  3. - name: restart nginx
  4.   systemd:
  5.     name: nginx
  6.     state: restarted
复制代码

定义剧本文件

new目次
  1. [root@server ~]# vi /etc/ansible/nginx_config/update.yml
  2. [root@server ~]# cat /etc/ansible/nginx_config/update.yml
  3. ---
  4. - hosts: host1
  5.   remote_user: root
  6.   roles:
  7.     - new
复制代码

old目次
  1. [root@server ~]# vi /etc/ansible/nginx_config/backup.yml
  2. [root@server ~]# cat /etc/ansible/nginx_config/backup.yml
  3. ---
  4. - hosts: host1
  5.   remote_user: root
  6.   roles:
  7.     - old
复制代码

定义之后的脚色布局

  1. [root@server ~]# tree /etc/ansible/nginx_config/
  2. /etc/ansible/nginx_config/
  3. ├── backup.yml
  4. ├── roles
  5. │   ├── new
  6. │   │   ├── files
  7. │   │   │   ├── nginx.conf
  8. │   │   │   └── vhosts
  9. │   │   ├── handlers
  10. │   │   │   └── main.yml
  11. │   │   ├── tasks
  12. │   │   │   └── main.yml
  13. │   │   └── vars
  14. │   │       └── main.yml
  15. │   └── old
  16. │       ├── files
  17. │       │   ├── nginx.conf
  18. │       │   └── vhosts
  19. │       ├── handlers
  20. │       │   └── main.yml
  21. │       ├── tasks
  22. │       │   └── main.yml
  23. │       └── vars
  24. │           └── main.yml
  25. └── update.yml
复制代码

其中new为更新时用到的,old为回滚时用到的,files下面为nginx.conf和vhosts目次,handlers为重启nginx服务的下令,tasks为实行的使命,vars为定义的变量。
(3).实行文件

在实行update.yml前,应备份当前设置文件,当实行之后发现错误,则进行回滚操纵。下令如下:
   实行update.yml文件之前一定要使用rsync下令备份设置文件
  回滚操纵就是把旧的设置覆盖,然后重新加载nginx服务, 每次改动nginx设置文件之前先备份到old里,对应目次为/etc/ansible/nginx_config/roles/old/files。
  1. [root@server ~]# rsync -av /etc/ansible/nginx_config/roles/new/files/ /etc/ansible/nginx_config/roles/old/files/
  2. sending incremental file list
  3. ./
  4. nginx.conf
  5. sent 2,807 bytes  received 39 bytes  5,692.00 bytes/sec
  6. total size is 2,655  speedup is 0.93
复制代码

修改new/files目次下的nginx.conf设置文件内容为123
  1. [root@server ~]# echo "123" > /etc/ansible/nginx_config/roles/new/files/nginx.conf
  2. [root@server ~]# cat /etc/ansible/nginx_config/roles/new/files/nginx.conf
  3. 123
复制代码

然后实行update.yml文件
  1. [root@server ~]# ansible-playbook /etc/ansible/nginx_config/update.yml
  2. PLAY [host1] ************************************************************************************************************************
  3. TASK [Gathering Facts] **************************************************************************************************************
  4. ok: [192.168.77.172]
  5. TASK [new : copy conf file] *********************************************************************************************************
  6. changed: [192.168.77.172] => (item={u'dest': u'conf/nginx.conf', u'src': u'/etc/ansible/nginx_config/roles/new/files/nginx.conf'})
  7. ok: [192.168.77.172] => (item={u'dest': u'conf/vhosts', u'src': u'/etc/ansible/nginx_config/roles/new/files/vhosts'})
  8. RUNNING HANDLER [new : restart nginx] ***********************************************************************************************
  9. fatal: [192.168.77.172]: FAILED! => {"changed": false, "msg": "Unable to restart service nginx: Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.\n"}
  10. NO MORE HOSTS LEFT ******************************************************************************************************************
  11. PLAY RECAP **************************************************************************************************************************
  12. 192.168.77.172             : ok=2    changed=1    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
复制代码

由于设置了错误的nginx设置文件以是nginx服务重启不了报错了,我们这时间想把设置文件还原必要实行backup.yml文件
  1. [root@server ~]# ansible-playbook /etc/ansible/nginx_config/backup.yml
  2. PLAY [host1] ************************************************************************************************************************
  3. TASK [Gathering Facts] **************************************************************************************************************
  4. ok: [192.168.77.172]
  5. TASK [old : copy conf file] *********************************************************************************************************
  6. changed: [192.168.77.172] => (item={u'dest': u'conf/nginx.conf', u'src': u'/etc/ansible/nginx_config/roles/old/files/nginx.conf'})
  7. ok: [192.168.77.172] => (item={u'dest': u'conf/vhosts', u'src': u'/etc/ansible/nginx_config/roles/old/files/vhosts'})
  8. RUNNING HANDLER [old : restart nginx] ***********************************************************************************************
  9. changed: [192.168.77.172]
  10. PLAY RECAP **************************************************************************************************************************
  11. 192.168.77.172             : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
复制代码

我们这样就把设置文件还原到实行update.yml文件之前的样子了。
至此Ansible-运维实战部门结束。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4