ansible自动化运维工具

ansible自动化运维工具

一、ssh工作原理和基本命令

1.ssh原理

ssh:是openssh提供的一种基于公钥和私钥的远程数据加密传输的方式。

ssh传输的原理图

两端传输的数据也会通过公钥和私钥进行加密,从而实现了数据的加密传输
描述用户身份认证的过程:
1.server创建密钥对ssh-keygen -t dsa/rsa 公钥和私钥
2.server分发公钥给客户端 ssh-copy-id -i /root/.ssh/id_dsa.pub [root]@ip
3.client 接收公钥信息 并且把确认返回给server
4.client 发送登录请求 id_dsa.pub(公钥) 发送给server,server拿到id_dsa.pub和自己的id_dsa(私钥进行匹配) 匹配成功建立连接

root@ks_cjd:~# ssh-keygen -t rsa      			//生成密钥 rsa加密
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):  //确认保存位置
Enter passphrase (empty for no passphrase): 		//输入加密的密码
Enter same passphrase again: 				//确认密码
Passphrases do not match.  Try again.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:tOHwYIQKVgq84jMYh++6dH0z+V+ox78wxDMAEQo+RL0 root@ks_cjd
The key's randomart image is:
+---[RSA 2048]----+
|o o=...+o        |
|oo+ oo. .        |
|.+.+ .= o.       |
|+.o .E * oo      |
|++      S  =     |
|.+. .   . . +    |
| oo. . =  .+ .   |
|. o   . + .o+    |
|oo       oo..o.  |
+----[SHA256]-----+
root@ks_cjd:~/.ssh# ls 
id_rsa  id_rsa.pub				//查看密钥生成成功

//分发公钥
root@ks_cjd:~/.ssh# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.209.129
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.209.129 (192.168.209.129)' can't be established.
ECDSA key fingerprint is SHA256:QmRFTi9gOa/8+FTRPqB4mmcpoKb+JAeB1wT7ZIlhI/0.
Are you sure you want to continue connecting (yes/no)? yes   //确认连接       
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
同时也会让你输入密码 
传输成功 

查看一下远程主机的公钥
root@ubuntu:~/.ssh# ls
authorized_keys
同时我们也可以比对client的authorized_keys和server的id_rsa.pub是相同的内容

问题:当我们查看server的目录时发现多了一个名字为known_hosts的文件
查了一下资料表示 之前我们通过ssh-copy-id命令分发公钥时输入了一个yes也就是让我们确认是否将远程主机的信息写入到这个文件中,这个文件的作用是将远程主机的系统内核参数等写入了这个文件中,每次进行身份确认时,openssh会去比对client的信息与konwn_hosts的信息是否一致,如果不一致也不会建立连接(这是openssh的提供的一种安全机制),你不仅仅要有公钥而且要确保你的内核和一些配置参数不能改变才能建立连接。

2.ssh的基本命令

基本命令:(不全)
前面分发时已经介绍了一些基本的命令 现在做一个汇总和补充
1.生成密钥对命令
ssh-keygen -t rsa/dsa
2.ssh-copy-id 分发公钥的命令
ssh-copy-id -i /root/.ssh/id_rsa.pub [root]@ip
3.sshpass 需要安装 apt-get insatll sshpass 实现交互的输入密码
在批量分发公钥时非常常用
sshpass -p*** ssh-copy-id -i /root/.ssh/id_rsa.pub [root]@ip
4.面交互分发公钥还涉及一个确认client的信息是否写入known_hosts文件,接触交互 并且设置不写入的方法
1)修改配置文件(永久的)
vim /etc/ssh/ssh/config
StrictHostKeyChecking=no
UserKnowHostFile /dev/null
2) 临时的修改 并且写入known_hosts 不需要手动输入yes
sshpass -p*** ssh-copy-id -i /root/.ssh/id_rsa.pub [root]@ip -o “StrictHostChecking=no”

二、anible 自动化运维工具

1.ansible 概述

ansible是一款由python开发的自动化的批量管理工具 可以实现批量的命令执行 脚本执行 文件分发 系统配置等功能
本身并没有批量部署的能力 只是提供了一个框架

2.ansible 的部署和基本组成

ansible的部署:
Ubuntu: apt-get install ansible 
Centos: 基于epel源 安装 yum -y install ansible 
root@ks_cjd:/etc/ansible# tree
.
├── ansible.cfg				//ansible的配置文件
├── hosts					//管理主机清单 
└── roles					//角色 

root@ks_cjd:~# ansible --version 
ansible 2.0.0.2
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides


ansible的基本组成:
- hosts :  主机清单 基于ssh密钥的访问的 
- playbooks : 剧本 实现批量的自动化运维的方式 
- 模块 : python 开发 通过具体的模块来实现批量管理的功能 ,确认管理端和被管理端同时安装python 

3.ansible的基本命令和基本用法

语法:
#ansible [主机] [-m 模块] [-a action]
#ansible ip/all/区域/主机地址 -m 模块 -a "操作"

root@ks_cjd:~# ansible 192.168.209.129 -m command -a "hostname"
192.168.209.129 | SUCCESS | rc=0 >>
ubuntu
查看远程主机192.168.209.129的主机名 

4.ansible 模块的介绍

ansible实现批量管理的基础是依赖于python开发的各种模块实现的
介绍一下我们常见的模块 以及模块的用法 我专门开一篇介绍模块, 大家可以根据自己工作和学习中遇到的实用模块在评论区进行补充

模块使用场景
command默认模块 但是在模块中不能使用一些特殊字符
shell万能模块 在远程主机上执行命令的模块
scripts在元辰主机上执行本地脚本的模块
copy批量分发传输数据
fench将远程主机数据拉到管理主机
file修改文件属性 创建数据信息
yum安装软件和卸软件
service管理服务运行状态
user用于批量创建用户并且设置密码
mount挂载模块
cron批量部署定时任务
ping远程测试模块
setup显示被管理主机的详细信息
debug???
查看模块详细信息的命令(help帮助文案)
#ansible-doc -l     		   ----列出模块使用的介绍 
#anisble-doc -s fetch(模块)   ---列出模块的详细说明
#ansible-doc fetch              ---列出模块的用法

5.主机的hosts清单配置

说明:配置主机的hosts清单目录:/etc/ansible/hosts

1)分组配置主机清单 
[data]
172.16.28.1
172.16.28.2
[chenjunde]
172.16.28.3
172.16.28.4
2)主机名的符号匹配 
[web]
172.16.28.[7:9]    		//172.16.28.7 172.16.28.8 172.16.28.9 

3)跟上端口号
[web]
172.16.28.7:22 
4)使用特殊变量
[web]
172.16.28.7 ansible_ssh_pory=22 ansible_ssh_user=root ansible_ssh_pass=123456
可以在没有配置密钥时使用 访问被管理端 
5)主机名的嵌入式配置
[rsync:children]
rsync_server 
rsync_client 
[rsync_server]
172.16.1.41
[rsync_client]
172.16.1.31

三、ansible剧本

1.yaml文件的格式

说明:yaml是一种标记语言 可以用来写配置变量
语法规则:
1.对大小写要求严格
2.使用缩进表示层级关系
3.缩进不允许使用Tab键 而是使用两个space空格来缩进
4.使用键值对的方式

2.playbook剧本

playbook剧本是ansible的脚本文件,通过task调用ansible的模块实现在一个文件中运行多个ansible指令
组成:
1.Tasks 任务 调用模块完成的操作
2.Variable 变量
3. Templates 便于修改配置文件 定义自己需要的内容模板
4. Handlers 处理器 满足雕件时触发执行操作
5. Roles 角色 将其分门别类的存放

案例:先来个小例子熟悉一下ansible 剧本 
- hosts: webservers				#指定我们的hosts组
  remote_user: root			    #指定远程主机用户
  become: yes					#切换用户 cjd     ansible2.6以前使用sudo 2.6后改用yes  
  become_user: cjd
  tasks:
    - name: test connnetc
      ping:
      remote_user: root

执行剧本的命令
ansible-playbook [yaml文件]
ansible-playbook --syntax-check xxx.yaml 检查语法
ansible-playbook -C xxx.yaml 模拟执行
ansible-playbook --list-hosts xxx.yaml 检查生效的主机清单

3.ansible 剧本的tasks

说明:	
			1)ansible-playbook的主体就是task列表,task列表中的各个任务按次序在hosts中指定的主机上执行
			2)playbook从上向下依次运行 失败执行回滚操作 
			3)tasks的目的是使用指定参数执行模块 在模块参数中可以使用变量 
			4)每个task必须有一个名称name 这样在运行playbook时 从其输出的任何任务执行消息中可以辨别属主
#ansible案例:
- hosts: webservers 
	  remote_user: root 
	  tasks: 
	    - name: install httpd 
		  yum: name=httpd 
		- name: start httpd 
		  service: name=httpd state=started 
		  ignore_errors: True		'//添加此项,能够忽略错误,跳过,继续执行'

4.ansible 的handlers 触发器

说明:配置文件被修改后 有可能需要重新重启程序 此时我们可以配置一个handlers 类似触发器
接受notify的信息 一旦notify上面的模块执行 则会出发handerles下面的动作

- hosts: webserver 
  vars:
    http_port: 80 
    max_clients: 200 
  user: root 
  tasks: 
    - name: ensure apahce is at the lastest version 
	  yum: pkg=httpd state=lastest
	- name: write the apache config file 
	  template: src=/srv/httpd.j2 dest=/etc/httpd.conf 
	  notify: 
	  - restart apache 
	- name: ensure apache is running 
	  service: name=httpd state=started 
	handlers:											//触发器 接收notify的signal 
	  - name: restart apache 
	    service: name=httpd state=restart 

5.ansible的变量 vars说明

1.playbook中定义变量

- hosts: webserver 
  vars:
    http_port: 80 
    max_clients: 200 

2.命令行设置变量

ansible-playbook cjd.yaml -e username="cjd"

3.主机清单hosts中配置变量

[webserver]
192.168.126.12 username=cjd

4.变量的引用

{{username}} 引用变量

6.ansible的流程控制

ansible 使用一些条件判断语句when来进行条件的判断
ansible_distribution ansible_distribution_major_version 属于系统内建参数

(1)单条件判断

- hosts: mysql
  remote_user: root
  tasks:
    - name: "shutdown CentOS"
	  command: "/usr/sbin/init 0"		'//当执行这条语句时需满足以下条件'
	  when: ansible_distribution == "CentOS"		'//满足于条件是CentOS'

(2)多条件判断

vim when.yml 
- hosts: webserver
  remote_user: root
  tasks:
    - name: "shutdown CentOS"
	  command: "/usr/sbin/init 0"
	  when:		'//多条件组合,为CentOS8才关机'
	  - ansible_distribution == "CentOS"
	  - ansible_distribution_major_version == "8"

(3)组条件判断

				- hosts: webserver
				  remote_user: root
				  tasks:
				   - name: "shutdown CentOS and Debian"
					 command: "/usr/sbin/init 0"		'//当满足CentOS且版本为6,或者,Debian且版本为7时,执行关机操作'
					 when: (ansible_distribution == "CentOS" and ansible_distribution_major_version == "6") or
						   (ansible_distribution == "Debian" and ansible_distribution_major_version == "7")

7. 迭代 当有重复性的任务时 可以使用迭代机制

		vim when.yaml 
		- hosts: webservers
		remote_user: root
		tasks: 
		  - name: create users 
		    user: name={{item}}
			with_items:
			  - cjd01 
			  - cjd02 
			  - cjd03

很多playbook根据飞哥博客改写: https://xucf1.blog.csdn.net/article/details/115526855

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值