当前位置: 代码网 > it编程>编程语言>Java > Ansible安装基本原理及操作(初识)

Ansible安装基本原理及操作(初识)

2024年08月02日 Java 我要评论
Ansible 是一款功能强大且易于使用的IT自动化工具,可用于配置管理、应用程序部署和云端管理。它使用无代理模式(agentless mode)来完成任务,这意味着您无需在目标主机上安装任何额外的软件。Ansible 通过 SSH 连接到目标主机并执行任务。

作者主页: 

ansible专栏:点击!

创作时间:2024年4月23日15点18分


 

ansible 是一款功能强大且易于使用的it自动化工具,可用于配置管理、应用程序部署和云端管理。它使用无代理模式(agentless mode)来完成任务,这意味着您无需在目标主机上安装任何额外的软件。ansible 通过 ssh 连接到目标主机并执行任务。

什么是无代理模式?

ansible 的无代理模式是一种无需在目标主机上安装任何额外软件的管理方式。与传统的基于代理的配置管理工具不同,ansible 通过 ssh 连接到目标主机并执行任务。

ansible 的工作原理

可以概括为以下几个步骤:

ansible 的核心组件

ansible 的核心组件包括:

ansible 的优势

ansible 具有以下优势:

1.安装教程

1.首先第一步配置网络源(需要扩展包)

我之前写过配置源的文章:
配置方法icon-default.png?t=n7t8http://t.csdnimg.cn/uqi7c

[root@localhost ~]# yum install -y ansible

安装完成之后查看是否成功

rpm -ql ansible	列出他的所有相关文件

rpm -qc ansible	列出配置文件

ansible-doc -l	 查看它的所有模块

安装完成之后我们来做一个小测试带大家理解ansible

2实验

以下实验环境

3台centos7

1台用于部署ansible服务器

2台用来被控制(其中一台免密登录,一台需用账户和密码登录)

主机名映射

[root@ansible ~]# vim /etc/hosts

1.设置ssh-key

ssh密钥(secure shell key)是ssh(secure shell)协议中用于身份验证的凭证。与传统的基于密码的身份验证相比,它提供了一种更安全的方式连接到远程服务器。

[root@localhost ~]# ssh-keygen
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: 
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:
47:b6:e3:55:27:5c:8f:93:03:be:fc:87:28:a8:6d:cc root@localhost.localdomain
the key's randomart image is:
+--[ rsa 2048]----+
|            .   .|
|           . o +.|
|          o . o o|
|         o o o = |
|        s + +    |
|         + o o . |
|       o. o . o .|
|       oe  .   . |
|      ...        |
+-----------------+

查看是否成功

[root@localhost ~]# ls .ssh/
id_rsa  id_rsa.pub
[root@localhost ~]# ssh-copy-id 192.168.93.112
the authenticity of host '192.168.93.112 (192.168.93.112)' can't be established.
ecdsa key fingerprint is e8:64:5f:06:f8:8c:63:6d:c9:eb:73:7d:78:d5:93:2b.
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@192.168.93.112's password: 

number of key(s) added: 1

now try logging into the machine, with:   "ssh '192.168.93.112'"
and check to make sure that only the key(s) you wanted were added.
2.定义主机清单
[root@localhost ~]# vim /etc/ansible/hosts 

在配置文件的最后一行加入如下配置

3.测试连通性
免密登录

ping的结果显示绿色就是成功的

[root@localhost ~]# ansible localhost -m ping	对本机进行测试
localhost | success => {
    "changed": false, 
    "ping": "pong"
}
[root@localhost ~]# ansible host1 -m ping		对host1进行测试

the authenticity of host 'host1 (192.168.93.112)' can't be established.
ecdsa key fingerprint is e8:64:5f:06:f8:8c:63:6d:c9:eb:73:7d:78:d5:93:2b.
are you sure you want to continue connecting (yes/no)? yes
host1 | success => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },    
    "changed": false, 
    "ping": "pong"

简洁化显示

[root@localhost ~]# ansible host1 -m ping -o
host1 | success => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
用户名密码登录
[root@localhost ~]# ansible host2 -m ping -o -u root -k

出现报错的情况下

host2 | failed! => {
    "msg": "using a ssh password instead of a key is not possible because host key checking is enabled and sshpass does not support this.  please add this host's fingerprint to your known_hosts file to manage this host."
}

解决办法

这个时候需要登录一次 再使用上边的命令才可以成功(因为你的密码自动输出给yes/no的选项中)

[root@localhost ~]# ansible host2 -m ping 
the authenticity of host 'host2 (192.168.93.113)' can't be established.
ecdsa key fingerprint is e8:64:5f:06:f8:8c:63:6d:c9:eb:73:7d:78:d5:93:2b.
are you sure you want to continue connecting (yes/no)? yes
host2 | success => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

一并输出

[root@localhost ~]# ansible all -m ping -o

这边报错正常的因为 host2没有设置免密登录需要账号密码来登录

(0)

相关文章:

  • 一文告诉你Golang和Java的区别

    一文告诉你Golang和Java的区别

    Golang和Java是两种非常流行的编程语言,它们分别具有自己的优点和特点。是否选择Golang或Java作为开发语言,往往要基于项目的实际需求和具体情况来进... [阅读全文]
  • 计算机必背单词——网络和安全

    计算机必背单词——网络和安全

    这些都是我认为程序员需要掌握的单词,就算有些英文你不熟悉,但是对应的中文至少了解什么意思。看完这个系列,希望你第一能认识更多单词,第二是拓宽自己的知识面,哪个概... [阅读全文]
  • 怎样正确做 Web 应用的压力测试?

    提到,我们想到的是压力测试,其实这是片面的,。为了让大家看完文章后,更有获得感,本文将从以下方面进行展开:1、Web应用(网站)压测范围2、前后端压测流程3、补充1:前端4、补充2…

    2024年08月02日 编程语言
  • 压力测试+接口测试(工具jmeter)

    是apache公司基于java开发的一款开源压力测试工具,体积小,功能全,使用方便,是一个比较轻量级的测试工具,使用起来非常简单。因 为jmeter是java开发的,所以运行的时候…

    2024年08月02日 编程语言
  • 黑客自学手册(网络安全)

    黑客自学手册(网络安全)

    此教程为纯技术分享!本教程的目的决不是为那些怀有不良动机的人提供及技术支持! [阅读全文]
  • Postman使用教程【项目实战】

    Postman 是一个多功能的 API 开发工具,它不仅可以帮助你测试 API,还可以提高开发效率和团队协作。通过本文的教程,你应该能够掌握 Postman 的基本使用,并开始利用…

    2024年08月02日 编程语言

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com