Ansible部署实践

1 概述

ansible是一个基于python开发的轻量级自动化运维管理工具,可以用来批量执行命令,安装程序,支持playbook编排。它通过ssh协议来连接主机,去中心化,相对比puppet和saltstack无需安装客户即可实现文件传输、命令执行、应用部署、配置管理、任务编排等,显得更为简单与轻量。ansible只是提供一种框架,其基于模块工作的,本身没有批量部署。

2 部署

2.1 环境

名称 主机名 IP
控制端 myimage 192.168.80.129
受控机1 server-1 192.168.80.130
受控机2 server-2 192.168.80.131
受控机3 server-3 192.168.80.132
受控机4 server-4 192.168.80.133
受控机5 server-5 192.168.80.134

2.2 安装ansible

有两种方式可以安装,一种是源代码,一种是yum。如果采用源代码安装,需要首先安装很多的依赖模块,为了简化安装,本文使用的是yum源安装。

yum install -y ansible

2.2 配置ssh互信

控制机需要将自己的公钥分发到各受控机,让控制机能够免秘钥登录受控机。

在控制机执行以下命令产生公钥,简化可以使用4个回车完成秘钥的生成。

ssh-keygen -t rsa

分发到各受控机,此时需要受控机的密码。

ssh-copy-id 192.168.80.130
ssh-copy-id 192.168.80.131
ssh-copy-id 192.168.80.132
ssh-copy-id 192.168.80.133
ssh-copy-id 192.168.80.134

可以在主控机ssh受控机IP来验证,如果不需要密码能够登录说明配置ssh互信成功。
例如:

[root@myimage ~]# ssh 192.168.80.130
Last login: Thu May 16 18:36:11 2019 from 192.168.80.1
[root@server-1 ~]# 

2.3 配置主机列表

修改主机文件inventory:此文件定义了受控机的列表

vim /etc/ansible/hosts

[agent]
192.168.80.130
192.168.80.131
192.168.80.132
192.168.80.133
192.168.80.134

2.4 配置ansible参数

配置以下几个常用的参数即可

vim /etc/ansible/ansible.cfg

inventory =/etc/ansible/hosts             #定义资源清单inventory文件的位置,一般保持默认
library =/usr/share/my_modules/           #library指向ansible模块的目录,一般保持默认
forks =10                                 #设置多少个进程同时工作
sudo_user=root                            #设置默认执行命令的用户,也可在playbook中重新设置此参数
remote_port=22                            #制定连接被管理的管理端口,默认为22
timeout =10                               #设置SSH连接的超时时间间隔,单位为秒

2.5 验证

  • 在各主机上创建一个测试文件test
[root@myimage ~]# ansible agent -m command -a "touch /root/test"
[DEPRECATION WARNING]: DEFAULT_SUDO_USER option, In favor of become which is a generic framework . This feature will be removed in version 2.8. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
 [WARNING]: Consider using file module with state=touch rather than running touch
192.168.80.131 | SUCCESS | rc=0 >>

192.168.80.130 | SUCCESS | rc=0 >>

192.168.80.132 | SUCCESS | rc=0 >>

192.168.80.134 | SUCCESS | rc=0 >>

192.168.80.133 | SUCCESS | rc=0 >>
  • 测试各台主机是否存活
[root@myimage ~]# ansible agent -m ping
192.168.80.134 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
192.168.80.132 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
192.168.80.131 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
192.168.80.130 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
192.168.80.133 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
  • 查看受控机上python的版本
[root@myimage ~]# ansible agent -m shell -a "python -V"
192.168.80.134 | SUCCESS | rc=0 >>
Python 2.7.5
192.168.80.131 | SUCCESS | rc=0 >>
Python 2.7.5
192.168.80.133 | SUCCESS | rc=0 >>
Python 2.7.5
192.168.80.130 | SUCCESS | rc=0 >>
Python 2.7.5
192.168.80.132 | SUCCESS | rc=0 >>
Python 2.7.5
  • 查看受控机上主机名称
[root@myimage ~]# ansible agent -m command -a "cat /etc/hostname"
192.168.80.132 | SUCCESS | rc=0 >>
server-3
192.168.80.133 | SUCCESS | rc=0 >>
server-4
192.168.80.134 | SUCCESS | rc=0 >>
server-5
192.168.80.130 | SUCCESS | rc=0 >>
server-1
192.168.80.131 | SUCCESS | rc=0 >>
server-2

3 模块介绍

ansible的模块非常多,相关的用法如下:

ansible-doc -l #查看所有的模块,后面接管道|统计后有1378个

ansible-doc -s MODULE_NAME  #查看指定模块的详细帮助  

命令语法:

ansible <host-pattern> [-f forks] [-m module_name] [-a args]

-f forks:启动的并发线程数
-m module_name: 要使用的模块
-a args: 模块特有的参数

3.1 copy模块

例如拷贝一个本地文件到各个受控节点上。

ansible agent -m copy -a "src=/root/test.py dest=/root"

3.2 script模块

执行受控节点上的脚本文件

ansible agent -m script -a "/root/test.py"

3.3 file模块

更改受控节点上file文件的属性

ansible agent -m file -a "dest=/root/test.py mode=777 owner=root group=root" -s

3.4 yum模块

在受控节点安装Apache软件

ansible agent -m yum -a "name=httpd state=latest" -s

3.5 cron模块

在受控节点手上增加同步时间计划任务

ansible agent -m cron -a 'name="my job" minute=*/1 hour=* day=* month=* weekday=* job="/usr/sbin/ntpdate time1.aliyun.com"'

3.6 shell

在受控节点执行命令,注意是bash命令。

ansible agent -m shell -a "/root/test.sh" 

shell模块支持管道符,command模块不支持

3.7 user模块

创建一个用户

ansible agent -m user -a "name=xuel home=/home/xuel state=present" -s

3.8 组模块

创建一个test组

ansible agent -m group -a "name=test state=present" -s

4 playbook介绍

(待更新)

参考资料:
官网
英文文档
Ansible中文权威指南

原文地址:https://www.cnblogs.com/easonbook/p/10877859.html