尝试使用一点点Ansible来体验一下它的感觉
Ansible是由Red Hat开发的开源配置管理工具。
我最近在调查Terraform的remote-exec等功能,但发现它只能在创建虚拟机时执行,而在架设后的运维中,Ansible仍然是一个选项。Ansible是一个多功能工具,但本文为了把握氛围,将简要总结执行的内容。

1) 制作钥匙
我会创建一把用于在虚拟机上安装的密钥。如果您要使用现有的密钥,可以省略。
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/ubuntu/.ssh/id_rsa): /ubuntu/ansible-key/id_rsa
Enter passphrase (empty for no passphrase): ※パスフレーズを入力※
Enter same passphrase again: ※パスフレーズを入力※
※出力は省略※
$ ls -l ~/ansible-key
total 8
-rw------- 1 root root 1766 Oct 23 06:35 id_rsa ※秘密鍵※
-rw-r--r-- 1 root root 403 Oct 23 06:35 id_rsa.pub ※公開鍵※
# cat ~/ansible-key/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2Exxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ubuntu@localhost ※以降の手順でこの鍵を設定※
2) 设置公钥 shè zhì)
在受管理设备上创建一个ansible-user,并设置1)的公钥。
# adduser ansible-user
Adding user `ansible-user' ...
Adding new group `ansible-user' (1002) ...
Adding new user `ansible-user' (1002) with group `ansible-user' ...
Creating home directory `/home/ansible-user' ...
Copying files from `/etc/skel' ...
Enter new UNIX password: ※パスワードを入力※
Retype new UNIX password: ※パスワードをもう一度入力※
passwd: password updated successfully
Changing the user information for ansible-user
Enter the new value, or press ENTER for the default
Full Name []: ※ENTERで進む※
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n] Y ※Yを入力※
# su - ansible-user
$ mkdir .ssh
$ chmod 700 .ssh
$ vi id_rsa.pub ※1)で作成したid_rsa.pubと同じ内容にする※
$ cat id_rsa.pub >> authorized_keys
3) 设置Ansible
在控制端(服务器端)进行设置。
安装Ansible:
# apt install -y ansible
※出力は省略※
# ansible --version
ansible 2.5.1
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/dist-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.15+ (default, Jul 9 2019, 16:51:35) [GCC 7.4.0]
在/etc/ansible/hosts文件中设置:配置管理目标终端的IP地址。
# cat /etc/ansible/hosts
# This is the default ansible 'hosts' file.
#
# It should live in /etc/ansible/hosts
#
# - Comments begin with the '#' character
# - Blank lines are ignored
# - Groups of hosts are delimited by [header] elements
# - You can enter hostnames or ip addresses
# - A hostname/ip can be a member of multiple groups
# Ex 1: Ungrouped hosts, specify before any group headers.
[pracice]
10.0.3.100 ※管理対象端末のIPアドレスを設定※
#green.example.com
安装具有秘密钥的1),然后使用ansible ping命令进行连接验证。
# ls -l /home/azureuser/.ssh/ansible.id_rsa
-rw------- 1 root root 1767 Oct 21 02:57 ansible.id_rsa ※1)の秘密鍵、パーミッションを600に設定※
# ansible 10.0.3.100 -m ping -u ansible-user --private-key=/home/azureuser/.ssh/ansible.id_rsa
Enter passphrase for key '/home/azureuser/.ssh/ansible.id_rsa': ※パスフレーズを入力※
10.0.3.100 | SUCCESS => {
"changed": false,
"ping": "pong"
}
准备一个记录了管理对象设备配置(执行的内容)的playbook。在这里,只需创建一个用于测试的目录。
# cat playbook.yml
- hosts: all
sudo: false
tasks:
- name: mkdir test
file: dest=/home/ansible-user/test_directory/ state=directory
执行ansible-playbook命令。在playbook中所述的内容将在受管理的终端执行。
# ansible-playbook playbook.yml -u ansible-user --private-key=/home/azureuser/.ssh/ansible.id_rsa
feature will be removed in version 2.6. Deprecation warnings can be disabled by setting deprecation_warnings=False in
ansible.cfg.
PLAY [all] *********************************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************************
Enter passphrase for key '/home/azureuser/.ssh/ansible.id_rsa': ※パスフレーズを入力※
ok: [10.0.3.100]
TASK [mkdir test] **************************************************************************************************************
changed: [10.0.3.100]
PLAY RECAP *********************************************************************************************************************
10.0.3.100 : ok=2 changed=1 unreachable=0 failed=0
确认没有出现错误,并在受管理的对象上确认结果。
$ ls -l /home/ansible-user/test_directory
drwxrwxr-x 2 ansible-user ansible-user 4096 Oct 21 12:19 test_directory/
非常非常简单,但步骤就是以上。