如何在Ubuntu上安装和配置Ansible()

本文概述

  • Ansible安装
  • SSH密钥交换
  • Ansible客户端设置
  • Ansible测试
在Ubuntu上开始使用Ansible进行更好的环境配置和配置管理。
配置管理是DevOps生命周期中的关键阶段。它有助于IT基础架构的自动化和编排。
有几种用于配置管理的工具, 例如Puppet, Ansible, Chef, SaltStack。当然, Ansible是目前DevOps中最受欢迎的工具之一。它可以轻松管理1000台服务器和你完整的IT基础架构。
< span data-mce-type =” bookmark” style =” display:inline-block; width:0px; 溢出:隐藏; line-height:0; ” class =” mce_SELRES_start” > < / span>
我们将在本文中介绍以下内容。
  • Ansible安装
  • SSH密钥交换
  • Ansible客户端设置
  • Ansible测试
如果你是绝对的初学者, 请查看此Ansible介绍文章。
给初学者的Ansible简介
Ansible安装为了简单起见, 让我们尝试在两台服务器上使用Ansible。一个将是Ansible服务器, 另一个将是具有以下IP的Ansible客户端。
  • ansible服务器– 10.0.0.1
  • ansible-client – 10.0.0.25
安装非常简单……需要在要使用Ansible的所有服务器上完成以下操作。在这种情况下, 在两台服务器上。
  • 运行以下命令以安装安装ansible所需的必要软件。
[email  protected]:~# apt install software-properties-common

  • 使用ansible软件包安装存储库。
[email  protected]:~# apt-add-repository --yes --update ppa:ansible/ansible

  • 更新高级打包工具(apt)
[email  protected]:~# apt update

  • 最后, 运行下面的命令进行安装
[email  protected]:~# apt install ansible

安装必要的软件包将花费几秒钟。
你如何确保其安装和版本?
好吧, 这很容易。你可以将– version语法与ansible一起使用, 如下所示。
[email  protected]:~# ansible --versionansible 2.8.1config file = /etc/ansible/ansible.cfgconfigured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']ansible python module location = /usr/lib/python2.7/dist-packages/ansibleexecutable location = /usr/bin/ansiblepython version = 2.7.15+ (default, Nov 27 2018, 23:36:35) [GCC 7.3.0][email  protected]:~#

如你所见, Ansible 2.8.1已安装, 它提供了必要的信息, 例如配置文件位置, python模块。
接下来, 我们需要进行SSH密钥交换, 以便服务和客户端可以相互交谈。
SSH密钥交换Ansible通过SSH(安全外壳)连接到其客户端。
我们将首先在ansible服务器上生成一个公共密钥, 需要将其复制到ansible-client。
确保你以root用户身份登录。
  • 使用ssh-keygen命令生成密钥, 如下所示
[email  protected]:~# ssh-keygenGenerating 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:SHA256:cDapZBESo+8XcbXupbtILkFrklUSpwa70Y1c7yH5K1A [email  protected]The key's randomart image is:+---[RSA 2048]----+|=.+oo .||. B.B.= .||. o @oE +||. *oO * .||o++.S + .||.o +o . +||.o..o +||..o o .||.o o.|+----[SHA256]-----+[email  protected]:~#

如你所见, 它已经在.ssh文件夹中生成了一个公共密钥。完整路径为/root/.ssh/id_rsa.pub
注意:确保私钥和公钥文件不可读。你可以列出文件进行验证。
  • 转到.ssh文件夹
cd /root/.ssh

  • 列出文件
[email  protected]:~# ls -l -rw------- 1 root root 1679 Jun 19 00:37 id_rsa -rw------- 1 root root 404 Jun 19 00:37 id_rsa.pub

如果你发现许可权错误, 则可以使用chmod命令对其进行更改
例如:
chmod 400 id_rsachmod 400 id_rsa.pub

让我们将公钥复制到IP地址为192.168.56.101的Ansible主机
[email  protected]:~/.ssh# ssh-copy-id -i ~/.ssh/id_rsa.pub [email  protected]/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"The authenticity of host '10.0.0.25 (10.0.0.25)' can't be established.ECDSA key fingerprint is SHA256:eXduPrfV0mhxUcpsZWg+0oXDim7bHb90caA/Rt79cIs.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[email  protected]'s password: Number of key(s) added: 1Now try logging into the machine, with:"ssh '[email  protected]'"and check to make sure that only the key(s) you wanted were added.[email  protected]:~/.ssh#

你可以在上面的输出中看到, 成功添加了1个密钥。这表示已交换SSH密钥。
接下来, 我们将设置Ansible客户端。
Ansible客户端设置我假设你已经按照前面步骤中的说明在客户端服务器上执行了Ansible安装步骤。
客户端或主机设置不过是使Ansible服务器了解客户端而已。并且, 这样做:
  • 登录到Ansible服务器
  • 转到/ etc / ansible
  • 使用你喜欢的编辑器在hosts文件中添加以下内容
[Client] node1 ansible_ssh_host=10.0.0.25

  • 保存主机文件
Ansible测试如果正确地执行了所有步骤, 则在ansible服务器上运行以下命令时, 将获得成功消息。
[email  protected]:~/.ssh# ansible -m ping Clientnode1 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"}, "changed": false, "ping": "pong"}[email  protected]:~/.ssh#

上面的Thea对客户端执行ping操作以测试连接性并确认是否良好。
总结
【如何在Ubuntu上安装和配置Ansible()】我希望这能给你一个想法, 让它开始进行安装和试玩。请继续关注更多Ansible教程, 或查看此Udemy Mastering Ansible课程。

    推荐阅读