一、Ansible基础
1.1 什么是Ansible
Ansible是一款基于python开发的自动化运维工具,是用来批量管理多台机器的。
ansible可以让我们实现:
– 自动化部署APP
– 自动化管理配置项
– 自动化的持续交付
– 自动化的(AWS)于服务管理
1.2 ansible特性
1.模块化设计,调用特定的模块来完成特定任务
2.基于python语言实现
3.其模块支持JSON等标准输出格式
4.支持playbook
1.3 部署Ansible
1.3.1 环境准备
1. 对管理主机
– 要求Python 2.6 或以上环境
2. 对于被托管主机
– 要求Pyhton 2.5 或以上环境
– 托管主机要开启 ssh 服务
1.3.2 安装Ansible
1.搭建自定义yum源
1.1 下载 ansible_soft
1.2 把软件包解压到ftp指定目录下,通过createrepo创建yum源
ansible]# createrepo .
附:如果自定义yum源添加的rpm包后,只需要更新索引文件即可 createrepo --update .
2.客户端挂载后,直接通过yum安装
~]# yum -y install ansible
3.安装后查看版本
~]# ansible --version
二、ad-hoc
2.1 主机管理
2.1.1 主机定义与分组
1.Ansible配置文件查找顺序
1.首先检测 ANSIBLE_CONFIG 变量定义的配置文件
2. 其次检查当前目彔下的 ./ansible.cfg 文件
3.再次检查当前用户家目彔下 ~/ansible.cfg 文件
4.最后检查 /etc/ansible/ansible.cfg 文件
2./etc/ansible/ansible.cfg 默认配置文件路径
3.ansible.cfg 配置文件
– inventory 是定义托管主机地址配置文件
– 首先编辑 /etc/ansible/hosts 文件,写入一些进程主机
4.格式
– # 表示注释
[组名称]
主机名称或ip地址,登录用户名,密码、端口等信息
5.测试
– ansible [组名称] --list-hosts
2.1.2 hosts文件inventory 参数说明
1. ansible_ssh_port
– ssh端口号.如果不是默认的端口号,通过此变量设置.
2. ansible_ssh_user
– 默认的 ssh 用户名
3. ansible_ssh_pass
– ssh 密码(这种方式并不安全,建议使用 --ask-pass 或 SSH 密钥)
4. ansible_sudo_pass
– sudo 密码(建议使用 --ask-sudo-pass)
2.1.3 hosts文件的配置
[web] //指定分组名称
web[1:2] // 指定web 1~2的范围
[db]
db1
db2
[app:children] //指定子组
web
db
[app:vars] //指定app主的公有变量
ansible_ssh_user="root"
ansible_ssh_pass="123456"
[other]
cache ansible_ssh_user="root" ansible_ssh_pass="123456" //指定连接的用户名和密码
2.1.4 动态主机
动态主机可以用一个JSON格式的字符串来表示,这种字符串可以自己写脚本定义
1. 脚本内容
#!/usr/bin/python
import json
hostlist = {}
hostlist["aa"] = ["192.168.1.15", "192.168.1.16"]
hostlist["192.168.1.13"] = {
"ansible_ssh_user":"root","ansible_ssh_pass":"pwd"
}
hostlist["bb"] = {
"hosts" : ["192.168.1.11", "192.168.1.12"],
"vars" : {
"ansible_ssh_user":"root","ansible_ssh_pass":"pwd"
}
}
print( json.dumps(hostlist))
2.脚本输出
{
"aa" : {
"hosts" : ["192.168.1.11", "192.168.1.12"],
"vars" : {
"ansible_ssh_user" : "root",
"ansible_ssh_pass" : "pwd"
}
},
"bb" : ["192.168.1.15", "192.168.1.16"],
"192.168.1.13": { "ansible_ssh_user" : "root",
"ansible_ssh_pass" : "pwd"}
}
2.2 批量执行
2.2.1 ansible命令基础
ansible 主机分组 -m 模块 -a '命令和参数'
– -M 指定模块路径
– -m 使用模块,默认 command 模块
– -a or --args 模块参数
– -i inventory 文件路径,或可执行脚本
– -k 使用交互式登录密码
– -e 定义变量
– -v 详细信息,-vvvv 开启 debug 模式
2.2.2 给所有主机部署密钥
1.创建密钥对 id_rsa 是私钥, id_rsa.pub 是公钥
ssh-keygen -t rsa -b 2048 -N ''
2.使用authorized_key模块实现批量部署免密登录
ansible all -m authorized_key -a "user=root exclusive=true manage_dir=true key='$(< /root/.ssh/id_rsa.pub)'" -k
exclusive=true:表示如果托管主机上有部署过的本机的公钥信息,直接覆盖。没有就添加公钥系信息
manage_dir: 表示托管主机没有/root/.ssh文件夹就创建,有就直接创建authorized_keys文件
三、批量配置管理
3.1 模块
3.1.1 ansible-doc 模块
1. 模块的手册,相当于 shell 的 man
2. ansible-doc -l 列出所有模块
3. ansible-doc modulename 查看帮助
~]# ansible-doc authorized_key
3.1.2 ping 模块
1. ping模块没有参数,测试的是 ssh 的连通性
~] # ansible host-pattern -m ping
3.1.3 command | shell | raw 模块
1.command 是默认模块,没有启用 shell ,所有shell 相关特性命令无法使用,例如 < > | &
– 使用默认模块下面两条命令执行会失败
~] #ansible all -m command -a 'ps aux|grep ssh'
~] #ansible all -m command -a 'set'
~] # ansible t1 -m command -a 'chdir=/tmp touch f3' //f3在/tmp目录下创建成功
2.raw 模块,没有 chdir create remove 等参数,能执行大部分操作
~] # ansible t1 -m raw -a 'chdir=/tmp touch f3' //f3文件会在当前目录下(/root)创建,并不会在/tmp目录下创建,因为raw模块不支持chdir
3.shell 模块,启动 /bin/sh 运行命令,可以执行所有操作 【生产环境中用】
~] # ansible t1 -m raw -a 'chdir=/tmp touch f3' //f3会在/tmp目录下创建,shell模块支持所有bash下的所有命令
3.1.4 script模块
1. 直接在本地写脚本,然后使用 script 模块批量执行
2. ansible t1 -m script -a 'script'
3.该脚本包含但不限于 shell 脚本,只要指定 Sha-bang 解释器的脚本都可运行
4.业务需求:
在web主的所有主机上如果有zhangsan用户就什么都不干
如果没有zhangsan用户,就添加lisi用户。并设置默认密码为123456,lisi用户第一次登录的时候必须修改自己的密码
5.功能实现
#!/bin/bash
id zhangsan
if [ $? -ne 0 ];then
useradd lisi
echo 654321 | passwd --stdin lisi
chage -d 0 lisi
fi
~ ] # ansible web -m script -a 'userweb.sh'
3.1.5 copy模块
1.copy模块用来复制文件到进程主机
– src:要复制到进程主机的文件在本地的地址。如果路径是一个目录,它将递归复制。
在这种情况下,如果路径使用"/"来结尾,则只复制目录里的内容,如果没有使用"/"来结尾,则
包含目录在内的整个内容全部复制
– dest:必选项。进程主机的绝对路径,如果源文件是一个目录,那么该路径也必须是个目彔
1.1 实验说明
~]# ansible cache -m copy -a 'src=/root/myansible dest=/root/'
cache主机的/root下会多一个myansible文件夹
~]# ansible cache -m copy -a 'src=/root/myansible/ dest=/root/'
cache主机的/root下会多出myansible文件夹里面的文件,并不会有myansible文件夹
– backup:在覆盖前将原文件备份,备份文件包含时
间信息。有两个选项:yes|no
– force:如果目标主机包含该文件,但内容不同,如果
设置为yes,则强制覆盖,如果为no,则只有当目标主
机的目标位置不存在该文件时,才复制。默认为yes
2. 复制文件
[root@ansible ~]# ansible cache -m copy -a 'src=/root/rsync.sh dest=/root'
3. 复制目录
ansible t1 -m copy -a 'src=urdir dest=/root/'
3.1.6 lineinfile | replace 模块
1. lineinfile | replace是一种替换文本内容的模块
2.参数说明
2.1 path 目的文件
2.2 regexp 正则表达式
2.3 line 替换后的结果
2.4 replace 正则匹配到结果需要替换后的内容
3.lineinfile替换的是一整行的内容
ansible t1 -m lineinfile -a 'path="/etc/selinux/config"
regexp="^SELINUX=" line="SELINUX=disabled"'
4.replace替换的是指定字符
ansible t1 -m replace -a 'path="/etc/selinux/config"
regexp="^(SELINUX=).*" replace="\1disabled"'
3.1.7 yum模块
1.yum模块是用来批量管理多台主机软件包
2.参数说明
2.1 name:要进行操作的软件包的名字
2.2 state:状态(installed,removed) [Default: installed]
3.安装删除软件包
3.1 安装多个软件包
ansible t1 -m yum -a 'name="lrzsz,lftp"'
3.2 删除多个软件包
ansible t1 -m yum -a 'name="lrzsz,lftp" state=absent'
3.1.8 service模块
1.yum模块批量管理多台主机服务
2.参数说明
– name:必选项,服务名称
– enabled:是否开机启动 yes|no
– state:对当前服务执行启动,停止、重启、重新加载
等操作(started,stopped,restarted,reloaded)
~] # ansible t1 -m service -a 'name="sshd" enabled="yes" state="started"'
3.1.9 setup模块
1.setup模块主要用于获取主机信息
2.filter 可以过滤到我们需要的信息
~]# ansible cache -m setup -a 'filter=ansible_os_family'