CLOUD 云计算进阶(六)-ansible基础 、 ad-hoc 、 批量配置管理

本文详细介绍Ansible自动化运维工具的基础知识,包括其优势、工作流程、模块化设计及Python开发特性。涵盖Ansible的安装部署、主机定义与分组、批量检测主机、SSH密钥部署及动态主机配置等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

				 ARCHITECTURE 001   ansible基础 、 ad-hoc 、 批量配置管理

DevOps:15K-起步
GIT->Ansible->Python->…
/usr.bin/python ->RHEL系统  yum 的原生编程语言


一 ansible 简介

1 Ansible 自动化运维,一款批量搞机的工具  2013年推出的一款自动化和DevOps软件,2015被redhat 收购,基于python开发,实现了批量操作系统配置,批量程序部署,批量运行命令.
Ansible: 可以实现  
1自动化部署APP
 2 自动化管理配置项
 3 自动化持续交付
 4 自动化AWS 云服务管理

2 Ansible  优点
只需要Ssh/python 配合即可使用
无客户端
上手容易.,门槛低
功能强大,模块丰富
基于python 开发,做二次开发更容易
使用公司多,社区活跃

Ansible 特性
1 模块化设计,调用特定的模块完成特定任务
2 基于python 语言实现
3 其模块支持标准输出格式,可以采用任何编程语言重写
4 部署简单
5 主从模式工作
6 支持自定义模块
7 支持playbook
8 易于使用
9 支持多层部署
10 支持异构IT环境

3 Ansible 工作流程 :两大核心指标
主机集合 :host1 host2 host3 hostn
命令集合 :cmd1 cmd2 cmd3 cmdn
主机的执行是并发的,而命令是顺序的
在这里插入图片描述

4软件依赖关系
对于管理主机:要求python 2.6以上
安装模块:paramiko
PyYAML
Jinja2
Httplib2
Six

对于被托管主机
Ansible 默认通过Ssh协议管理机器
被管理主机需要开启ssh ,允许ansible 主机登录 
在托管节上需要安装Python2.5以上版本呢
如果托管节点开启了Selinux,需要安装libselinux-python


二 部署安装Ansible
1自定义yum源
把软件包拷贝到真机 /var/ftp/ansible
Createrepo --update .

2 安装验证
Ansible 托管主机配置yum源
[local]
name=local
baseurl=“ftp://192.168.1.254/ansible
enabled=1
gpgcheck=0
安装软件:yum -y install ansible
验证  :ansible --version


环境准备:6台虚拟机主机 ,1.5G以上内存,10G以上那个硬盘,

在这里插入图片描述


三 主机定义与分组

1 )Ansible 配置文件查找顺序:
1 ANSIBLE_CONFIG
2 ./ansible.cfg
3 ~/ansible.cfg
4 最后是默认配置文件 /etc/ansible/ansible.cfg


配置文件 Vim /etc/ansible/ansible.cfg
Ansible.cfg 配置文件
Inventory 定义托管主机地址配置文件路径名
Inventory指定的配置文件,写入远程主机的地址
Ssh 主机key 验证配置参数
Host_key_checking =False
   
去注释两行     inventory = /etc/ansible/hosts
    Host_key_checking = Flase
1 分组定义范围
Vim /etc/ansible/hosts
[web]
Web1
Web2

[db]
Db1
Db2
Ansible 命令基础
Ansible 主机集合  -m 模块名称  -a 模块参数
主机集合  主机名或者分组名,多个使用"逗号"分割
Absible  常见的选项
     -m 模块
-a 模块参数
 -i inventory 文件路劲
 -k 使用交互式登录密码
 -v 显示详细信息
 -e 定义变量

2 批量检测主机 Ansible -m ping -k

列出要执行的主机
Ansible web --list-host
Db --list-host
All --list-host

3 部署证书文件
Ansible 是通过SSH 在远程执行命令的
SSH 远程执行命令必须要通过验证才行
密码写入配置文件安全性很差
使用key方式验证
给所有主机部署公钥
-没有秘钥命令执行会出错
-ansible web -a ‘uptime’

主机ansible 操作 Cd /root/.ssh
创建秘钥 Ssh-keygen -t rsa -b 2048 -N ‘’ -f key
给所有主机部署秘钥 Ssh-copy-id -i key.pub web1 /web2 /db1/db2 cache

Ssh-keygen dsa | rsa
Ssh-copy-id web1


SSH是建立在应用层和传输层基础上的安全协议,其目的是专为远程登录会话和其他网络服务提供安全性的保障,用过SSH远程登录的人都比较熟悉,可以认为SSH是一种安全的Shell。SSH登录是需要用户名和密码的,要实现无密码登录,就需要创建SSH 密钥(ssh key),SSH 密钥可以认为是和另一台电脑通信时的唯一的识别证,SSH 密钥对可以让我们方便的登录到 SSH 服务器,而无需输入密码。GIT源代码管理就是使用此种安全机制,本文本将介绍SSH KEY的生成过程。

  1. 检查是否生成过ssh 密钥

生成ssh 密钥后,会在当前用户的根目录下创建.ssh目录。因此,可以通过以下两种方式检查是否生成过ssh 密钥。

其一,是否可切换到.ssh目录:

cd ~/.ssh

如果.ssh目录则会切换到对应目录,不存在会有相关提示。

其二,使用ls命令检查:

ls -l ~/

通过以上两种方式检查后,当存在时,可以继续下面的操作重新生成ssh 密钥,也可以使用已经生成的ssh 密钥。

  1. 使用ssh-keygen命令生成ssh 密钥

使用ssh-keygen命令生成ssh 密钥可以使用以下命令:

ssh-keygen -t rsa

也可以使用以下命令在生成时添加注释:

ssh-keygen -t rsa -C “我的SSH密钥”

上面两个命令都可以生成ssh 密钥,输入上面命令后,连按三个空格即可生成ssh 密钥。

生成ssh 密钥后,可以到~/.ssh目录下查看相关文件,一般来说ssh 密钥会包含id_rsa和id_rsa.pub两个文件,分别表示生成的私钥和公钥。

在git等源代码管理中,使用cat ~/.ssh/id_rsa.pub命令,打印并将相应内容复制到源代码管理服务器即可实现git的无密码管理。

  1. ssh-keygen命令的一些说明

ssh-keygen命令可以生成rsa或dsa两种格式的密钥。在上面的示例中,使用-t rsa参数生成了id_rsa和id_rsa.pub两个文件,分别表示rsa私钥和rsa公钥。同理,可以使用-t dsa参数生成dsa私钥和dsa公钥,生成的文件名分别是:id_dsa、id_dsa.pub。

在上面的示例中,还通过-C参数添加了密钥的注释,如果不需要这个参数也可以省略。

除前面用到的两个参数外,ssh-keygen命令还有其它一些参数,具体说明请使用ssh-keygen --help命令查看


4 部署inventory 拓展参数

主机定义分组

Db2 主机操作:这里生产环境修改ssh端口为了安全保障
vim /etc/ssh/sshd_config
ssh
Systemctl restart sshd

Inventory 参数:
比如 1 ansible_ssh_port
2 Ansible_ssh_user
3 ansible_ssh_pass
4 ansible_ssh_private_key_file
5 vars
6 children -子组定义
Absible主机操作
Vim /etc/ansible/hosts
[web]
web1
web2
[db]
db1
db2 ansible_ssh_port=222 -变量引用

[other]
cache

[phoneapp:children]   -子组定义
Web
Db

[all:vars]
ansible_ssh_private_key_file="/root/.ssh/key"

动态主机:
Ansible Inventory 包含静态和动态的Invenrory ,
Json (Javascript Object Notion Javascript 对象表示法)
一种基于文本独立与语言的轻量级数据交换格式


批量配置管理
Ansible 常见核心模块:ansible-doc ping
Command
Shell
Script
Copy
Lineinfile|replace
Yum
Service
setup
ansible-doc 模块
Ansible-doc -l

Ping模块
Ansible web -m ping

Command 默认模块
不能解析系统变量
不启动shell直接在ssh 进程中执行,所有使用到shell都会执行失败
Ansible all -m command -a ‘pa aux | grep ssh ’

Shell 模块 基本用法和command一样,区别是shell模块是通过/bin/bash执行命令
可以执行任意命令
不能执行交互式命令  ,例如vim top
查看所有机器负载
Ansible all -m shell -a ‘uptime’

变量解析::

ansible web -m shell -a “echo ${HOSTNAME}”
ansible web -m shell -a ‘echo ${HOSTNAME}’
ansible web -m shell -a ‘echo ${HOSTNAME}’
ansible web -m shell -a “echo ‘${HOSTNAME}’”

We were fortunate enough to be able to talk with a lot of very talented people while preparing this book for you. We spoke not only to people who implemented their own cloud solutions, but also those who help develop cloud solutions, as well as officers of several major cloud solution providers. First, we want to thank Darren Boeck of McNeilus Steel. He told us how he was able to build a virtualized environment for the clients on his network, allowing us to summarize his experiences for you. We also had the pleasure of speaking again with John Engates, CTO of Rackspace Hosting. He talked with us about Rackspace’s cloud computing services and explained their vision of how they see cloud computing as it exists today and how it will continue to evolve. Steve Fisher, senior vice president of AppExchange at Salesforce.com, was a wonderful resource, talking to us not only about what Salesforce.com brings to the cloud party, but also how businesses are using it and how he sees it evolving. Ryan Collier, member of Accenture’s Citrix XenServer Capability Group, enlightened us about the merits of the Citrix XenServer, as well as how he sees the cloud developing. Christopher Crowhurst, Vice President of Architecture & BSI at Thomson Reuters, shared his thoughts on cloud computing. He also shared a case study outlining a very forward-thinking implementation of a cloud-based service at Thompson Reuters that was successfully completed back when the words “Cloud Computing” were rarely uttered. Charles Babcock, our Technical Editor, helped us keep the facts straight and the overall content aligned with your expectations. We appreciate every edit and suggestion and know that it is a better book because of his input. We also acknowledge the patience and hard work of the people at McGraw-Hill, notably those involved in the process of bringing this book to fruition. Thanks to Wendy Rinaldi, Sponsoring Editor and Editorial Director; Patty Mon, Editorial Supervisor; Jean Bodeaux, Senior Production Supervisor; Vipra Fauzdar, Production Project Manager; Joya Anthony, Acquisitions Coordinator; and Margaret Berson, Copy Editor. You can also flip back a few pages to see the rest of the folks who worked their magic throughout the process of creating this book for you. We acknowledge all of them and appreciate their efforts.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

~上善若水~~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值