Ansible 是一款功能强大且易于使用的IT自动化工具,可用于配置管理、应用程序部署和云端管理。它使用无代理模式(agentless mode)来完成任务,这意味着您无需在目标主机上安装任何额外的软件。Ansible 通过 SSH 连接到目标主机并执行任务。
什么是无代理模式?
Ansible 的无代理模式是一种无需在目标主机上安装任何额外软件的管理方式。与传统的基于代理的配置管理工具不同,Ansible 通过 SSH 连接到目标主机并执行任务。
Ansible 的工作原理
可以概括为以下几个步骤:
Ansible 的核心组件
Ansible 的核心组件包括:
Ansible 的优势
1.首先第一步配置网络源(需要扩展包)
[root@localhost ~]# yum install -y ansible
安装完成之后查看是否成功
rpm -ql ansible 列出他的所有相关文件
rpm -qc ansible 列出配置文件
ansible-doc -l 查看它的所有模块
以下实验环境
3台Centos7
1台用于部署ansible服务器
2台用来被控制(其中一台免密登录,一台需用账户和密码登录)
主机名映射
[root@ansible ~]# vim /etc/hosts
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.
`root@localhost ~# vim /etc/ansible/hosts
`
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"}`
ansible
:这是 Ansible 命令行工具。host1
:指定要 ping 的目标计算机的主机名或 IP 地址。-m ping
:此选项告诉 Ansible 使用 ping
模块,该模块尝试 ping 目标主机。用户名密码登录
[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
`
all
:这是 Ansible 中的特殊关键字,指您库存中的所有主机。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。