1、使用说明:nginx

   以前一直用的saltstack自动化管理工具,须要维护客户端,而去批量执行时网络延迟也是个很头疼的问题,后来接触到了ansible这个工具,不须要安装客户端,走的ssh的加密协议,比较方便。今天这里说一下使用ansible自动化安装nginx的操做。shell


2、实验环境:vim

 3台服务器(centos 6.5 final版本):centos

192.168.1.193(ansible服务端)
192.168.1.194(安装nignx)
192.168.1.195(安装nignx)


3、安装配置:bash

 在192.168.1.193上面安装ansible,能够直接用yum直接安装,安装以前,须要给yum添加个epel的源。服务器

rpm -ivh http://mirrors.ustc.edu.cn/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm

 安装好epel以后,就能够直接用yum安装ansible了网络

yum install -y ansible


 装好ansible,其它都不须要修改,只须要将被控服务器添加到hosts文件里面就好。app

vim /etc/ansible/hosts

内容为:
[testhosts]
192.168.1.194
192.168.1.195


 由于ansible经过ssh来控制被控服务器,因此想要在控制服务器时不须要输入密码,那么就须要把192.168.1.193的公钥放置到被控服务器上面。操做方法以下:ssh

 先在192.168.1.193上面生成密钥:ide

ssh-keygen -t rsa -P ''

 一直enter键就好了,将生成一个公钥和私钥。-P '' 表示空密码。

 而后将公钥copy至被控服务器,并写入authorized_keys,这能够用一个命令一次性搞定:

ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.1.194
ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.1.195


 ok,到这里就能够用ansible测试一下了。

ansible testhosts -m ping

结果为:
192.168.1.194 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
192.168.1.195 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

 表示ansible能够无需输入密码控制194和195两台服务器了。



 下面书写playbook来自动化安装nginx到194和195两台服务器上:

[root@localhost playbook]# ls
base-install.yml  nginx-install-common.yml


 base-install.yml内容为:

---
#check or madir in remote server.
- name: check if the dir software existed, if not existed, then mkdir it.
  file:
    path: "{
   
   
   
   { base_dir }}/auto_deploy/software"
    owner: root
    group: root
    mode: 0755
    state: directory
- name: check if the dir config_file existed, if not existed, then mkdir it.
  file:
    path: "{
   
   
   
   { base_dir }}/auto_deploy/config_file"
    owner: root
    group: root
    mode: 0755
    state: directory
- name: check if the dir install_log existed, if not existed, then mkdir it.
  file:
    path: "{
   
   
   
   { base_dir }}/auto_deploy/install_log"
    owner: root
    group: root
    mode: 0755
    state: directory


 nginx-install-common.yml内容为:

---
- hosts: 192.168.1.194 192.168.1.195
  gather_facts: no
  remote_user: root
  #become_method: sudo
  #become_user: root
  vars:
  - base_dir: "/home/workspace"
  - install_dir: "/usr/local/rktsapps"
  tasks:
  - include: base-install.yml
  - name: copy nginx dir to remote servers.
    copy:
      src: "{
   
   
   
   { base_dir }}/ansible_home/auto_deploy/software/nginx"
      dest: "{
   
   
   
   { base_dir }}/auto_deploy/software/"
  - name: copy nginx conf to remote servers.
    copy:
      src: "{
   
   
   
   { base_dir }}/ansible_home/auto_deploy/config_file/nginx"
      dest: "{
   
   
   
   { base_dir }}/auto_deploy/config_file/"
  - name: copy install script to remote servers.
    copy:
      src: "{
   
   
   
   { base_dir }}/ansible_home/auto_deploy/auto_install.sh"
      dest: "{
   
   
   
   { base_dir }}/auto_deploy/"
  - name: test if nginx has been installed in remote server.
    shell: ls "{
   
   
   
   { install_dir }}/nginx/sbin/nginx"
    ignore_errors: True
    register: result
  - name: execute shell script to install nginx in remote servers.
    shell: sh auto_install.sh nginx
    ignore_errors: True
    args:
      chdir: "{
   
   
   
   { base_dir }}/auto_deploy/"
    when: result|failed 
  - name: delete auto_install.sh .
    file:
      path: "{
   
   
   
   { base_dir }}/auto_deploy/auto_install.sh"
      state: absent


 而后执行

ansible-playbook nginx-install-common.yml --syntax-check

 这里只是检查一下yml的语法是否正确,并无正确的执行。

 检查无误,再执行:

ansible-playbook nginx-install-common.yml

 就能够执行nginx的安装操做了。大功告成。



4、注意说明:


  •  这里主要是为了让你们熟悉ansible的安装,初始化配置,已经playbook的语法

  •  ansible在这里作的主要是创建目录,从193上面复制一些nginx安装文件,配置文件到194和195上面

  •  主要的安装过程在auto_instal.sh脚本里面,也就是新建用户,编译安装,添加配置文件的一些过程。