ansible自动运维

自动运维—Ansible

 

 

一、Ansible安装

(1)环境安装

准备两台机器,都要关闭防火墙和SELinux,并修改/etc/hosts文件

 

 

 

 

 

将SELinux修改成SELinux=disabled

添加两台主机的IP和主机名

 

(2)安装Ansible

两台机器,只需要在ansible1上安装Ansible,先安装epel仓库

 

 

 

(3)免密设置

ansible1上生成密钥对ssh-keygen -t rsa,把公钥放到ansible2上,设置密钥认证        *注意:需要本机也配置密钥 ssh-copy-id 本机IP

 

 

 

 

 

(4)主机组设置

在/etc/ansible/hosts文件中添加本机和另一台机器的IP

 

 

 

二、Ansible远程执行命令

这样就可以批量执行命令了。这里的testhost为主机组名,-m后边是模块名字,-a后面是命令。当然我们也可以直接写一个IP,针对某一台机器来执行命令。

 

 

 

也可以直接写一个IP,针对某一台机器来执行命令

 

Shell模块

还有一个模块就是shell同样也可以实现 (shell模块一般用来远程执行一个shell脚本)

ansible  testhost -m shell -a 'w'

 

 

三、Ansible拷贝文件或者目录

拷贝目录到一台机器

源目录会放到目标目录下面去,如果目标指定的目录不存在,它会自动创建。如果拷贝的是文件,如果dest指定的名字和源不同,并且它不是已经存在的目录,相当于拷贝过去后又重命名。但相反,如果desc是目标机器上已经存在的目录,则会直接把文件拷贝到该目录下面。

ansible 192.168.200.7 -m copy -a "src=/etc/ansible  dest=/tmp/ansibletest owner=root group=root mode=0755"

(-m copy模块拷贝文件或目录,src指定来源的文件目录,owner指定目录的属主,group属组,mode指定权限)

 

 

 

 

 

四、Ansible远程执行脚本

先创建一个shell脚本

 

然后把该脚本分发到各个机器上

 

最后是批量执行该shell脚本。

 

 

 

五、Ansible管理任务计划

创建任务计划,命名并定义工作

 

若要删除cron只需要加一个字段,state=absent

crontab   -e    查看任务管理记录

 

可以看到jobs里面没有内容了

其他的时间表示——分钟:minute;小时:hour;日期:day;月份:month。

 

 

六、Ansible安装RPM包/管理任务

使用Yum模块安装httpd服务

 

在name后面还可以加上state=installed/removed。

设置服务状态,这里的name是CentOS系统里的服务名,可以通过chkconfig –list命令查到。

 

查看服务状态

 

 

 

可以看到通过ansible来批量安装某服务并且管理服务

**ansible-doc -l   列出所有的模块

**ansible-doc cron  查看指定模块的文档

 

 

 

 

七、Ansible playbook中的使用

 

说明:第一行需要有三个杠,hosts参数指定了对哪些主机进行参作,如果是多台机器可以用逗号作为分隔,也可以使用主机组,在/etc/ansible/hosts里定义,user参数指定了使用什么用户登录远程主机操作,tasks指定了一个任务,其下面的name参数同样是对任务的描述,在执行过程中会打印出来,shell是ansible模块名字

 

再来一个创建用户的例子:

 

 

 

可以看到 到TASK时不用执行setup模块了

说明:name参数对该playbook实现的功能做一个概述,后面执行过程中,会打印 name变量的值 ,可以省略;gather_facts参数指定了在以下任务部分执行前,是否先执行setup模块获取主机相关信息,这在后面的task会使用到setup获取的信息时用到;vars参数,指定了变量,这里指字一个user变量,其值为test ,需要注意的是,变量值一定要用引号引住;user提定了调用user模块,name是user模块里的一个参数,而增加的用户名字调用了上面user变量的值。

 

 

八、Ansible playbook中的循环

创建while.yml文件

 

说明: with_items为循环的对象

创建文件       文件权限由644  变动  600

执行while.yml。

 

改变文件权限的循环语句

 

 

九、Ansible playbook中的条件判断

创建when.yml文件

 

判断网卡是否存在    若存在  执行任务

说明:ansible anisble-02 -m setup 可以查看到所有的facter信息

执行when.yml。

 

 

  

 

十、Ansible playbook中的handlers

执行task之后,服务器发生变化之后要执行的一些操作,比如我们修改了配置文件后,需要重启一下服务,创建handlers.yml文件加入如下内容

 

说明,只有copy模块真正执行后,才会去调用下面的handlers相关的操作。也就是说如果1.txt(源文件)和2.txt(目标文件)内容是一样的,并不会去执行handlers里面的shell相关命令。 这种比较适合配置文件发生更改后,重启服务的操作。

 

 

 

可以看到执行程序后目标文件追加了111111,若源文件和目标文件一样的话。就不会执行handlers模块

 

 

十一、Ansible playbook实战

ansible自动化安装nginx

1)编译安装nginx

1)使用wget下载nginx包,下载地址:

http://mirrors.sohu.com/nginx/nginx-1.9.6.tar.gz

yum install -y gcc gcc-c++ pcre-devel zlib-devel openssl-devel

两个节点安装依赖包

2)解压下载的nginx包

./configure --prefix=/usr/local/nginx

make && make install

3)编写/etc/init.d/nginx文件

内容如下:

#!/bin/bash

# chkconfig: - 30 21

# description: http service.

# Source Function Library

. /etc/init.d/functions

# Nginx Settings

NGINX_SBIN="/usr/local/nginx/sbin/nginx"

NGINX_CONF="/usr/local/nginx/conf/nginx.conf"

NGINX_PID="/usx/local/nginx/logs/nginx.pid"

RETVAL=0

prog="Nginx"

 

start()

{

        echo -n $"Starting $prog: "

        mkdir -p /dev/shm/nginx_temp

        daemon $NGINX_SBIN -c $NGINX_CONF

        RETVAL=$?

        echo

        return $RETVAL

}

stop()

{

        echo -n $"Stopping $prog: "

        killproc -p $NGINX_PID $NGINX_SBIN -TERM

        rm -rf /dev/shm/nginx_temp

        RETVAL=$?

        echo

        return $RETVAL

}

reload()

{

        echo -n $"Reloading $prog: "

        killproc -p $NGINX_PID $NGINX_SBIN -HUP

        RETVAL=$?

        echo

        return $RETVAL

}

restart()

{

        stop

        start

}

configtest()

{

        $NGINX_SBIN -c $NGINX_CONF -t

        return 0

}

case "$1" in

        start)

                start

                ;;

        stop)

                stop

                ;;

        reload)

                reload

                ;;

        restart)

                restart

                ;;

        configtest)

                configtest

                ;;

        *)

                echo $"Usage: $0 {start|stop|reload|restart|configtest}"

                RETVAL=1

esac

exit $RETVAL

4)清空配置文件并重新编写

# > /usr/local/nginx/conf/nginx.conf

内容如下:

user nobody nobody;        //定义nginx运行的用户和用户组

worker_processes 2;         //nginx进程数,一般为CPU总核心数

error_log /usr/local/nginx/logs/nginx_error.log crit;  //全局错误日志定义类型

pid /usr/local/nginx/logs/nginx.pid;    //进程文件

worker_rlimit_nofile 51200;

events        //工作模式与连接数上限

{

use epoll;

worker_connections 6000;

}

http            //http下的一些配置

{

include mime.types;          //文件扩展名与文件类型映射表

default_type application/octet-stream;            //默认文件类型

server_names_hash_bucket_size 3526;

server_names_hash_max_size 4096;

log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'

'$host "$request_uri" $status'

'"$http_referer" "$http_user_agent"';

sendfile on;           //开启高效文件传输模式

tcp_nopush on;           //防止网络阻塞

keepalive_timeout 30;      //长连接超时时间,单位为秒

client_header_timeout 3m;

client_body_timeout 3m;

send_timeout 3m;

connection_pool_size 256;

client_header_buffer_size 1k;

large_client_header_buffers 8 4k;

request_pool_size 4k;

output_buffers 4 32k;

postpone_output 1460;

client_max_body_size 10m;

client_body_buffer_size 256k;

client_body_temp_path /usr/local/nginx/client_body_temp;

proxy_temp_path /usr/local/nginx/proxy_temp;

fastcgi_temp_path /usr/local/nginx/fastcgi_temp;

fastcgi_intercept_errors on;

tcp_nodelay on;          //防止网络阻塞

gzip on;           //开启gzip压缩输出

gzip_min_length 1k;

gzip_buffers 4 8k;

gzip_comp_level 5;

gzip_http_version 1.1;

gzip_types text/plain application/x-javascript text/css text/htm

application/xml;

server        //虚拟主机配置

{

listen 80;

server_name localhost;

index index.html index.htm index.php;

root /usr/local/nginx/html;

location ~ \.php$

{

include fastcgi_params;

fastcgi_pass unix:/tmp/php-fcgi.sock;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME /usr/1ocal/nginx/html$fastcgi_script_name;

}

}

}

5)编写完成后可以检查一下

# /usr/local/nginx/sbin/nginx -t

 

6)启动nginx

# service nginx start

编译安装完成

 

 

 

查看端口  

 

发现80端口已经被https服务占了(另一台机器同理)

关闭https服务,并启动nginx服务 查看端口  nginx服务状态

 

要把这些文件打包

 

 

 

 

 

包已经打包完毕,并移动文件

说明: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是需要一些依赖包

定义变量

首先要把所有用到的文档拷贝到目标机器

第一个文件的正确书写

 

接下来会建立用户,启动服务,删除压缩包

再创建main.yml并且把copy和install调用

到此两个roles:common和install就定义完成了,接下来要定义一个入口配置文件

 

执行: ansible-playbook /etc/ansible/nginx_install/install.yml

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

 

PLAY [192.168.2.31] ****************************************************************************************************

 

TASK [Gathering Facts] *************************************************************************************************

ok: [192.168.2.31]

 

TASK [common : install initializtion requre software] ******************************************************************

[DEPRECATION WARNING]: Invoking "yum" only once while using a loop via squash_actions is deprecated. Instead of using a

 loop to supply multiple items and specifying `name: "{{ item }}"`, please use `name: ['zlib-devel', 'pcre-devel']` and

 remove the loop. This feature will be removed in version 2.11. Deprecation warnings can be disabled by setting

deprecation_warnings=False in ansible.cfg.

ok: [192.168.2.31] => (item=[u'zlib-devel', u'pcre-devel'])

 

TASK [install : Copy Nginx Software] ***********************************************************************************

changed: [192.168.2.31]

 

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: [192.168.2.31]

 

TASK [install : Copy Nginx Start Script] *******************************************************************************

ok: [192.168.2.31]

 

TASK [install : Copy Nginx Config] *************************************************************************************

ok: [192.168.2.31]

 

TASK [install : Create Nginx User] *************************************************************************************

ok: [192.168.2.31]

 

TASK [install : Start Nginx Service] ***********************************************************************************

changed: [192.168.2.31]

 

TASK [install : Add Boot start Nginx service] **************************************************************************

changed: [192.168.2.31]

 

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: [192.168.2.31]

 

PLAY RECAP *************************************************************************************************************

192.168.2.31               : ok=10   changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

 

 

十二、管理配置文件

生产环境中大多时候是需要管理配置文件的,安装软件包只是在初始化环境的时候用一下。下面我们来写个管理nginx配置文件的playbook。

[root@ansible2nginx_config]#cat

/etc/ansible/nginx_config/roles/new/handlers/main.yml

- name: restart nginx   //用于重新加载nginx服务

  shell: /etc/init.d/nginx reload

[root@ansible2nginx_config]# cat /etc/ansible/nginx_config/roles/new/tasks/main.yml

- name: copy conf file //复制.conf和hosts文件

  copy: src={{ item.src }} dest={{ nginx_basedir }}/{{ item.dest }} backup=yes owner=root group=root mode=0644

  with_items:

    - { src: nginx.conf, dest: conf/nginx.conf }

    - { src: vhosts, dest: conf/ }

  notify: restart nginx

[root@ansible2 nginx_config]# cat /etc/ansible/nginx_config/roles/new/vars/main.yml

nginx_basedir: /usr/local/nginx    //定义变量

[root@ansible2 nginx_config]# cat update.yml

---

- hosts: 192.168.2.101 //入口文件

  user: root

  roles:

  - new           //这里只有new

old目录中的yml文件与new目录中的相同,files中的配置文件不同。

其中new为更新时用到的,old为回滚时用到的,files下面为nginx.conf和vhosts目录,handlers为重启nginx服务的命令

在执行update.yml前,应备份当前配置文件,当执行之后发现错误,则进行回滚操作。关于回滚,需要在执行playbook之前先备份一下旧的配置,所以对于老配置文件的管理一定要严格,千万不能随便去修改线上机器的配置,并且要保证new/files下面的配置和线上的配置一致,命令如下:

# rsync -av /etc/ansible/nginx_config/roles/new/

/etc/ansible/nginx_config/roles/old/

回滚操作就是把旧的配置覆盖,然后重新加载nginx服务, 每次改动nginx配置文件之前先备份到old里,对应目录为/etc/ansible/nginx_config/roles/old/files。

更新操作结果:

[root@ansible-01 nginx_config]# ansible-playbook

/etc/ansible/nginx_config/update.yml

 

PLAY [testhost] ********************************************************************************************************

 

TASK [Gathering Facts] *************************************************************************************************

ok: [192.168.2.31]

ok: [127.0.0.1]

 

TASK [new : copy conf file] ********************************************************************************************

ok: [192.168.2.31] => (item={u'dest': u'conf/nginx.conf', u'src': u'nginx.conf'})

ok: [127.0.0.1] => (item={u'dest': u'conf/nginx.conf', u'src': u'nginx.conf'})

ok: [127.0.0.1] => (item={u'dest': u'conf/', u'src': u'vhosts'})

changed: [192.168.2.31] => (item={u'dest': u'conf/', u'src': u'vhosts'})

 

RUNNING HANDLER [new : restart nginx] **********************************************************************************

changed: [192.168.2.31]

 

PLAY RECAP *************************************************************************************************************

127.0.0.1                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

192.168.2.31               : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

回滚操作结果:

[root@ansible-01 nginx_config]# ansible-playbook /etc/ansible/nginx_config/rollback.yml

 

PLAY [testhost] ********************************************************************************************************

 

TASK [Gathering Facts] *************************************************************************************************

ok: [192.168.2.31]

ok: [127.0.0.1]

 

TASK [old : copy conf file] ********************************************************************************************

ok: [192.168.2.31] => (item={u'dest': u'conf/nginx.conf', u'src': u'nginx.conf'})

ok: [127.0.0.1] => (item={u'dest': u'conf/nginx.conf', u'src': u'nginx.conf'})

ok: [192.168.2.31] => (item={u'dest': u'conf/', u'src': u'vhosts'})

ok: [127.0.0.1] => (item={u'dest': u'conf/', u'src': u'vhosts'})

 

PLAY RECAP *************************************************************************************************************

127.0.0.1                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

192.168.2.31               : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

注:本次实验并未改变配置文件,故changed为0。

posted @ 2021-07-09 13:00  哆啦丢了梦  阅读(299)  评论(0编辑  收藏  举报