1.ansible 3W1H

1.1 ansible介绍

ssh秘钥认证+脚本批量管理,特点:简单、实用

但是看起来比较low,需要人工写脚本,类似实时复制的inotify工具。

2013以前这种方式很普遍。

MySQL高可用MHA集群,要求所有机器互相密钥认证。

大数据集群也需要。

1.2 批量管理工具历史

ssh+加脚本 CFEngine Puppet saltstack ansible

08年以前 07-08年sina网 10-13年 14-17(python开发) 16-(python开发)

1.3 为什么用ansible?

简单、方便、容易学习、功能同样强大。

ansible有配置文件,可以多线程直接实现。不需要写脚本,类似实时复制的sersync。

1.4 什么是ansible?

Ansible是一个用来远程管理服务器的工具软件。

Ansible是一个用来批量部署远程主机上服务的工具。这里“远程主机(Remote Host)”是指任

何可以通过SSH登录的主机,所以它既可以是远程虚拟机或物理机,也可以是本地主机。

Ansible通过SSH协议实现管理节点与远程节点之间的通信。理论上来说,只要能通过SSH登录到

远程主机来完成的操作,都可以通过Ansible实现批量自动化操作。

涉及管理操作:复制文件、安装服务、服务启动停止管理、配置管理等等。

1.5 为什么要用批量管理工具运维?

提高效率,百度几万台服务器,阿里几十万台服务器。

如何省钱?

SSD+SATA 热点存储 15 15 7

1.6 Ansible特点

Ansible基于Python语言实现,由Paramiko和PyYAML两个关键模块构建。

1)安装部署过程特别简单,学习曲线很平坦。

2)不需要单独安装客户端,只是利用现有的SSHD服务(协议)即可。

3)不需要服务端(no servers)。

4)ansible playbook,采用yaml配置,提前编排自动化任务。

5)ansible功能模块较多,对于自动化的场景支持丰富。

无服务端,没有进程

1.7 Ansible架构介绍(见图说明)

ansible批量windows ansible批量管理windows主机_服务器

1、连接插件connectior plugins用于连接主机 用来连接被管理端

2、核心模块 core modules 连接主机实现操作, 它依赖于具体的模块来做具体的事情

3、自定义模块 custom modules,根据自己的需求编写具体的模块

4、插件 plugins,完成模块功能的补充

5、剧本 playbooks,ansible的配置文件,将多个任务定义在剧本中,由ansible自动执行

6、主机清单 inventor,定义ansible需要操作主机的范围

最重要的一点是 ansible是模块化的 它所有的操作都依赖于模块

(懂Python可以二次开发)

2.部署实时

2.1 ansible实践环境准备

61(m01)====>31(nfs01)

61(m01) ====>41(backup)

2.2 安装ansible

m01管理机:

yum install epel-release -y

yum install ansible -y

#如果有libselinux-python就不执行下面的命令了。

#rpm -qa |grep libselinux-python

#yum install libselinux-python -y

其他所有机器:

#rpm -qa |grep libselinux-python

#yum install libselinux-python -y

2.3 主机列表配置

ssh列表实现方法:


for n in 7 8 do echo 172.16.1.$n done


/etc/ansible/hosts主机资产清单文件,用于定义被管理主机的认证信息,

例如ssh登录用户名、密码以及key相关信息。如何配置Inventory文件

1.主机支持主机名通配以及正则表达式,例如web[1:3].oldboy.com代表三台主机

2.主机支持基于非标准的ssh端口,例如web1.oldboyedu.com:6666

3.主机支持指定变量,可对个别主机的特殊配置,如登陆用户\密码

4.主机组支持指定变量[group_name:vars],同时支持嵌套组[game:children]

实践:

cp /etc/ansible/hosts{,.ori}

cat >/etc/ansible/hosts<<EOF

[oldboy]

172.16.1.31

172.16.1.41

[oldgirl]

172.16.1.31

172.16.1.41

172.16.1.51

EOF

/etc/ansible/ansible.cfg #ansible的配置文件

2.4 小试牛刀

直接执行如下命令或报错

ansible oldboy -m command -a "free -m"

基于SSH秘钥认证的前提下:

如果没有做SSH秘钥认证,可以把用户名密码写到

/etc/ansible/hosts

[oldboy_pass]

172.16.1.7 ansible_ssh_user=root ansible_ssh_pass=123456

172.16.1.8 ansible_ssh_user=root ansible_ssh_pass=123456

特殊端口:

[oldboy]

172.16.1.7 ansible_port=52113 ansible_user=root ansible_ssh_pass=123456

172.16.1.8 ansible_port=52113 ansible_user=root ansible_ssh_pass=123456

ansible oldboy -m command -a "free -m" 要想成功,先解决yes/no的问题。

1、ssh连接一遍。

2、ssh -o 参数

ansible关闭ssh首次连接时yes/no提示

使用ssh连接时,可以使用-o参数将StrictHostKeyChecking设置为no,

避免使用ssh连接时避免首次连接时让输入yes/no部分的提示。

方法1:修改 /etc/ansible/ansible.cfg中的374行

结尾增加: -o StrictHostKeyChecking=no

ssh_args = -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no

374行改为:

ssh_args = -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no

方法2:修改 /etc/ansible/ansible.cfg中的71行

70 # uncomment this to disable SSH key host checking

71 #host_key_checking = False

71行的注释取消:host_key_checking = False

结论:使用SSH连接:

密码认证 host里主机后面加密码 Paramiko模块 重点:

秘钥认证:提前发公钥,才能用ansible. SSHPASS工具

How to build your inventory — Ansible Documentation

基于SSH秘钥认证的实践

一键创建及分发秘钥:


#!/bin/bash ssh-keygen -f ~/.ssh/id_rsa -P '' -q for ip in 7 8 41 31 do sshpass -p123456 ssh-copy-id -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" 172.16.1.$ip done


2.5 ansible命令参数

-m MODULE_NAME, 模块名字,默认command

-a MODULE_ARGS, 模块参数

-f FORKS 并发进程数,默认5个。

-i INVENTORY(default=/etc/ansible/hosts)指定主机列表文件

===================

ansible

ansible-doc

ansible-playbook

ansible-galaxy

===================

3.ansible模块查看和帮助

查看模块列表

ansible-doc -l #查找shell模块在指令行/shell。

查看某个模块的具体参数帮助

ansible-doc -s command #Linux命令参数

3.1 command模块

1)功能说明:

command Executes a command on a remote node

功能说明:执行一个命令在远程节点上

操作实践:

ansible oldboy -m command -a "free -m"

ansible oldboy -m command -a "df -h"

ansible oldboy -m command -a "ls /root"

ansible oldboy -m command -a "cat redhat-release"

ansible oldboy -m command -a "cat /etc/redhat-release"

最通用的功能。

特殊:不支持的东西,例如 > < | &等 $HOME,替代方案用shell模块

ansible oldboy -m shell -a "ps -ef|grep ssh"

ansible oldboy -m shell -a "echo oldboy >/tmp/a.log"

2)模块常用参数说明及实践

[root@m01 ~]# ansible-doc -s command

- name: Executes a command on a remote node

command:

argv: # Allows the user to provide the command as a list vs. a string. Only the

string or the list form can be provided, not

both. One or the other must be provided.

chdir: # Change into this directory before running the command.

creates: # A filename or (since 2.0) glob pattern. If it already exists, this step

*won't* be run.

free_form: # (required) The command module takes a free form command to run. There is no

parameter actually named 'free form'. See the

examples!

removes: # A filename or (since 2.0) glob pattern. If it already exists, this step *will* be run.

stdin: # Set the stdin of the command directly to the specified value.

warn: # If command_warnings are on in ansible.cfg, do not warn about this particular

line if set to `no'.

参数:chdir=/tmp配置相当于cd /tmp

[root@m01 ~]# ansible oldboy -m command -a "pwd chdir=/etc"

ansible oldboy -m shell -a "cd /etc/;pwd"

参数:creates=/etc 相当于条件测试 [ -e /etc ]||pwd 和下面removes相反

[root@m01 ~]# ansible oldboy -m command -a "pwd creates=/etc"

参数:removes=/root 相当于条件测试 [ -e /root ]&&ls /root

ansible oldboy -m command -a "ls /root removes=/root"

ansible oldboy -m shell -a "[ -d /etc ]||pwd"

[root@m01 ~]# ansible oldboy -m command -a "cat /etc/hosts removes=/etc/hosts"

参数:warn=False 忽略警告

[root@m01 ~]# ansible oldboy -m command -a "chmod 000 /etc/hosts warn=False"

更多官方链接:http://docs.ansible.com/ansible/latest/command_module.html或ansible-doc -s command

3.2 shell模块

功能说明:在执行一个命令在远程节点上

shell Execute commands in nodes.

官方链接:http://docs.ansible.com/ansible/latest/shell_module.html

ansible批量windows ansible批量管理windows主机_服务器_02

远程执行脚本:脚本必须在远端存在

ansible oldboy -m shell -a "/bin/bash /server/scripts/setup.sh"

3.3 copy模块

copy模块功能说明:

功能说明:复制文件到远程主机

官方链接:http://docs.ansible.com/ansible/latest/copy_module.html

ansible oldboy -m copy -a "src=/server dest=/ mode=ugo+x group=root owner=root"

ansible oldboy -m copy -a "src=/server/scripts/setup dest=/server/scripts mode=ugo+x group=root owner=root backup=yes"

ansible批量windows ansible批量管理windows主机_ssh_03

ansible批量windows ansible批量管理windows主机_ssh_04

3.4 script模块

功能说明:在远程节点上运行本地脚本

官方链接:http://docs.ansible.com/ansible/latest/script_module.html

远端可以没有脚本,本地有就行:

[root@m01 /server/scripts]# cat setup.sh

pwd

ls /root

for n in {1..100}

do

echo $n >>/tmp/oldboy.log

done

执行:

ansible oldboy -m script -a "/server/scripts/setup.sh"

3.5 file模块

功能说明:设置文件属性

官方链接:http://docs.ansible.com/ansible/latest/file_module.html

ansible oldboy -m file -a "dest=/tmp/oldboy_dir state=directory"

ansible oldboy -m command -a "mkdir -p /tmp/oldboy_dir1 warn=false"

ansible批量windows ansible批量管理windows主机_运维_05

替代方案:

ansible oldboy -m command -a "chmod 777 /etc/hosts warn=false"

ansible oldboy -m command -a "chmod 644 /etc/hosts warn=false"

ansible oldboy -m command -a "chown oldboy /etc/hosts warn=false"

ansible oldboy -m command -a "chown root /etc/hosts warn=false"

创建目录:mkdir /tmp/oldboy_dir

ansible oldboy -m file -a "dest=/tmp/oldboy_dir state=directory"

递归设置权限:

ansible oldboy -m file -a "dest=/tmp/oldboy_dir state=directory mode=644 recurse=yes"

创建文件:touch /tmp/oldboy_file

ansible oldboy -m file -a "dest=/tmp/oldboy_file state=touch"

删除文件:rm -f /tmp/oldboy_file

ansible oldboy -m file -a "dest=/tmp/oldboy_file state=absent"

创建链接文件:ln -s /etc/hosts /tmp/link_file

ansible oldboy -m file -a "src=/etc/hosts dest=/tmp/link_file state=link"

ansible oldboy -m file -a "dest=/tmp/oldboy_file state=touch owner=oldboy group=oldboy mode=000"

ansible oldboy -m file -a "dest=/tmp/oldboy_file state=touch owner=oldboy group=oldboy mode=ugo=rwx"

3.5 yum模块

功能说明:yum包管理模块

官方链接:http://docs.ansible.com/ansible/latest/yum_module.html

ansible oldboy -m yum -a "name=nginx state=installed"

[root@nfs01 oldboy_dir]# rpm -qa nginx

nginx-1.10.2-1.el6.x86_64

###不要用yum卸载,用rpm -e卸载。

3.6 systemd模块(service模块)

功能说明:启动停止服务

官方链接:http://docs.ansible.com/ansible/latest/service_module.html

#相当于

#service crond stop|/etc/init.d/crond stop

#chkconfig crond off

ansible oldboy -m service -a "name=crond state=stop enabled=no"

#相当于/etc/init.d/crond start

chkconfig crond on

ansible oldboy -m service -a "name=crond state=started enabled=yes"

ansible oldboy -m systemd -a "name=crond.service state=started enabled=yes"

ansible oldboy -m command -a "name=crond state=started enabled=yes"

ansible oldboy -m command -a "systemctl status cround"

3.7 cron模块

功能说明:管理定时任务条目信息模块

官方链接:http://docs.ansible.com/ansible/latest/cron_module.html

定时任务格式:

* * * * * CMD

创建定时任务:

ansible oldboy -m cron -a "name='sync time' minute=00 hour=00 job='/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1'"

结果:

#Ansible: sync time

00 00 * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1

添加如下定时任务:

05 03 * * * /bin/sh /server/scripts/backup.sh /server/scripts/list >/dev/null 2>&1

命令如下:

ansible oldboy -m cron -a "name='backup data' minute=05 hour=03 job='/bin/sh /server/scripts/backup.sh /server/scripts/list >/dev/null 2>&1'"

结果:

#Ansible: backup data

05 03 * * * /bin/sh /server/scripts/backup.sh /server/scripts/list >/dev/null 2>&1

删除定时任务:

ansible oldboy -m cron -a "name='backup data' state=absent"

4.一键部署实战

实战项目一分钟内完成:

  • 各一键完成rsync服务端和客户端。
  • 各一键完成nfs服务端和客户端。
  • 各一键完成sersync服务端和客户端。

一个脚本one_key.sh或者一个ansible。完成

自己写完后,重看Day40的视频课程