yrtew 发表于 2017-3-20 10:20:40

ansible之playbook自动化安装nginx

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


二、实验环境:
3台服务器(centos 6.5 final版本):

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





三、安装配置:
在192.168.1.193上面安装ansible,可以直接用yum直接安装,安装之前,需要给yum添加个epel的源。

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




安装好epel之后,就可以直接用yum安装ansible了

1
yum install -y ansible





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

1
2
3
4
5
6
vim /etc/ansible/hosts

内容为:

192.168.1.194
192.168.1.195





因为ansible通过ssh来控制被控服务器,所以想要在控制服务器时不需要输入密码,那么就需要把192.168.1.193的公钥放置到被控服务器上面。操作方法如下:
先在192.168.1.193上面生成密钥:

1
ssh-keygen -t rsa -P ''




一直enter键就行了,将生成一个公钥和私钥。-P '' 表示空密码。
然后将公钥copy至被控服务器,并写入authorized_keys,这可以用一个命令一次性搞定:

1
2
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测试一下了。

1
2
3
4
5
6
7
8
9
10
11
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两台服务器上:

1
2
# ls
base-install.ymlnginx-install-common.yml





base-install.yml内容为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
---
#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内容为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
---
- 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=\'#\'" /ansible_home/auto_deploy/software/nginx"
      dest: "{{ base_dir }}/auto_deploy/software/"
- name: copy nginx conf to remote servers.
    copy:
      src=\'#\'" /ansible_home/auto_deploy/config_file/nginx"
      dest: "{{ base_dir }}/auto_deploy/config_file/"
- name: copy install script to remote servers.
    copy:
      src=\'#\'" /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





然后执行

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




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

1
ansible-playbook nginx-install-common.yml




就可以执行nginx的安装操作了。大功告成。


四、注意说明:


[*] 这里主要是为了让大家熟悉ansible的安装,初始化配置,已经playbook的语法

[*] ansible在这里做的主要是建立目录,从193上面复制一些nginx安装文件,配置文件到194和195上面

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


bingoz 发表于 2017-4-13 18:00:33

Zabbix企业级分布式监控系统
页: [1]
查看完整版本: ansible之playbook自动化安装nginx