zoukankan      html  css  js  c++  java
  • Ansible 使用 Playbook 安装 Nginx

    思路:先在一台机器上编译安装好 Nginx,打包,然后通过 Ansible 下发

    [root@localhost ~]$ cd /etc/ansible/
    [root@localhost ansible]$ mkdir nginx_install
    [root@localhost ansible]$ cd nginx_install/
    [root@localhost nginx_install]$ mkdir -p roles/{common,install}/{handlers,files,meta,tasks,templates,vars}
    [root@localhost nginx_install]$ tree ./
    ./
    └── roles                # 定义角色目录,common为一些准备操作,install为安装nginx的操作    
        ├── common
        │   ├── files        # 存放安装包的目录
        │   ├── handlers     # 存放当发生变更是要执行的操作,如修改配置文件、重启服务等
        │   ├── meta         # 存放说明信息,如说明角色依赖等信息
        │   ├── tasks        # 存放核心的任务配置文件
        │   ├── templates    # 存放配置文件、启动文件等模板文件
        │   └── vars         # 用来定义变量的目录
        └── install
            ├── files
            ├── handlers
            ├── meta
            ├── tasks
            ├── templates
            └── vars

    编译安装 Nginx :

    [root@localhost ~]$ cd /usr/local/src/
    [root@localhost ~]$ yum install -y pcre-devel zlib-devel gcc gcc-c++
    [root@localhost src]$ wget http://nginx.org/download/nginx-1.12.2.tar.gz
    [root@localhost src]$ tar xf nginx-1.12.2.tar.gz 
    [root@localhost src]$ cd nginx-1.12.2/
    [root@localhost nginx-1.12.2]$ ./configure --prefix=/usr/local/nginx
    [root@localhost nginx-1.12.2]$ make && make install
    [root@localhost nginx-1.12.2]$ vim /etc/init.d/nginx          # https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/etc_init.d_nginx
    [root@localhost nginx-1.12.2]$ chmod 755 /etc/init.d/nginx
    [root@localhost nginx-1.12.2]$ chkconfig --add nginx
    [root@localhost nginx-1.12.2]$ chkconfig nginx on
    [root@localhost nginx-1.12.2]$ cd /usr/local/nginx/conf/
    [root@localhost conf]$ mv nginx.conf nginx.conf.bak
    [root@localhost conf]$ vim nginx.conf                         # https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/nginx.conf
    [root@localhost conf]$ /usr/local/nginx/sbin/nginx -t
    [root@localhost conf]$ /etc/init.d/nginx start
    [root@localhost conf]$ netstat -tunlp | grep 80

    打包:

    [root@localhost ~]$ cd /etc/ansible/nginx_install/roles/install/files/
    [root@localhost files]$ tar czf nginx.tar.gz --exclude "nginx.conf" --exclude "vhost" /usr/local/nginx

    拷贝配置文件和启动文件到模板目录:

    [root@localhost ~]$ cp /usr/local/nginx/conf/nginx.conf /etc/ansible/nginx_install/roles/install/templates/
    [root@localhost ~]$ cp /etc/init.d/nginx /etc/ansible/nginx_install/roles/install/templates/

    安装一些依赖包:

    [root@localhost ~]$ cd /etc/ansible/nginx_install/roles/
    [root@localhost roles]$ cat common/tasks/main.yml - name: Install initializtion require software yum: name={{ item }} state=installed with_items: - zlib-devel - pcre-devel

    定义一些变量:

    [root@localhost roles]$ cat install/vars/main.yml 
    nginx_user: www
    nginx_port: 80
    nginx_basedir: /usr/local/nginx

    把用到的文档拷贝到目标机器:

    [root@localhost roles]$ cat install/tasks/copy.yml 
    - name: Copy Nginx Software
      copy: src=nginx.tar.gz dest=/tmp/nginx.tar.gz owner=root group=root
    
    - name: Uncompression Nginx Software
      shell: tar zxf /tmp/nginx.tar.gz -C /usr/local/
    
    - name: Copy Nginx Start Script
      template: src=nginx dest=/etc/init.d/nginx owner=root group=root mode=0755
    
    - name: Copy Nginx Config
      template: src=nginx.conf dest={{ nginx_basedir }}/conf/ owner=root group=root mode=0644

    创建用户 ,启动服务 ,删除压缩包:

    [root@localhost roles]$ cat install/tasks/install.yml
    - name: Create Nginx User
      user: name={{ nginx_user }} state=present createhome=no shell=/sbin/nologin
    
    - name: Start Nginx Service
      shell: /etc/init.d/nginx start
    
    - name: Add Boot Start Nginx Service
      shell: chkconfig --level 345 nginx on
    
    - name: Delete Nginx compression files
      shell: rm -rf /tmp/nginx.tar.gz

    创建 main.yml ,去调用 copy.yml 和 install.yml:

    [root@localhost roles]$ cat install/tasks/main.yml
    - include: copy.yml
    - include: install.yml

    最后,创建总入口文件:

    [root@localhost ~]$ cat /etc/ansible/nginx_install/install.yml
    ---
    - hosts: 192.168.119.134
      remote_user: root
      gather_facts: True
      roles:
        - common
        - install

    执行 yaml 安装 Nginx:

    [root@localhost ~]$ ansible-playbook /etc/ansible/nginx_install/install.yml 

          

  • 相关阅读:
    Selenium+unittest(4)生成html报告带截图
    Selenium+unittest(2)配置模块介绍
    python+requests+unittest(1)接口自动化测试框架结构介绍
    Selenium+unittest(1)web自动化整体框架介绍
    App自动化测试
    robotframework自动化测试框架搭建及问题汇总
    QTP11完美破解小笔记
    Loadrunner11完美破解小笔记
    【转】微信小程序专项测试
    【转】如何测试微信应用号
  • 原文地址:https://www.cnblogs.com/pzk7788/p/10221130.html
Copyright © 2011-2022 走看看