当前位置:网站首页>ansible安装与使用
ansible安装与使用
2022-07-02 04:46:00 【浮沫*】
简介:ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。
本实验由三台主机进行测试,其中一台作为管理机,另外两台被管主机
文章目录
一、ansible安装
1.1 安装epel扩展源
yum install -y epel-release.noarch
1.2 安装ansible
yum install -y ansible
查看ansinle版本
安装完成后,已经多出很多ansible命令了
二、配置主机
2.1 配置hosts文件
添加另外两台主机信息
vi /etc/ansible/hosts
执行ansible命令,其实这种情况把密码明文形式写在配置文件了,不适用
ansible 10.49.33.58 -m ping
第二种配置文件仅一个ip,执行命令要输入一边密码,多主机维护时不方便
ansible 10.49.33.58 -m ping -k
输入密码
第三种情况,可以生成密钥管理,不用输入密码,管理方便
生产密钥
ssh-keygen
生成的密钥默认放在/root/.ssh/
ll /root/.ssh
2.2 管理密钥
id_rsa是私钥
id_rsa.pub是公钥,需要把公钥发送给被管主机
使用模式:
ssh-copy-id [-i [identity_file] ] [[email protected]]machine
-i:指定公钥文件
把本地的ssh公钥文件安装到远程主机对应的账户下:
ssh-copy-id [email protected]
ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]10.49.33.58
执行成功之后,我们在目标机器上查看/root/.ssh目录已经生成,并且多了一个名为authorized_keys的文件,里面保存的正是原机器上ssh-keygen生成的id_rsa.pub的内容
按照系统提示尝试登录10.49.33.58
ssh [email protected]10.49.33.58
直接进入,不再需要密码
同理,把公钥传到10.49.33.59中
ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]10.49.33.59
公钥认证的相关操作配置完成,可以通过ansible主机免密码连接到这些主机,极大的提高了安全性
2.3 被管主机分组
还可以对不同主机进行分组,默认配置文件也有提示
ansible test -m ping
至此,简单的ansible搭建管理完成
三、ansible组成介绍
3.1 查看ansible目录文件
tree /etc/ansible/
/etc/ansible/
├── ansible.cfg # 配置文件
├── hosts # 主仓库 用来存储需要管理的远程主机的相关信息
└── roles # 存放角色的目录
3.2 Ansible命令参数详解
Ansible命令语法如下:
ansible [-i 主机文件] [-f 批次] [组名] [-m 模块名称] [-a 模块参数]
Ansible详细参数如下:
-i inventory_file——指定了主机文件,如果不指定,则默认为/etc/ansible/hosts文件,在主机文件中,定义了Ansible要控制的主机IP或域名,及其用户、密码
-f 10——表示指定开启同步进程的个数
-m module——表示Ansible要调用的模块
-a ——指定模块的参数,可以是命令等等
-sudo ——表示使用ansible获得sudo权限
-k ——表示ansible使用的SSH密码
-u username——表示指定Ansible执行的用户
-C —— 表示命令测试
3.3 ansible模块功能查看
Ansible的模块有很多,查看所有ansible的模块,使用命令:
ansible-doc -l
有非常多的模块,左边的是模块名称,右边的是对模块功能的描述
详细查看一个模块的功能,并且查看其参数,可以执行命令:
ansible-doc -s [模块名]
3.4 hosts文件
hosts文件一般存放我们的被管理主机信息。主机列表清单,也叫Inventory。所有被管理的主机都需要定义在该文件中。如果不想使用默认清单的话可以用-i选项指定自定义的清单文件,防止多人混合使用一个主机清单。如果没有定义在主机列表文件中,执行命令会提示“No hosts matched”
谢谢
边栏推荐
- What are the rules and trading hours of agricultural futures contracts? How much is the handling fee deposit?
- LeetCode-对链表进行插入排序
- Solution of DM database unable to open graphical interface
- MySQL table insert Chinese change? Solution to the problem of No
- C language guessing numbers game
- DMA Porter
- 【提高课】ST表解决区间最值问题【2】
- Summary of main account information of zhengdaliu 4
- Use of Baidu map
- 60后关机程序
猜你喜欢
Record the bug of unity 2020.3.31f1 once
Keil compilation code of CY7C68013A
Summary of main account information of zhengdaliu 4
Getting started with pytest -- description of fixture parameters
Introduction to Luogu 3 [circular structure] problem list solution
Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products
Hcip day 17
Several methods of capturing packets under CS framework
CorelDRAW graphics suite2022 free graphic design software
Federal learning: dividing non IID samples according to Dirichlet distribution
随机推荐
How do I interview for a successful software testing position? If you want to get a high salary, you must see the offer
Pytorch-Yolov5從0運行Bug解决:
Social media search engine optimization and its importance
I sorted out some basic questions about opencv AI kit.
win11安装pytorch-gpu遇到的坑
unable to execute xxx. SH: operation not permitted
二叉树解题(二)
Flag bits in assembly language: CF, PF, AF, ZF, SF, TF, if, DF, of
Leetcode- insert and sort the linked list
DC-1靶场搭建及渗透实战详细过程(DC靶场系列)
Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products
Getting started with pytest ----- confitest Application of PY
10 minute quick start UI automation ----- puppeter
How to write a client-side technical solution
Unit testing classic three questions: what, why, and how?
Hcip day 17
TypeScript类的使用
My first experience of shadowless cloud computer
6月书讯 | 9本新书上市,阵容强大,闭眼入!
千亿市场规模医疗美容行业的水究竟有多浑?