使用Ansible进行测试
首先
最近我开始了使用Ansible,所以我简单地总结了从部署到测试执行的过程。
执行环境
・CentOS7.5
・Python 2.7.5(默认)
在中国引入Ansible
计划的目的
· 引入Ansible的服务器(控制器)
· 使用Ansible进行更改的服务器(管理节点)
引入任务
所有的工作都在“控制器”服务器端执行。
1. 安装Ansible
在作为Ansible控制器的服务器上安装Ansible。
使用yum命令安装ansible。
尽管可能会根据依赖关系安装其他组件,但请继续执行安装命令。
创建一个用于作业的目录。
创建用于存储ansible文件的目录。
这次,我们将在主目录下创建。
在主目录下使用mkdir ~/ansible。
准备SSH密钥。
为了在控制器和管理节点之间进行SSH通信,需要创建SSH公钥。
ssh-keygen
系统会要求输入passphrase,但可以留空并按回车继续。
[root@localhost ansible]# 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:
将SSH公钥发送到管理节点。
将生成的SSH公钥发送到管理节点。
ssh-copy-id [username@management_node_IP]
[root@localhost ansible]# ssh-copy-id root@192.168.242.134
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.242.134 (192.168.242.134)' can't be established.
~省略~
Are you sure you want to continue connecting (yes/no)? yes
这样,安装工作就完成了。
创建Playbook
Playbook是一个用于描述在Ansible中执行内容的文件。
1.编辑可执行文件
在工作目录下创建一个作为执行文件的Playbook。
本次将创建一个用于在yum上安装httpd的Playbook,使用vi编写以下内容的文件。
- hosts: all
tasks:
- name: install packages from yum
yum: name=httpd
2.执行Playbook
因为所有的准备工作已经完成,所以现在我将开始执行Playbook。
ansible-playbook -i 管理节点IP,[要执行的Playbook]。
[root@localhost ansible]# ansible-playbook -i 192.168.242.134, test.yml
PLAY [all] *********************************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.242.134]
TASK [install packages from yum] ***********************************************
changed: [192.168.242.134] ←httpdのインストールを行ったログ
PLAY RECAP *********************************************************************
192.168.242.134 : ok=2 changed=1 unreachable=0 failed=0
[root@localhost ansible]#
确认了管理节点上安装了HTTPD。
[root@test01 ~]# yum info httpd
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
* base: ftp.iij.ad.jp
* extras: ftp.iij.ad.jp
* updates: ftp.iij.ad.jp
インストール済みパッケージ
名前 : httpd
アーキテクチャー : x86_64
バージョン : 2.4.6
リリース : 80.el7.centos.1
容量 : 9.4 M
リポジトリー : installed
提供元リポジトリー : updates
要約 : Apache HTTP Server
URL : http://httpd.apache.org/
ライセンス : ASL 2.0
説明 : The Apache HTTP Server is a powerful, efficient, and
: extensible web server.
[root@test01 ~]#
以上 translates to “the above” in English.
附加内容:常用模块列表
我简单整理了常用模块,使用方法请参考官方说明。