ansible

ansible

ansible具体的安装和命令可以百度,下面以一个例子来说明如何利用ansble进行自动化远程部署。

playbook实战–nginx安装

思路:

  1. 先在一台机器上编译安装好nginx、打包,

  2. 然后再用ansible去下发

    cd /etc/ansible

进入ansible配置文件目录

   mkdir  nginx_install 
创建一个nginx_install的目录,方便管理  
   cd nginx_install  mkdir -p roles/{common,install}/{handlers,files,meta,tasks,templates,vars}  

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

需要事先准备好安装用到的文件,具体如下:

在一台机器上事先编译安装好nginx,配置好启动脚本,配置好配置文件

安装好后,我们需要把nginx目录打包,并放到/etc/ansible/nginx_install/roles/install/files/下面,名字为nginx.tar.gz

启动脚本、配置文件都要放到/etc/ansible/nginx_install/roles/install/templates下面

cd /etc/ansible/nginx_install/roles

定义common的tasks,nginx是需要一些依赖包的

vim ./common/tasks/main.yml

内容如下 -

name: Install initializtion require software yum: name={{ item }} state=installed with_items: - zlib-devel - pcre-devel

[root@shuai-01 ansible]# mkdir nginx_install
[root@shuai-01 ansible]# cd nginx_install/
[root@shuai-01 nginx_install]# mkdir -p roles/{common,install}/{files,handles,mate,tasks,templates,vars}
[root@shuai-01 nginx_install]# ls
roles
[root@shuai-01 nginx_install]# ls roles/
common  install
[root@shuai-01 nginx_install]# ls roles/install/
files  handles  mate  tasks  templates  vars
[root@shuai-01 nginx_install]# ls roles/common/
files  handles  mate  tasks  templates  vars
 
 
将Nginx打包,配置文件复制到相应的目录下
 
[root@shuai-01 local]# tar czvf nginx.tar.gz --exclude "nginx.conf" --exclude "vhost" /usr/local/nginx
[root@shuai-01 local]# mv nginx.tar.gz /etc/ansible/nginx_install/roles/install/files/
[root@shuai-01 local]# cp nginx/conf/nginx.conf /etc/ansible/nginx_install/roles/install/templates/
[root@shuai-01 local]# cp /etc/init.d/nginx /etc/ansible/nginx_install/roles/install/templates/
 
 
common目录下编辑一个安装依赖包的task
 
[root@shuai-01 common]# vim tasks/main.yml
- name: Install initializtion require software
  yum: name="zlib-devel,pcre-devel" state=installed
 
 
定义需要的变量
 
[root@shuai-01 install]# vim vars/main.yml
nginx_user: www
nginx_port: 80
nginx_basedir: /usr/local/nginx
 
 
写拷贝的yml
 
[root@shuai-01 install]# vim 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
 
 
写建立用户,启动,服务管理的yml
 
[root@shuai-01 install]# vim 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
 
 
一个yml将copy.yml,install.yml包含进去
 
[root@shuai-01 install]# vim tasks/main.yml
- include: copy.yml
- include: install.yml
 
 
一个调用的主yml
 
[root@shuai-01 install]# vim /etc/ansible/nginx_install/install.yml
---
- hosts: testhost
  remote_user: root
  gather_facts: True
  roles:
    - common
    - install
 
 
[root@shuai-01 common]# ansible-playbook /etc/ansible/nginx_install/install.yml 
 
 
 
执行结果
[root@shuai-01 common]# ansible-playbook /etc/ansible/nginx_install/install.yml 
 
PLAY [shuai-02] *************************************************************************
 
TASK [Gathering Facts] ******************************************************************
ok: [shuai-02]
 
TASK [common : Install initializtion require software] **********************************
ok: [shuai-02]
 
TASK [install : Copy Nginx Software] ****************************************************
changed: [shuai-02]
 
TASK [install : Uncompression Nginx Software] *******************************************
 [WARNING]: Consider using the unarchive module rather than running 'tar'.  If you need
to use command because unarchive is insufficient you can add 'warn: false' to this
command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
 
changed: [shuai-02]
 
TASK [install : Copy Nginx Start Script] ************************************************
changed: [shuai-02]
 
TASK [install : Copy Nginx Config] ******************************************************
changed: [shuai-02]
 
TASK [install : Create Nginx User] ******************************************************
changed: [shuai-02]
 
TASK [install : Start Nginx Service] ****************************************************
changed: [shuai-02]
 
TASK [install : Add Boot Start Nginx Service] *******************************************
changed: [shuai-02]
 
TASK [install : Delete Nginx compression files] *****************************************
 [WARNING]: Consider using the file module with state=absent rather than running 'rm'.
If you need to use command because file is insufficient you can add 'warn: false' to
this command task or set 'command_warnings=False' in ansible.cfg to get rid of this
message.
 
changed: [shuai-02]
 
PLAY RECAP ******************************************************************************
shuai-02                   : ok=10   changed=8    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
 
 
 
[root@shuai-02 ~]# ps aux |grep nginx
root       3051  0.0  0.0  46312   952 ?        Ss   17:58   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx      3052  0.0  0.1  46696  1912 ?        S    17:58   0:00 nginx: worker process
root       3181  0.0  0.0 112680   976 pts/0    R+   18:00   0:00 grep --color=auto ngin
 
 

[root@shuai-01 common]# ansible-playbook /etc/ansible/nginx_install/install.yml

执行结果

[root@shuai-01 common]# ansible-playbook /etc/ansible/nginx_install/install.yml 

PLAY [shuai-02] *************************************************************************

TASK [Gathering Facts] ******************************************************************
ok: [shuai-02]

TASK [common : Install initializtion require software] **********************************
ok: [shuai-02]

TASK [install : Copy Nginx Software] ****************************************************
changed: [shuai-02]

TASK [install : Uncompression Nginx Software] *******************************************
 [WARNING]: Consider using the unarchive module rather than running 'tar'.  If you need
to use command because unarchive is insufficient you can add 'warn: false' to this
command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.

changed: [shuai-02]

TASK [install : Copy Nginx Start Script] ************************************************
changed: [shuai-02]

TASK [install : Copy Nginx Config] ******************************************************
changed: [shuai-02]

TASK [install : Create Nginx User] ******************************************************
changed: [shuai-02]

TASK [install : Start Nginx Service] ****************************************************
changed: [shuai-02]

TASK [install : Add Boot Start Nginx Service] *******************************************
changed: [shuai-02]

TASK [install : Delete Nginx compression files] *****************************************
 [WARNING]: Consider using the file module with state=absent rather than running 'rm'.
If you need to use command because file is insufficient you can add 'warn: false' to
this command task or set 'command_warnings=False' in ansible.cfg to get rid of this
message.

changed: [shuai-02]

PLAY RECAP ******************************************************************************
shuai-02                   : ok=10   changed=8    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

 

[root@shuai-02 ~]# ps aux |grep nginx
root       3051  0.0  0.0  46312   952 ?        Ss   17:58   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx      3052  0.0  0.1  46696  1912 ?        S    17:58   0:00 nginx: worker process
root       3181  0.0  0.0 112680   976 pts/0    R+   18:00   0:00 grep --color=auto ngin

img

这些信息有用吗?
Do you have any suggestions for improvement?

Thanks for your feedback!