七大功能:
ansible命令,用于临时执行,必须掌握
ansible-doc是ansible模块的文档说明,针对每个模块都有详细说明,类似man帮助
ansible-console是ansible为用户提供交互式工具,可以在ansible-console虚拟出来的终端上像shell一样使用命令,交互式
ansible-galaxy从github上下载管理Roles的一款工具,与python的pip类似
ansible-playbook是日常使用频率最高的命令,是按一定条件组成的ansible任务集,通过读取playbook实现批量管理
ansible-vault主要用于配置文件加密
ansible-pull有两种模式pull/push,适用大批量机器配置,灵活性欠佳,但效率无上限,push是实时处理
一、部署和安装(以centos7.2.1511为例)
安装:
https://pkgs.org/download/ansible #源码包下载地址
python-setuptools python-devel #依赖包
进入源码目录:
python setup.py build #c:make
python setup.py install #c:make install
yum -y install ansible.noarch #另一种yum安装ansible
ansible --version #检查是否可用
二、配置及使用
ansible基础配置
ansible配置文件查找顺序:
1.检测ANSIBLE_CONFIG变量定义的配置文件
2.检查当前目录下的./ansible.cfg文件
3.检查当前用户价目录下~/ansible.cfg文件
4.最后/etc/ansible/ansible.cfg文件
vim /etc/ansible/ansible.cfg #ansible默认配置文件
inventory = /etc/ansible/hosts #指定分组文件路径,主机的分组文件hosts
host_key_checking = False #不检测主机的sshkey,在第一次连接的时候不用输入yes
vim /etc/ansible/hosts #ansible静态分组配置文件
[web] #自定义组名
web1 #主机名,需在/etc/hosts中配置才能管理
web2
web3
[db]
db[1:3] #多个主机写法
[app:children] #定义多个组
web
db
[other]
cache ansible_ssh_user="root" ansible_ssh_pass="a"
[app:children] #定义组中所有主机
web
db
[all:vars] #所有组:变量(vars不改),多台机器共用一个用户名和密码
ansible_ssh_user="root"
ansible_ssh_pass="a"
ansible_ssh_port="22"
ansible的inventory参数:
ansible_ssh_host #将要连接远程主机名与设定的主机别名不同
ansible_ssh_port #ssh连接端口
ansible_ssh_user #默认ssh的用户名
ansible_ssh_pass #ssh密码,一般使用ssh密钥,建议使用–ask-pass
ansible_sudo_pass #sudo密码,建议使用–ask-sudo-pass
ansible_sudo_exe #sudo命令路径
ansible_connection #与主机连接类型,如:local,ssh或paramiko,1.2以前默认"paramiko",1.2以后默认"smart"
ansible_ssh_private_key_file #ssh私钥文件,适用于多个私钥
ansible_shell_type #目标系统的shell类型,默认只用"sh"
ansible_python_interpreter #目标主机python路径,用于系统中有多个python,或者命令路径不是"/usr/bin/python"
动态主机分组:
Inventory通过外部脚本,按照其要求格式(json格式)返回给ansible命令
mkdir /root/ansible
cd /root/ansible
[defaults]
inventory = a.py
host_key_checking = False
touch ansible.cfg a.py
chmod 755 a.py
#!/usr/bin/python
import json
hostlist = {}
hostlist["web"] = ["web1","web2"]
hostlist["db"] = ["db1","db2"]
print json.dumps(hostlist)
ansible all --list-hosts 通过脚本返回json作为配置文件定义主机
ansible命令基础
ansible <host-pattern> [options]
–host-pattern 主机或定义的分组
-M 指定模块路径
-m 使用模块,默认command模块
-a or --args 模块参数
-i inventory文件路径,或脚本路径
-k 使用key进行ssh
-e 定义变量
-v 详细信息,-vvvv开启debug模式
示例:
ansible all -m command -a 'uptime' -k #远程执行命令uptime
ansible批量部署ssh公钥:
ssh-keygen -t rsa -b 1024 -N "" #创建密钥对
ansible all -m authorized_key -a "user=root exclusive=true manage_dir=true key='$(< /root/.ssh/id_rsa.pub)'" -k
#调用authorized_key公钥部署模块
#为root用户创建
#删除其他公钥
#没有即创建公钥目录
#标准输入公钥到key变量
ansible-doc模块手册:
ansible-doc -l #列出所有模块
ansible-doc [modulename] #查看模块帮助
command模块:
通过-a跟上要执行的命令,命令不能含有"<",">","|","&"
该模块不启动shell(bash)直接在ssh进程中执行,所有使用到shell的命令都会失败
ansible all -m command -a 'uptime'
shell模块:(常用)
用法基本和command一样,是通过/bin/sh进行执行命令,可以执行任意命令
ansible all -m shell -a "useradd z3;echo abc|passwd --stdin z3&&chage -d 0 z3"
raw模块:
用法和shell模块一样,区别是chdir,creates,removes参数无效
script模块:(常用)
在远程主机批量执行本地脚本
vim useradd.sh #本地不需要+x权限
#!/bin/bash
if ! `id z3 &>/dev/null`;then #如果z3不存在
useradd li4&&echo 123|passwd --stdin li4 #创建li4
fi
ansible web -m script -a 'useradd.sh' #远程执行脚本
copy模块(会进行校验,适用小文件):
复制本地文件到远程主机
src:本地源文件
dest:必选项,远程主机的绝对路径,如果源文件是目录,该路径也必须是目录
backup:(会对比源文件,较消耗资源,少用)覆盖前先备份原文件,包含时间信息
ansible all -m copy -a 'src=/etc/resolv.conf dest=/etc/resolv.conf'
lineinfile|replace模块:
类似sed的一种行编辑替换模块
dest: 要修改的文件
regexp: 正则表达式,匹配要修改的字段
line 修改后的整行
replace 修改后的值
ansible db -m lineinfile -a 'dest="/etc/my.cnf" regexp="^binlog-format =" line="binlog-format = mixed"'
ansible db -m replace -a 'dest="/etc/my.cnf" regexp="mixed" replace="statement"' #替换匹配到的所有mixed
ansible db -m replace -a 'dest="/etc/my.cnf" regexp="^(binlog-format\ +=).*" replace="\1 mixed"'
yum模块:
使用yum来管理软件包
name: 包名,也可以时url或本地rpm路径,多个包:httpd, mariadb
state: 执行动作(installed,removed,latest(更新))
ansible web -m yum -a "name=httpd state=installed"
ansible web -m yum -a "name=httpd state=latest"
service模块:
name: 服务名称
state: 指定动作(started,stopped,restarted,reloaded)
enabled: 是否开机自启(yes,no)
sleep: restarted的休眠时间
ansible web -m service -a 'name=httpd enabled=yes state=started'
setup模块:
获取远程主机信息
filter: 过滤
ansible cache -m setup|grep hostname
ansible cache -m setup -a 'filter=ansible_hostname' #过滤且输出为json格式
ansible-playbook:
链接: ansible-playbook.