码迷,mamicode.com
首页 > 其他好文 > 详细

ansible- 安装nginx

时间:2016-06-12 15:32:15      阅读:482      评论:0      收藏:0      [点我收藏+]

标签:software   配置文件   require   实际应用   common   

实际应用- 安装nginx

cd  /etc/ansible
mkdir nginx_install
mkdir  -p nginx_install/roles/{common,delete,install}/{handlers,files,meta,tasks,templates,vars}

说明:roles目录下有三个角色,common为一些准备操作,delete为删除nginx的操作,install为安装nginx的操作

每个角色下面又有几个目录,handlers下面是当发生改变时要执行的操作,通常用在配置文件发生改变,重启服务。files为安装时用到的一些文件,meta为说明信息,说明角色依赖等信息,tasks里面是核心的配置文件,templates通常存一些配置文件,启动脚本等模板文件,vars下为定义的变量

cd  nginx_install/roles

vim  ./common/tasks/main.yml //内容如下
- name: Install initializtion require software
  yum: name={{ item }} state=installed
  with_items:
    - gcc
    - zlib-devel
    - pcre-devel
vim  ./install/vars/main.yml
nginx_user: www
nginx_port: 80
nginx_web_dir: /data/www
nginx_version: 1.4.3

ls ./install/files/
nginx-1.4.3.tar.gz

说明: 我们需要把源码包放到 files目录里面

ls ./install/templates
index.html index.php install_nginx.sh nginx nginx.conf vhost.conf

说明: 需要再templates下面准备好 默认页、安装nginx的shell脚本、nginx启动脚本、nginx配置文件以及虚拟主机配置文件

技术分享


vim ./install/tasks/copy.yml
  - name: Copy Nginx Software To Redhat Client
    copy: src=nginx-{{ nginx_version }}.tar.gz dest=/tmp/nginx-{{ nginx_version }}.tar.gz owner=root group=root
    when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
  - name: Uncompression Nginx Software To Redhat Client
    shell: tar zxf /tmp/nginx-{{ nginx_version }}.tar.gz -C /usr/local/
    when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
  - name: Copy Nginx Start Script To Redhat Client
    template: src=nginx dest=/etc/init.d/nginx owner=root group=root mode=0755
    when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
  - name: Copy Nginx Config To Redhat Client
    template: src=nginx.conf dest=/usr/local/nginx-{{ nginx_version }}/conf/ owner=root group=root mode=0644
    when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
  - name: Copy Nginx Vhost Config to RedHat Client
    template: src=vhost.conf dest=/usr/local/nginx-{{ nginx_version }}/conf/vhost/ owner=root group=root mode=0644
    when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6


说明: 首先把压缩包拷贝到/tmp/目录下,然后解压到/usr/local/下,再拷贝启动脚本到/etc/init.d/下,再拷贝nginx.conf以及vhost.conf

vim ./install/tasks/install.yml
  - name: Create Nginx User In Redhat Client
    user: name={{ nginx_user }} state=present createhome=no shell=/sbin/nologin
    when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
  - name: Create Nginx Dir
    file: dest={{ nginx_web_dir }}/{{ item }} state=directory
    with_items:
      - vhost
      - logs
    when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
  - name: Modify Nginx Dir Permission In Redhat Client
    file: path={{ item }} owner={{ nginx_user }} group={{ nginx_user }} mode=0755
    with_items:
      - "{{ nginx_web_dir }}"
      - /usr/local/nginx-{{ nginx_version }}
    when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
  - name: Create Index Html To Redhat Client
    template: src=index.html dest={{ nginx_web_dir }}/vhost/index.html owner={{ nginx_user }} group={{ nginx_user }} mode=0644
    when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
  - name: Install Check Script In Redhat Client
    template: src=index.php dest={{ nginx_web_dir }}/vhost/ owner={{ nginx_user }} group={{ nginx_user }} mode=0644
    when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
  - name: Start Nginx Service In Redhat Client
    service: name=nginx state=restarted
    when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
  - name: Add Boot Start Nginx Service In Redhat Client
    shell: chkconfig --level 345 nginx on
    when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

vim ./install/tasks/delete.yml
  - name: Delete Nginx compression Software In Redhat Client
    shell: rm -rf /tmp/nginx-{{ nginx_version }}.tar.gz
    when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

vim ./install/tasks/main.yml
- include: copy.yml
- include: install.yml
- include: delete.yml

vim  ./delete/vars/main.yml
nginx_user: www
nginx_port: 80
nginx_web_dir: /data/webroot/nginx
nginx_version: 1.4.3

vim ./delete/tasks/main.yml
- include: delete.yml

vim ./delete/tasks/delete.yml
  - name: stop nginx service
    shell: ps -ef|grep nginx|grep -v grep|awk ‘{print $2}‘|xargs kill -9 >>/dev/null 2>&1
    ignore_errors: yes
    when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
  - name: Delete Nginx Boot Start Script
    shell: chkconfig --del nginx
    ignore_errors: yes
    when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
  - name: Delete Nginx Dir
    shell: rm -rf /usr/local/nginx-{{ nginx_version }}
    when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
  - name: Delete Nginx User
    shell: userdel {{ nginx_user }}
    ignore_errors: yes
    when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
  - name: Delete Nginx Service Start Script
    shell: rm -rf /etc/init.d/nginx
    when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

cd ../
vim install.yml
---
- hosts: testhost
  remote_user: root
  gather_facts: True
  roles:
    - common
    - install

vim delete.yml
---
- hosts: testhost
  remote_user: root
  gather_facts: True
  roles:
    - delete

安装nginx:  ansible-playbook  install.yml
删除nginx: ansible-playbook  delete.yml


下载整个样例库   
git clone git://github.com/dl528888/ansible-examples.git

git命令,需要yum先安装一下: yum install -y git


ansible- 安装nginx

标签:software   配置文件   require   实际应用   common   

原文地址:http://xiongrunchu.blog.51cto.com/11696174/1788221

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!