阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

自动化运维之Ansible详解

166次阅读
没有评论

共计 9559 个字符,预计需要花费 24 分钟才能阅读完成。

1.Ansible 安装以及配置认证

ansible 也是有 Python 开发的。

ansible 特点:

不需要安装客户端,通过 sshd 去通信

基于模块工作,模块可以由任何语言开发

不仅支持命令行使用模块,也支持编写 yaml 格式的 playbook

支持 sudo

又提供 UI(浏览器图形化)www.ansible.com/tower 10 台主机以内免费

开源 UI http://github.com/alaxli/ansible_ui

文档 http://www.linuxidc.com/Linux/2017-03/142190.htm

ansible 安装:

两台机器:

服务端:192.168.147.137

客户端:192.168.147.138

在两台机器的 /etc/hosts 文件里加入:

192.168.147.137 server

192.168.147.138 client

只需要在服务端上安装 ansible 即可

服务端:

yum install -y epel-release

yum install -y ansible

ansible 配置密钥:

服务端:

生成秘钥对(默认放在 /root/.ssh/ 目录下,也可以自定义):

ssh-keygen -t rsa

直接回车即可,不用设置秘钥密码

把公钥(id_rsa.pub)内容放到客户端(192.168.147.138)的 /root/.ssh/authorized_keys 里面:scp /root/.ssh/id_rsa.pub 192.168.147.138:/root/.ssh/authorized_keys

也在服务端上的 /root/.ssh/authorized_keys 里面复制一份公钥,后面要用本机做一个客户端,即 127.0.0.1:scp /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys

客户端:

更改文件权限:

chmod 600 /root/.ssh/authorized_keys

关闭 selinux:

setenforce0

测试服务端连接客户端:

ssh client

如果 ssh 命令不存在,安装一下:

yum insall -y openssh-clients

 

2.ansible 远程执行命令

服务端:

定义一个主机组:

vim /etc/ansible/hosts

添加一个主机组:

[testhost]

127.0.0.1

192.168.147.138

说明:testhost 为主机组名字,自定义的。下面两个 IP 为组内的机器 IP,也可以写主机名,前提是能解析为 IP。

对这组主机执行 w 命令:

ansible testhost -m command -a ‘w’

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

ansible 127.0.0.1 -m command -a ‘hostname’

错误:“msg”:”Aborting, target uses selinux but python bindings (libselinux-python) aren’t installed!”

解决:yum install -y libselinux-python

还有一个模块就是 shell 同样也可以实现,支持管道:

ansible testhost -m shell -a ‘cat /etc/passwd | grep root’

shell 功能比 command 功能多。

 

3.ansible 拷贝目录或者文件

ansible testhost -m copy -a “src=/etc/ansible dest=/tmp/ansibletest owner=root group=root mode=0755”

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

ansible testhost -m copy -a “src=/etc/passwd dest=/tmp/123”

这里的 /tmp/123 和源机器上的 /etc/passwd 是一致的,但如果目标机器上已经有 /tmp/123 目录,则会在 /tmp/123/ 目录下建立 passwd 文件。

 

4.ansible 远程执行脚本

首先创建一个 shell 脚本:

vim /tmp/test.sh

#!/bin/bash

echo `date` > /tmp/ansible_test.txt

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

ansible testhost -m copy -a “src=/tmp/test.sh dest=/tmp/test.sh mode=0755”

最后是批量执行该 shell 脚本

ansible testhost -m shell -a “/tmp/test.sh”

shell 模块,还支持远程执行命令并且带管道:

ansible testhost -m shell -a “cat /etc/passwd | wc -l”

 

5.ansible 实现任务计划

服务端:

创建一个任务计划:

ansible testhost -m cron -a “name=’test cron’ job=’/bin/touch /tmp/1212.txt’ weekday=6”

如果删除该 cron 只需要加一个字段 state=absent

ansible testhost -m cron -a “name=’test cron’ state=absent”

其他的时间表示:

分:minute

时:hour

日:day

月:month

周:weekday

客户端:

查看任务计划:

crontab -l

 

6.ansible 安装 rpm 包和管理服务

安装 httpd:

ansible testhost -m yum -a “name=httpd”

在 name 后面还可以加上 state=installed,默认不加也可以安装

开启 httpd 服务,并设为开机启动:

ansible testhost -m service -a “name=httpd state=started enabled=yes”

这里的 name 是 CentOS 系统里的服务名,可以通过 chkconfig –list 查看。

ansible 文档的使用:

ansible-doc -l 查看所有的模块

ansible-doc cron 查看指定的模块

 

7.ansible playbook 介绍

类似于 shell 脚本,相当于把命令写入到文件里,例如:

vim /etc/ansible/test.yml

– hosts: testhost

 remote_user: root

 tasks:

   – name: test_playbook

     shell: touch /tmp/test.txt

说明:hosts 参数制定了对哪些主机进行操作;

user 参数制定了使用什么用户登录远程主机操作;

tasks 制定了一个任务,其下面的 name 参数同样是对任务的描述,在执行过程中会打印出来。

执行:ansible-playbook /etc/ansible/test.yml

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

vim /etc/ansible/create_user.yml

– name: create_user

 hosts: testhost

 user: root

 gather_facts: false

 vars:

   – user: “test”

 tasks:

   – name: create user

     user: name=”{{user}}”

执行:ansible-playbook /etc/ansible/create_user.yml

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

收集客户端的信息:

ansible client -m setup

 

8.ansible playbook 循环

修改 testhost 组下主机的 /tmp/ 目录下的 1.txt,2.txt,3.txt(首先保证这些文件存在)的权限修改为 600,属主改为 root,属组改为 root:

编辑一个 loop.yml 脚本:

vim /etc/ansible/loop.yml

– hosts: testhost

 user: root

 tasks:

   – name: change mode for file

     file: path=/tmp/{{item}} mode=600 owner=root group=root

     with_items:

      – 1.txt

      – 2.txt

      – 3.txt

执行:

ansible-playbook /etc/ansible/loop.yml

 

9.ansible playbook 判断

当满足条件 ansible_hostname == “client” 时执行命令:

touch /tmp/when.txt:

编辑一个 when.yml 脚本:

vim /etc/ansible/when.yml

– hosts: testhost

 remote_user: root

 gather_facts: True

 tasks:

   – name: use when

     shell: touch /tmp/when.txt

     when: ansible_hostname == “client”

执行:ansible-playbook /etc/ansible/when.yml

 

10.ansible playbook 中的 handlers

在执行 task 之后,服务器发生变化之后要执行的一些操作,比如我们修改了一些配置问价后,需要重启一下服务:

vim /etc/ansinle/handlers.yml

– name: handlers test

 hosts: testhost

 user: root

 tasks:

   – name: copy file

     copy: src=/etc/passwd dest=/tmp/aaa.txt

     notify: test handlers

 handlers:

   – name: test handlers

     shell: echo “111111” >> /tmp/aaa.txt

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

执行:ansible-playbook /etc/ansinle/handlers.yml

 

11.ansible 实例 - 安装 Nginx

思路:先在一台机器上编译安装好 Nginx、打包,然后再用 ansible 去下发

进入 ansible 配置文件目录:cd /etc/ansible

创建一个 nginx_install 目录,方便管理:mkdir nginx_install

进入该目录:cd nginx_install

mkdir -p roles/{common,install}/{handlers,files,meta,tasks,templates,vars}

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

详细步骤:

1. 编译安装 Nginx 可以参考 LNMP 部分

2. 安装完 Nginx 后可以看到安装目录下的内容:ls /usr/local/nginx/

3. 切换目录:cd /usr/local/

4. 打包压缩 Nginx 安装目录:tar czf nginx.tar.gz nginx

5. 切换到 ansible 配置文件目录:cd /etc/ansible

6. 创建 nginx_install 目录:mkdir nginx_install

7. 进入该目录:cd nginx_install

8. 创建子目录:mkdir -p roles/{common,install}/{handlers,files,meta,tasks,templates,vars}

9. 将相关文件拷贝到指定目录下:

cp /usr/local/nginx.tar.gz /etc/ansible/nginx_install/roles/install/files/

cp /usr/local/nginx/conf/nginx.conf /etc/ansible/nginx_install/roles/install/templates/

cp /etc/init.d/nginx /etc/ansible/nginx_install/roles/install/templates/

10. 编写 common/tasks 的总入口配置文件:

vim  /etc/ansible/nginx_install/roles/common/tasks/main.yml

– name: Install initialization require software

        yum: name={{item}} state=installed

        with_items:

          – zlib-devel

          – pcre-devel

          – openssl-devel

11. 编写 install/vars 的总入口配置文件:

vim /etc/ansible/nginx_install/roles/install/vars/main.yml

nginx_user: www

nginx_basedir: /usr/local/nginx

12. 编写 install/tasks 的 copy 配置文件:

vim /etc/ansible/nginx_install/roles/install/tasks/copy.yml

– name: Copy Nginx Sofrware

 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

13. 编写 install/tasks 的 install 配置文件:

vim /etc/ansible/nginx_install/roles/install/tasks/install.yml

– name: Create Nginx User

 user: name={{nginx_user}} state=present createhome=no shell=/sbin/nologin

– name: Start Nginx Service

 service: name=nginx state=started

– name: Add Boot Start Nginx Service

 shell: chkconfig –level 345 nginx on

– name: Delete Nginx compression files

 shell: rm -rf /tmp/nginx.tar.gz

14. 编写 install/tasks 的总入口配置文件:

vim /etc/ansible/nginx_install/roles/install/tasks/main.yml

– include: copy.yml

– include: install.yml

15. 编写整个程序的总入口配置文件:

vim /etc/ansible/nginx_install/install.yml

– hosts: 192.168.147.138

 remote_user: root

 gather_facts: True

 roles:

   – common

   – install

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

客户端:

查看上述三个包是否已经安装成功:rpm -qa | egrep ‘pcre|openssl|zlib’

查看 Nginx 是否安装成功:ls /usr/local/nginx/

查看服务是否启动成功:ps aux | grep nginx

查看加入服务列表是否成功:chkconfig –list nginx

 

11.ansible 实例 - 管理 Nginx 配置文件

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

mkdir -p /etc/ansible/nginx_config/roles/{new,old}/{files,handlers,vars,tasks}

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

关于回滚,需要在执行 playbook 之前备份一下旧的配置,所以对于老配置文件的管理一定要严格,千万不要随便去修改线上机器的配置,并且要保证 new/files 下面的配置和线上的配置一致

先把 nginx.conf 和 vhosts 目录放到 files 目录下面

cd /usr/local/nginx/conf/

cp -r nginx.conf vhosts /etc/ansible/nginx_config/roles/new/files

详细步骤:

1. 级联创建所需要的目录:

mkdir -p /etc/ansible/nginx_config/roles/{new,old}/{files,handlers,vars,tasks}

2. 将 Nginx 的配置文件拷贝到指定目录:

cp /usr/local/nginx/conf/nginx.conf /etc/ansible/nginx_config/roles/new/files/

cp -r /usr/local/nginx/conf/vhosts /etc/ansible/nginx_config/roles/new/files/

3. 定义 vars:

vim /etc/ansible/nginx_config/roles/new/vars/main.yml

nginx_basedir: /usr/local/nginx

4. 定义 handlers:

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

– name: restart nginx

 shell: /etc/init.d/nginx reload

5. 定义 tasks:

vim /etc/ansible/nginx_config/roles/new/tasks/main.yml

– name: copy conf file

 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

6. 定义一个 update 程序的入口文件:

vim /etc/ansible/nginx_config/update.yml

– hosts: 192.168.147.138

 user: root

 roles:

   – new

7. 执行更新:ansible-playbook /etc/ansible/nginx_config/update.yml

上面实现了更新,即在服务端修改配置文件,执行 playbook 即可同步到客户端。

回滚的实现更简单:在每次更新之前,先将 new/ 目录下的配置文件备份到 old/ 目录下,定义一个 rollback 程序的入口文件:

vim /etc/ansible/nginx_config/rollback.yml

– hosts:  192.168.147.138

 user: root

 roles:

   – old

如果更新失败,可以执行回滚:ansible-playbook /etc/ansible/nginx_config/rollback.yml

ansible 样例库:

git clone git://github.com/dl528888/ansible-examples.git

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

下面关于 Ansible 的文章您也可能喜欢,不妨参考下:

使用 Ansible 批量管理远程服务器  http://www.linuxidc.com/Linux/2015-05/118080.htm

在 CentOS 7 中安装并使用自动化工具 Ansible  http://www.linuxidc.com/Linux/2015-10/123801.htm

CentOS 7 上搭建 Jenkins+Ansible 服务  http://www.linuxidc.com/Linux/2016-12/138737.htm

Linux 下源码编译安装 Ansible 及排错记录  http://www.linuxidc.com/Linux/2017-03/141427.htm

Ansible 基础—安装与常用模块  http://www.linuxidc.com/Linux/2017-02/140216.htm

Ansible 配置及使用  http://www.linuxidc.com/Linux/2017-03/142121.htm

自动化运维工具之 Ansible 介绍及安装使用  http://www.linuxidc.com/Linux/2016-12/138104.htm

Ansible 入门 notify 和 handlers  http://www.linuxidc.com/Linux/2017-02/140871.htm

CentOS 6.5 安装自动化工具 Ansible 和图形化工具 Tower  http://www.linuxidc.com/Linux/2017-03/141422.htm

Ansible 的详细介绍:请点这里
Ansible 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-03/142191.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计9559字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中