尝试运行Ansible

为了达到某个预期的结果或者完成某个任务的意愿或原因。

    • Virtual Box上に構築したCentOSからさくらVPS上に構築したCentOSへのSSH接続の設定を行う

 

    • Virtual Box上に構築したCentOSのAnsibleを動かす

 

    さくらVPS上に構築したCentOSに対してAnsibleで疎通確認をする

相关网站

官方网站

    Ansible

请参考以下网站

    Ansible Tutorial

下载软件

    なし

环境

主机

    • Windows 7 Home Premium

Vagrant 1.7.2
VirtualBox 4.3.26-98988

访客机器

    • CentOS7

Python 2.7.5
Ansible 1.9.1

目标机器

    • さくらVPS

CentOS7

步骤

    • ターゲットマシンにvagrantユーザを追加する

 

    • ゲストマシンで秘密鍵・公開鍵を作成する

 

    • ターゲットマシンに公開鍵を配置する

 

    • ゲストマシンでAnsibleのhostsファイルを作成する

 

    Ansibleコマンドでゲストマシンからターゲットマシンの疎通を確認する

在目标机上添加vagrant用户。

在连接到目标计算机之前,通过Ansible创建一个名为vagrant的用户。在目标计算机上运行以下命令。

# useradd vagrant
# passwd vagrant
Changing passwd for user vagrant.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
#

在主机上创建私钥和公钥。

为了以SSH公钥身份验证方式从主机连接到目标主机,需要创建私钥和公钥。

在主机上执行以下命令。

$ 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:
:
$

将公钥放置在目标机器上

在先前的步骤中创建的密钥对中,将公钥配置到目标机器上。可以使用ssh-copy-id命令来实现。按照以下方式执行:
在过程中,会要求输入目标机器vagrant用户的密码。
在-i选项后面,输入要配置的公钥路径;然后使用@符号连接目标机器的用户名和IP地址,生成一个字符串。

$ ssh-copy-id -i .ssh/id_rsa.pub vagrant@XXX.XXX.XXX.XXX
The authenticity of host 'XXX.XXX.XXX.XXX (XXX.XXX.XXX.XXX)' can't be established.
ECDSA key fingerprint is ZZ:ZZ:ZZ:ZZ:ZZ:ZZ:ZZ:ZZ:ZZ:ZZ:ZZ:ZZ:ZZ:ZZ:ZZ:ZZ.
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
vagrant@XXX.XXX.XXX.XXX's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'vagrant@XXX.XXX.XXX.XXX'"
and check to make sure that only the key(s) you wanted were added.

$

在客机上创建Ansible的清单文件

Ansible会查看一个包含连接信息的文件,即清单文件。默认情况下,该文件位于/etc/ansible/hosts。
在宿主机上创建/etc/ansible/hosts文件。文件中写入的是目标机器的IP地址。

# mkdir /etc/ansible
# vi /etc/ansible/hosts
XXX.XXX.XXX.XXX

使用Ansible命令来确认从虚拟机到目标机器的连通性。

Ansible通过为命令添加称为模块的功能,可以执行各种操作。在这里,我们使用ping模块来进行通信检查。执行以下命令以确认通信。

$ ansible XXX.XXX.XXX.XXX -m ping
Enter passphrase for key '/home/vagrant/.ssh/id_rsa':
XXX.XXX.XXX.XXX | success >> {
    "changed": false,
    "ping": "pong"
}

以上 can be paraphrased in Chinese as “上述” .