思路:先在一台機器上編譯安裝好 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