day 38期中架构第一阶段-ansible自动化管理实践
回顾:
ssh批量管理如何一键完成:
一键创建及分发秘钥:
#!/bin/bash
yum install sshpass -y
ssh-keygen -f ~/.ssh/id_rsa -P '' -q
for ip in 7 8
do
sshpass -p111111 ssh-copy-id -f -i ~/.ssh/id_rsa.pub "-o strictHostKeyChecking=nd172.16.1.$ip"
done
ssh-keygen非交互式创建秘钥对:
具体命令:ssh-keygen -f ~/.ssh/id rsa -P '' -q
命令参数讲解:
ssh-keygen:秘钥对创建工具
[-P old-passphrase] 密码
[-f outputkeyfile] 输出的秘钥文件
[-q] 不输出信息
[-t dsa ] 指定秘钥类型。
ssh-copy-id不需要提示yes/no分发秘钥
具体命令:ssh-copy-id -f -i ~/.ssh/id_rsa.pub -o StrictHostKeyChecking=no 172.16.1.8
参数讲解:
ssh-copy-id -f -i ~/.ssh/id_rsa.pub -o StrictHostKeyChecking=no root172.16.1.8
ssh-copy-id [-f] [-i [identity_file]] [-p port] [[-o <ssh -o options>] ...] [user@]hostname
说明:
-f: force mode 强制
[-i [identity_file]] 指定秘钥文件
[[-o <ssh -o options>] ...] 指定ssh参数选项。
3.sshpass工具:指定密码非人工交互分发秘钥
sshpass -p123456 ssh-copy-id -f -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" 172.16.1.7
[root@web02 ~]# sshpass -help
Usage: sshpass [-f|-d|-p|-e] [-hV] command parameters
sshpass -p123456 ssh-copy-id -f -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" 172.16.1.7
sshpass [-f|-d|-p|-e] [-hV] command parameters
参数讲解:
-p password Provide password as argument (security unwise) #指定用户密码操作
[root@m01 ~]# ssh-copy-id -h
Usage: /usr/bin/ssh-copy-id [-h|-?|-f|-n] [-i [identity_file]] [-p port] [[-o <ssh -o options>] ...] [user@]hostname
-f: force mode -- copy keys without trying to check if they are already installed
-n: dry run -- no keys are actually copied
-h|-?: print this help
sshpass工具:
[root@web02 ~]# sshpass -help
Usage: sshpass [-f|-d|-p|-e] [-hV] command parameters
-f filename Take password to use from file
-d number Use number as file descriptor for getting password
-p password Provide password as argument (security unwise)
-e Password is passed as env-var "SSHPASS"
With no parameters - password will be taken from stdin
-P prompt Which string should sshpass search for to detect a password prompt
-v Be verbose about what you're doing
-h Show help (this screen)
-V Print version information
At most one of -f, -d, -p or -e should be used
sshpass -p111111 ssh-copy-id -f -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=nd172.16.1.7
把web02作为分发服务器:
web02(8)-->m01(61)
web02(8)-->web01(7)
ssh-keygen -f ~/.ssh/id_rsa -P '' -q
ssh-copy-id -f -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" 172.16.1.7
sshpass -p111111 ssh-copy-id -f -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" 172.16.1.7
#!/bin/bash
#yum install sshpass -y
ssh-keygen -f ~/.ssh/id_rsa -P '' -q
for ip in 7 61
do
sshpass -p111111 ssh-copy-id -f -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" 172.16.1.$ip
done
#test
ssh 172.16.1.7 "ifconfig eth0"
ssh 172.16.1.61 "ifconfig eth0"
1、ansible软件介绍:
ssh秘钥认证+脚本批量管理,特点:简单、实用
但是看起来比较LOW,需要人工写脚本,类似实时复制的inotify工具。
2013以前这种方式很普遍
MYSQL高可用 MHA集群,要求所有机器互相秘钥认证。
2、批量管理工具历史
SSH+脚本 CFEngine、Puppet、saltstack、 ansible
08年以前 07-08年 10-13年 14-17(python开发) 16-(python开发)
3、为什么用ansible?
简单、方便、容易学习、功能同样强大。
ansible有配置文件,可以多线程直接实现。不需要写脚本,类似实时复制的sersync.
4、什么是ansible?
Ansible是一个用来远程管理服务器的工具软件。
Ansible是一个用来批量部署远程主机上服务的工具。这里“远程主机(Remote Host) "是指任
何可以通过SSH登录的主机,所以它既可以是远程虚拟机或物理机,也可以是本地主机。
Ansible通过SSH协议实现管理节点与远程节点之间的通信。理论上来说,只要能通过SSH登录到
远程主机来完成的操作,都可以通过Ansible实现批量自动化操作。
涉及管理操作:复制文件、安装服务、服务启动停止管理、配置管理等等。
5、为什么要用批量管理工具运维?
提高效率,省钱SSD+SATA热点存储,
6、Ansible特点
Ansible基于Python语言实现,由Paramiko和PyYAML两个关键模块构建。
Shell、Python是Linux运维学员必会的两门语言。
1)安装部署过程特别简,学习曲线很平坦。
2)不需要单独安装客户端,只是利用现有的SSHD服务(协议)即可。
3)不需要服务端(no servers).
4)ansible playbook,采用yaml配置,提前编排自动化任务。
5)ansible功能模块较多,对于自动化的场景支持丰富
7、Ansible架构介绍(见图说明)
1、连接插件connectior plugins用于连接主机 用来连接被管理端
2、核心模块core modules连接主机实现操作, 它依赖于具体的模块来做具体的事情
3、自定义模块custom modules,根据自己的需求编写具体的模块
4、插件plugins,完成模块功能的补充
5、剧本playbooks, ansible的配置文件,将多个任务定义在剧本中,由ansible自动执行
6、主机清单inventor,定义ansible需要操作主机的范围
最重要的一点是ansible是模块化的它所有的操作都依赖于模块
8、实践环境准备
61(m01)====>31(nfs01)
61(m01)====>41(backup)
9、安装ansible
m01管理机:
yum install epel-release -y
yum install ansible -y
yum install libselinux-python -y
#如果有libselinux-python就不执行下面的命令了。
#rpm -qa |grep libselinux-python
#yum install libselinux-python -y
其他所有机器:
#rpm -qa |grep libselinux-python
#yum install libselinux-python -y
查看:
rpm -qa |grep libselinux-python
10、主机列表配置
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
[root@m01 ~]# cp /etc/ansible/hosts{,.ori}
cat >/etc/ansible/hosts<<EOF
[oldboy]
172.16.1.31
172.16.1.41
172.16.1.7
172.16.1.8
[oldgirl]
172.16.1.31
172.16.1.41
172.16.1.51
[oldboy_pass]
172.16.1.31 ansible_ssh_user=root ansible_ssh_pass=111111
172.16.1.41 ansible_ssh_user=root ansible_ssh_pass=111111
EOF
[root@m01 ~]# cat /etc/ansible/hosts
/etc/ansible/ansible.cfg #ansible的配置文件
11、测试:
[root@m01 /server/scripts]# vim /etc/ansible/ansible.cfg
修改 /etc/ansible/ansible.cfg中的71行 把注释去掉
或者374行 把注释去掉 结尾加-o StrictHostKeyChecking=no
ssh_args = -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no
[root@m01 /server/scripts]# vim /etc/ansible/hosts
[oldboy]
172.16.1.31
172.16.1.41
172.16.1.7
172.16.1.8
[oldgirl]
172.16.1.31
172.16.1.41
172.16.1.51
[oldboy_pass]
172.16.1.31 ansible_ssh_user=root ansible_ssh_pass=111111
172.16.1.41 ansible_ssh_user=root ansible_ssh_pass=111111
[root@m01 /server/scripts]# > ~/.ssh/known_hosts #-----清空ssh密钥文件
[root@m01 /server/scripts]# ansible oldboy_pass -m command -a "free -m"
172.16.1.41 | CHANGED | rc=0 >>
total used free shared buff/cache available
Mem: 972 95 589 7 287 702
Swap: 1999 0 1999
172.16.1.31 | CHANGED | rc=0 >>
total used free shared buff/cache available
Mem: 972 88 699 7 184 721
Swap: 1999 0 1999
[root@m01 /server/scripts]# ansible oldboy -m command -a "free -m"
172.16.1.41 | CHANGED | rc=0 >>
total used free shared buff/cache available
Mem: 972 95 589 7 287 702
Swap: 1999 0 1999
172.16.1.31 | CHANGED | rc=0 >>
total used free shared buff/cache available
Mem: 972 88 699 7 184 721
Swap: 1999 0 1999
结论:使用SSH连接:
密码认证host里主机后面加密码
秘钥认证:提前发公钥,才能用ansible.
12、ansible模块查看和帮助
查看模块
[root@m01 ~]# ansible-doc -l
查看某个模块的具体参数帮助
[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"
ansible命令参数:
-m MODULE_NAME, 模块名字,默认command
-a MODULE_ARGS, 模块参数
-f FORKS 并发进程数,默认5个。
-i INVENTORY(default=/etc/ansible/hosts)指定主机列表文件
command模块
功能说明:
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"
最通用的功能。
不指定密码的话这样配置:
[root@m01 /server/scripts]# vim /etc/ansible/hosts
[oldboy]
172.16.1.31
172.16.1.41
172.16.1.7
172.16.1.8
[root@m01 /server/scripts]# vim /etc/ansible/ansible.cfg
修改 /etc/ansible/ansible.cfg中的71行 把注释去掉
或者374行 把注释去掉 结尾加-o StrictHostKeyChecking=no
生成秘钥对:
[root@m01 ~]# ssh-keygen -f ~/.ssh/id_rsa -P '' -q
[root@m01 ~]# cat ~/.ssh/id_rsa
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA2+lQpapojG6Pt1pC2uOt9x7PBpmPMKBj2G5Z+xEx3eDPfRs3
vNEHEq24xQzdC/GJFtOz+rr3P2t95YAB1dcxC9GmdUJkU0ZZeaDB1Xo0wUjN7w3Y
把要连接的推送过去:
[root@m01 ~]# sshpass -p111111 ssh-copy-id -f -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" 172.16.1.7
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
Number of key(s) added: 1
Now try logging into the machine, with: "ssh -o ' StrictHostKeyChecking=no' '172.16.1.7'"
and check to make sure that only the key(s) you wanted were added.
[root@m01 ~]# sshpass -p111111 ssh-copy-id -f -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" 172.16.1.31
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
Number of key(s) added: 1
Now try logging into the machine, with: "ssh -o ' StrictHostKeyChecking=no' '172.16.1.31'"
and check to make sure that only the key(s) you wanted were added.
[root@m01 ~]# sshpass -p111111 ssh-copy-id -f -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" 172.16.1.41
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
Number of key(s) added: 1
Now try logging into the machine, with: "ssh -o ' StrictHostKeyChecking=no' '172.16.1.41'"
and check to make sure that only the key(s) you wanted were added.
在查看推送的结果:
[root@m01 ~]# ansible oldboy -m command -a "free -m"
172.16.1.31 | CHANGED | rc=0 >>
total used free shared buff/cache available
Mem: 972 87 692 7 192 719
Swap: 1999 0 1999
172.16.1.41 | CHANGED | rc=0 >>
total used free shared buff/cache available
Mem: 972 177 489 7 305 620
Swap: 1999 0 1999
172.16.1.7 | CHANGED | rc=0 >>
total used free shared buff/cache available
Mem: 972 87 506 7 378 698
Swap: 1999 0 1999