ansible
第1章 了解及安装ansible
1.1 介绍
ansible是一个基于Python开发的自动化运维工具!
其功能实现基于SSH远程连接服务!
ansible可以实现批量系统配置、批量软件部署、批量文件拷贝、批量运行命令等功能
http://docs.ansible.com/ansible/intro_installation.html
http://docs.ansible.com/modules_by_category.html
特点:
1、不需要单独安装客户端(no agents),基于系统自带的sshd服务,sshd就相当于ansible的客户端
2、不需要服务端( no servers)
3、需要依靠大量的模块实现批量管理。
4、配置文件/etc/ansible/ansible.cfg
1.2 ansible特点
- 配置管理
- 批量部署
- ad-hoc批量执行命令编写
- playbook剧本-脚本
1.3 核心功能
- PyYAML-脚本的语言
- paramiko-远程连接与数据传输
- Jinjia2-
1.4 管理端安装ansible
先要有eqel源 wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
yum install ansible -y
[root@m01 ~]# ansible --version
ansible 2.3.1.0
config file = /etc/ansible/ansible.cfg
configured module search path = Default w/o overrides
python version = 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)]
[root@m01 ~]#
[root@m01 ~]# tree -F /etc/ansible/
/etc/ansible/
├── ansible.cfg ####所有服务器的ssh端口变量
├── hosts ####被ansible管理的服务器 ip地址列表
└── roles/
1.5 所有被管理端需要安装
yum install libselinux-python -y
1.6 编辑配置文件
编辑ansible的主机配置文件hosts, 添加主机组 oldboy
这个文件里都是注释 说明书
cp /etc/ansible/hosts{,.bak}
cat >>/etc/ansible/hosts<<EOF
[oldboy]
172.16.1.41
172.16.1.31
172.16.1.8
EOF
1.7 ansible 如果服务器用户密码不同如何处理。
[oldboy]
172.16.1.31 ansible_ssh_user=oldboy ansible_ssh_pass=123456
172.16.1.41 ansible_ssh_user=oldboy ansible_ssh_pass=123456
第2章 ansible 怎么使用
[root@m01 ~]# ansible oldboy -m command -a "whoami"
172.16.1.41 | SUCCESS | rc=0 >>
root
172.16.1.31 | SUCCESS | rc=0 >>
root
172.16.1.8 | SUCCESS | rc=0 >>
root
2.1 #1.批量运行命令
#1.批量运行命令
ansible oldboy -a "uptime "
ansible oldboy -m command -a "uptime "
2.2 #2.批量发送文件
#2.批量发送文件
ansible oldboy -m copy -a "src=/etc/hosts dest=/opt"
ansible oldboy -a "ls -l /opt"
ansible oldboy -m copy -a "src=/etc/hosts dest=/opt/oldboy/oldgirl/"
ansible oldboy -a "tree /opt"
ansible oldboy -m copy -a "src=/etc/hosts dest=/opt/oldboy/oldgirl/ backup=yes"
copy
src=
dest=
mode=
backup=
2.3 #3.批量运行脚本
shell模块
运行脚本 命令 特殊符号
echo 'yum install -y ipvsadm' >/server/scripts/yum.sh
##1.先把脚本发送到对应的服务
ansible oldboy -m copy -a "src=/server/scripts/yum.sh dest=/server/scripts/ mode=755"
ansible oldboy -a "ls -l /server/scripts/yum.sh"
##2.运行 /bin/sh
ansible oldboy -m shell -a "/bin/sh /server/scripts/yum.sh"
script 模块
1.把脚本发送到对应的服务器上面
2.运行脚本
ansible oldboy -m script -a "/server/scripts/yuan.sh"
写一个脚本 更改所有服务器的yum源
cat >>/server/scripts/yuan.sh<<EOF
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
EOF
ansible oldboy -m script -a "/server/scripts/yuan.sh"
ansible oldboy -a "yum repolist"
ansible oldboy -m cron -a 'name=hello minute=*/2 job="echo hello >>/tmp/oldboy.log 2>&1" '
ansible oldboy -a "crontab -l"
2.4 远程批量执行命令
-a argument 参数
ansible oldboy -m command -a "uptime"
absible 主机组/ip/域名 -m 模块 动作
测试下
[root@m01 ~]# ansible oldboy -a "uptime"
172.16.1.41 | SUCCESS | rc=0 >>
10:14:32 up 1 day, 18:14, 2 users,
load average: 0.00, 0.00, 0.00
172.16.1.8 | SUCCESS | rc=0 >>
10:14:33 up 22:28, 2 users,
load average: 0.00, 0.00, 0.00
172.16.1.31 | SUCCESS | rc=0 >>
10:14:33 up 1 day, 16:53, 2 users,
load average: 0.00, 0.00, 0.00
[root@m01 ~]# ansible oldboy -m command -a "whoami"
172.16.1.41 | SUCCESS | rc=0 >>
root
172.16.1.31 | SUCCESS | rc=0 >>
root
172.16.1.8 | SUCCESS | rc=0 >>
root
2.5 远程批量拷贝文件或目录
语法:
ansible oldboy -m copy -a "src=/etc/passwd dest=/tmp/oldgirl.txt owner=oldboy group=oldboy mode=0755"
注意:
l)如果指定的目标目录不存在,系统会自动创建,否则源目录会放到目标目录下面去
2)如果copy的是文件,dest指定的名字和源如果不同,并且它不是已经存在的目录,相当于copy过去后再重命名
3)若果dest是目标机器上己经存在的目录,则会直接把文件copy到该目录下面。
4)设定的用户和组oldboy在所有客户端必须存在。
ansible oldboy -m copy -a "src=/etc/hosts dest=/opt"
ansible oldboy -a "ls -l /opt"
ansible oldboy -m copy -a "src=/etc/hosts dest=/opt/oldboy/oldgirl/"
ansible oldboy -a "tree /opt"
ansible-doc -s copy 看帮助
ansible oldboy -m copy -a "src=/etc/hosts dest=/opt/oldboy/oldgirl/ backup=yes" 是否备份
相关选项:
backup 在覆盖之前,将源文件备份,备份文件包含时间信息。有两个选项:yes|no
content 用于替代“src",可以直接设定指定文件的值
dest 必选项。要将源文件复制到的远程主机的绝对路径,如果源文件是一个目录,那么该路径也必须是个目录
directory_mode:递归设定目录的权限,默认为系统默认权限
force 如果目标主机包含该文件,但内容不同,如果设置为yes,则强制覆盖,如果为no,则只有当目标主机的目标位置不存在该文件时,才复制。默认为yes
others 所有的file模块里的选项都可以在这里使用
src 被复制到远程主机的本地文件,可以是绝对路径,也可以是相对路径。如果路径是一个目录,它将递归复制。在这种情况下,如果路径使用“/”来结尾,则只复制目录里的内容,如果没有使用 / 来结尾,则包含日录在内的整个内容全部复制,类似于rsync
2.6 远程运行脚本
shell 模块 支持特殊符号
#1.先把脚本发送到对应的服务
ansible oldboy -m copy -a "src=/server/scripts/yum.sh dest=/server/scripts/ mode=755"
ansible oldboy -a "ls -l /server/scripts/yum.sh"
#2.运行 /bin/sh
ansible oldboy -m shell -a "/bin/sh /server/scripts/yum.sh"
写一个更新源的脚本
cat >>/server/scripts/yuan.sh<<EOF
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
EOF
scripts 模块
1.把脚本发送到对应的服务器上面
2.运行脚本
ansible oldboy -m script -a "/server/scripts/yuan.sh"
ansible oldboy -a "yum repolist"
2.7 #1.批量运行命令
ansible oldboy -a "uptime "
ansible oldboy -m command -a "uptime "
2.8 #2.批量发送文件
ansible oldboy -m copy -a "src=/etc/hosts dest=/opt"
ansible oldboy -a "ls -l /opt"
ansible oldboy -m copy -a "src=/etc/hosts dest=/opt/oldboy/oldgirl/"
ansible oldboy -a "tree /opt"
ansible oldboy -m copy -a "src=/etc/hosts dest=/opt/oldboy/oldgirl/ backup=yes"
copy
src=
dest=
mode=
backup=
2.9 #3.批量运行脚本
2.9.1 shell模块
运行脚本 命令 特殊符号
echo 'yum install -y ipvsadm' >/server/scripts/yum.sh
##1.先把脚本发送到对应的服务
ansible oldboy -m copy -a "src=/server/scripts/yum.sh dest=/server/scripts/ mode=755"
ansible oldboy -a "ls -l /server/scripts/yum.sh"
##2.运行 /bin/sh
ansible oldboy -m shell -a "/bin/sh /server/scripts/yum.sh"
2.9.2 script 模块
1.把脚本发送到对应的服务器上面
2.运行脚本
nsible oldboy -m script -a "/server/scripts/yuan.sh"
写一个脚本 更改所有服务器的yum源
cat >>/server/scripts/yuan.sh<<EOF
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
EOF
ansible oldboy -m script -a "/server/scripts/yuan.sh"
ansible oldboy -a "yum repolist"
2.10 ansible远程批量执行脚本
shell模块
1.先将脚本分发到各个机器上
ansible oldboy -m copy -a "src=/server/scripts/yum.sh dest=/server/scripts/ mode=0755 backup=yes"
注意: 注意dest路径的写法,若是不存在的目录,结尾要加斜线, 否则默认不会创建目标目录
2.远程批量执行脚本
ansible oldboy -m shell -a "/bin/bash /server/scripts/yum.sh"
script模块
可以给被管理的机器执行本地有的脚本
ansible oldboy -m script -a "/server/scripts/xxx.sh"
2.11 模块:每个模块就是一个功能
command (默认的模块):执行命令模块
shell 执行shell脚本模块 [ 执行的是远端机器上有的脚本 ]
script 把本地脚本放到客户端(客户端可以没有这个脚本),然后执行。
copy 把本地文件发送到远端
file 设定文件属性模块
service 系统服务管理模块
cron 定时任务管理模块
yum yum软件包安装管理模块
synchronize 使用rsync同步文件模块
ansible oldboy -m service -a "name=crond state=started enabled=yes"
ansible 主机组 模块 操作的内容 ( enabled 启动)
authorized_key Adds or removes an SSH authorized key
2.12 ansible如何查询帮助 查询模块的参数
2.12.1 1.ansible-doc -s copy #查看某一个模块的帮助
ansible-doc -l (显示所有模块的列表)
2.12.2 2.官网
http://docs.ansible.com/ansible/list_of_all_modules.html
Ctrl + f 在浏览器里搜索内容
http://docs.ansible.com/ansible/list_of_all_modules.html
2.13 定时任务模块 cron
ansible oldboy -m cron -a 'name=hello minute=*/2 job="echo hello >>/tmp/oldboy.log 2>&1" '
ansible oldboy -a "crontab -l"
检查语法与模拟运行 ansible -C
显示过程 -v -vvvv 最多可以四个v
ansible all -m cron -a "name='network restart' minute=00 hour=00 job='/etc/init.d/network restart &>/dev/null' state=present"
如果想改定时任务 直接改就行 不改的不要动
ansible all -m cron -a "name='network restart' minute=00 hour=01 job='/etc/init.d/network restart &>/dev/null' state=present"
##添加定时任务
ansible oldboy -m cron -a 'name="restart network" minute=00 hour=00 job="/etc/init.d/network restart >/dev/null 2>&1" state=present' -C
ansible oldboy -a "tail -2 /var/spool/cron/root"
ansible oldboy -m cron -a 'name="restart network" minute=00 hour=00 job="/etc/init.d/network restart >/dev/null 2>&1" state=present'
ansible oldboy -a "tail -2 /var/spool/cron/root"
###删除定时任务
ansible oldboy -m cron -a 'name="restart network" state=absent' -C
ansible oldboy -m cron -a 'name="restart network" state=absent'
ansible oldboy -a "tail -2 /var/spool/cron/root"
1.真正运行ansible 生效前 检查语法
ansible -C
2.去掉-C生效
2.14 课后作业 数据实时备份
预习:
1.书写剧本-playbook
2.网站基础 http服务基础
用户在网站输入www.oldboyedu.com 到显示出内容
老男孩教育每日一题-2017年3月22日:请说明用户访问网站流程
http://lidao.blog.51cto.com/3388056/1914578
课后题目:
使用ansible部署rsync服务
rsync
nfs
数据同步-inotify/sersync
nfs服务器
[root@nfs01 scripts]# vim
deploy_nfs.sh
nfs=`rpm
-qa nfs-utils |wc -l`
if [
$nfs
-ge
1
]
then
:
else
yum install nfs-utils -y
fi
rpm=`rpm
-qa rpcbind |wc -l`
if [
$rpm
-ge
1
]
then
:
else
yum install rpcbind -y
fi
#创建共享目录
dir=/nfsbackup
if [
-d
$dir
]
then
chown -R
nfsnobody.nfsnobody $dir/
else
mkdir
-p
$dir
chown -R
nfsnobody.nfsnobody $dir/
fi
/etc/init.d/rpcbind start
/etc/init.d/nfs start
chkconfig nfs on
chkconfig rpcbind on
cat
>>/etc/exports<<EOF
#share dir
$dir
172.16.1.0/24(rw,sync)
EOF
/etc/init.d/nfs reload
web服务器
[root@web01 scripts]# vim mount_dir.sh
#!/bin/bash
##############################################################
# File Name: mount_dir.sh
# Version: V1.0
# Author: wu
# Organization: www.oldboyedu.com
# Created Time : 2017-07-01 18:42:37
# Description:
##############################################################
#检查是否有安装包
nfs=`rpm
-qa nfs-utils |wc -l`
if [
$nfs
-ge
1
]
then
:
else
yum install nfs-utils -y
fi
rpm=`rpm
-qa rpcbind |wc -l`
if [
$rpm
-ge
1
]
then
:
else
yum install rpcbind -y
fi
showmount -e
172.16.1.31 &>/dev/null
if [
$?
-eq
0
]
then
mount -t
nfs 172.16.1.31:/nfsbackup /mnt
else
echo
"不能挂载,"&&
exit
fi
#echo "mount -t nfs
172.16.1.31:/nfsbackup /mnt" >>/etc/rc.local
"mount_dir.sh" 33L, 711C
written
第3章 开始编写剧本 playbook
通过ansible批量管理服务
1.命令行 各种模块
2.书写剧本-playbook
3.1 核心---空格
ansible剧本中不支持tab
3.2 核心: 找谁 干啥
3.2.1 找谁
- hosts: all ##表示所有服务器
- hosts: oldboy
- hosts: 172.16.1.41
/etc/ansible/hosts all是这个文件里有的所有
3.2.2 干啥
tasks: ------>任务,作业,工作
-command: ifconfig
模块名 模块里的动作
[root@m01 ansible]# cat /etc/ansible/show.yml
- hosts: all
tasks:
- command: ifconfig
ansible-playbook /etc/ansible/show.yml -C 实验测试
vim 里 :w空格 新文件名 可以改文件名 生成个新文件 默认保存在当前路径 也可以指定到某一个路径
奶牛
yum install cowsay -y 装这个软件就会有奶牛
rpm -e cowsay 删除这个软件
3.3 例子
3.3.1 ansible oldboy -m shell -a "ifconfig >/tmp/new.txt"变为剧本
[root@m01 ansible]# cat /etc/ansible/ifconfig.yml
- hosts: oldboy
tasks:
- name: "show ip addr"
shell: ifconfig >/tmp/new.txt
[root@m01 ansible]# ansible-playbook /etc/ansible/ifconfig.yml
PLAY [oldboy] ***********************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************
ok: [172.16.1.31]
ok: [172.16.1.8]
ok: [172.16.1.41]
TASK [show ip addr] *****************************************************************************************************
changed: [172.16.1.41]
changed: [172.16.1.31]
changed: [172.16.1.8]
PLAY RECAP **************************************************************************************************************
172.16.1.31 : ok=2 changed=1 unreachable=0 failed=0
172.16.1.41 : ok=2 changed=1 unreachable=0 failed=0
172.16.1.8 : ok=2 changed=1 unreachable=0 failed=0
3.3.2 批量管理定时任务--设置时间同步
给所有服务器添加定时任务----变为剧本
ansible oldboy -m cron -a 'name="restart network" minute=00 hour=00 job="/etc/init.d/network restart >/dev/null 2>&1" state=present'
找谁:oldboy
干啥
cron模块添加定时任务
#ansible oldboy
#-m cron -a 'name="network restart 007" minute=00 hour=00 job="/etc/init.d/network restart >/dev/null 2>&1" state=present '
[root@m01 ansible]# vim cron.yml
#- hosts: all
# tasks:
# - name: cron
# cron: name='network restart' minute=00 hour=00 job='/etc/init.d/network restart &>/dev/null'
- hosts: oldboy
tasks:
- name: cron
cron: name="restart network" minute=00 hour=00 job="/etc/init.d/network restart >/dev/null 2>&1" state=present
[root@m01 ansible]# ansible-playbook cron.yml -C
PLAY [oldboy] ***********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [172.16.1.31]
ok: [172.16.1.41]
ok: [172.16.1.8]
TASK [cron] *************************************************************************************************************************
changed: [172.16.1.8]
changed: [172.16.1.31]
changed: [172.16.1.41]
PLAY RECAP **************************************************************************************************************************
172.16.1.31 : ok=2 changed=1 unreachable=0 failed=0
172.16.1.41 : ok=2 changed=1 unreachable=0 failed=0
172.16.1.8 : ok=2 changed=1 unreachable=0 failed=0
[root@m01 ansible]#
3.4 配置多个任务
3.4.1 all或组 执行多条任务
[root@m01 ansible]# cat multi-command.yml
- hosts: oldboy
tasks:
- name: show hostname
shell: hostname >>/tmp/name.log
- name: show ipaddr
shell: ip a >>/tmp/ip.log
[root@m01 ansible]# ansible-playbook multi-command.yml -C
PLAY [oldboy] ***********************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************
ok: [172.16.1.8]
ok: [172.16.1.41]
ok: [172.16.1.31]
TASK [show hostname] ****************************************************************************************************
skipping: [172.16.1.31]
skipping: [172.16.1.8]
skipping: [172.16.1.41]
TASK [show ipaddr] ******************************************************************************************************
skipping: [172.16.1.41]
skipping: [172.16.1.31]
skipping: [172.16.1.8]
PLAY RECAP **************************************************************************************************************
172.16.1.31 : ok=1 changed=0 unreachable=0 failed=0
172.16.1.41 : ok=1 changed=0 unreachable=0 failed=0
172.16.1.8 : ok=1 changed=0 unreachable=0 failed=0
[root@m01 ansible]# ansible-playbook multi-command.yml
PLAY [oldboy] ***********************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************
ok: [172.16.1.41]
ok: [172.16.1.31]
ok: [172.16.1.8]
TASK [show hostname] ****************************************************************************************************
changed: [172.16.1.31]
changed: [172.16.1.8]
changed: [172.16.1.41]
TASK [show ipaddr] ******************************************************************************************************
changed: [172.16.1.41]
changed: [172.16.1.31]
changed: [172.16.1.8]
PLAY RECAP **************************************************************************************************************
172.16.1.31 : ok=3 changed=2 unreachable=0 failed=0
172.16.1.41 : ok=3 changed=2 unreachable=0 failed=0
172.16.1.8 : ok=3 changed=2 unreachable=0 failed=0
3.4.2 不同主机对应不同任务
[root@m01 ansible]# cat /etc/ansible/multi-host.yml
- hosts: 172.16.1.41
tasks:
- name: show hostname
shell: hostname >>/tmp/name2.log
- hosts: 172.16.1.31
tasks:
- name: show ipaddr
shell: ip a >>/tmp/ip2.log
[root@m01 ansible]# cat /etc/ansible/multi-host.yml
- hosts: 172.16.1.41
tasks:
- name: show hostname
shell: hostname >>/tmp/name2.log
- hosts: 172.16.1.31
tasks:
- name: show ipaddr
shell: ip a >>/tmp/ip2.log
[root@m01 ansible]# ansible-playbook multi-host.yml -C
PLAY [172.16.1.41] ******************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************
ok: [172.16.1.41]
TASK [show hostname] ****************************************************************************************************
skipping: [172.16.1.41]
PLAY [172.16.1.31] ******************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************
ok: [172.16.1.31]
TASK [show ipaddr] ******************************************************************************************************
skipping: [172.16.1.31]
PLAY RECAP **************************************************************************************************************
172.16.1.31 : ok=1 changed=0 unreachable=0 failed=0
172.16.1.41 : ok=1 changed=0 unreachable=0 failed=0
[root@m01 ansible]# ansible-playbook multi-host.yml
PLAY [172.16.1.41] ******************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************
ok: [172.16.1.41]
TASK [show hostname] ****************************************************************************************************
changed: [172.16.1.41]
PLAY [172.16.1.31] ******************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************
ok: [172.16.1.31]
TASK [show ipaddr] ******************************************************************************************************
changed: [172.16.1.31]
PLAY RECAP **************************************************************************************************************
172.16.1.31 : ok=2 changed=1 unreachable=0 failed=0
172.16.1.41 : ok=2 changed=1 unreachable=0 failed=0
[root@m01 ansible]# ansible 172.16.1.41 -a "cat /tmp/name2.log"
172.16.1.41 | SUCCESS | rc=0 >>
backup
[root@m01 ansible]# ansible 172.16.1.31 -a "cat /tmp/ip2.log"
172.16.1.31 | SUCCESS | rc=0 >>
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:b0:74:02 brd ff:ff:ff:ff:ff:ff
inet 10.0.0.31/24 brd 10.0.0.255 scope global eth0
inet6 fe80::20c:29ff:feb0:7402/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:b0:74:0c brd ff:ff:ff:ff:ff:ff
inet 172.16.1.31/24 brd 172.16.1.255 scope global eth1
inet6 fe80::20c:29ff:feb0:740c/64 scope link
valid_lft forever preferred_lft forever
3.5 yum模块
6.3 想想linux命令-----对应的模块
yum install htop -y
第一个里程碑-翻译为ansible命令
ansible all -m shell -a "yum install htop -y" #ansible all -m yum -a "name=cowsay,sl"
第二个里程碑-翻译为playbook
- hosts: all
tasks:
- name: yum install htop
yum: name=htop,sl,cowsay
第三个里程碑-执行与测试

浙公网安备 33010602011771号