在VirtualBox的实例之间运行Ansible!
以下是在VirtualBox上运行Ansible的两个实例之间进行的操作记录。
总结
Vagrant を使用して2つのインスタンスを内部ネットワークに接続し
ssh でインスタンス間で接続できるようにし
ansible all -m ping を通してみました。
环境
-
- Vagrant 2.2.9
-
- VirtualBox 6.0.22r137980
- Ansible 2.9.9
使用流浪找遍在云端创建实例。
使用Vagrant定义两个实例。确保每个实例都连接到私有网络,并且可以彼此通信。
Vagrant.configure("2") do |config|
config.vm.box = "centos/7"
config.vm.define "ansible-controll-node" do |controll|
controll.vm.network "private_network", ip: "192.168.50.1", virtualbox__intnet: true
controll.vm.provider "virtualbox" do |v|
v.memory = 1024
end
end
config.vm.define "ansible-host-node" do |host|
host.vm.network "private_network", ip: "192.168.50.2", virtualbox__intnet: true
end
end
使用Vagrant up命令启动实例。
SSH的设置
只是启动实例并不能让实例之间能够通过SSH通信,所以我们需要进行设置。由于我们将使用Ansible从ansible-control-node到ansible-host-node,所以需要确保它们可以在同一方向上通过SSH进行通信。
Ansible 主机节点的配置
进入 ansible-host-node,并确保可以通过密码进行SSH连接。
$ vagrant ssh ansible-host-node
[vagrant@localhost ~]$ sudo vi /etc/ssh/sshd_config
将sshd_config文件中的PasswordAuthentication更改为以下方式。
# To disable tunneled clear text passwords, change to no here!
PasswordAuthentication yes
#PermitEmptyPasswords no
# PasswordAuthentication no
我将重新启动sshd。
[vagrant@localhost ~]$ sudo systemctl restart sshd
ansible控制节点的配置
进入ansible-controll-node并创建SSH密钥。
$ vagrant ssh ansible-controll-node 4.5m 月 6/15 11:15:12 2020
[vagrant@localhost ~]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/vagrant/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/vagrant/.ssh/id_rsa.
Your public key has been saved in /home/vagrant/.ssh/id_rsa.pub.
The key fingerprint is:
(以下略)
使用上述命令,在/home/vagrant/.ssh/目录下成功创建了公钥和私钥。
接下来,将公钥传递给 ansible-host-node。使用 ssh-copy-id 命令,您可以将公钥正确地配置到连接目标实例的SSH连接中。当进行SSH连接时会要求输入密码,而vagrant用户的默认密码是vagrant。
[vagrant@localhost ~]$ ssh-copy-id vagrant@192.168.50.2
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/vagrant/.ssh/id_rsa.pub"
/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
vagrant@192.168.50.2's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'vagrant@192.168.50.2'"
and check to make sure that only the key(s) you wanted were added.
我会验证是否可以无需密码进行连接。
[vagrant@localhost ~]$ ssh vagrant@192.168.50.2
Last login: Mon Jun 15 11:19:17 2020 from 10.0.2.2
安装和运行Ansible
使用ansible控制节点来执行以下操作。
为了安装Ansible,需要启用EPEL软件源。
[vagrant@localhost ~]$ sudo yum -y install epel-release
启用EPEL仓库后,将可以安装ansible。
[vagrant@localhost ~]$ sudo yum -y install ansible
将清单文件进行修改。
[vagrant@localhost ~]$ sudo vi /etc/ansible/hosts
# この行を追加
192.168.50.2
使用Ansible执行ping命令。
[vagrant@localhost ~]$ ansible all -m ping
192.168.50.2 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
可以了。