Ansible入门① 从安装到执行ansible-playbook命令实例
Ansible的优点十分突出。
-
- 構成を管理する対象のサーバー側にソフトウェアのインストールが不要でエージェントレス
-
- 設定ファイルがYAMLでの表記で、可読性が非常にメンテナンスコストも低い
- YAMLは学習コストが非常に低い
YAML -> YAML格式
关键字
以下是为了大致了解而列举出最基本的元素,虽不是严格的解释。
节点
这涉及到服务器、客户端等等。
库存
可以使用INI格式或YAML格式来管理目标节点(即要管理配置的服务器)的连接信息,可以用于管理一个或多个节点,并且还可以对其进行分组。
模块
Ansible发送到远程计算机的工作单位是任务。
它提供了许多标准模块,基本上是通过组合这些模块来进行配置管理。
还有许多与Linux命令类似的模块。
您也可以自行创建模块。
任务 wù)
这是指汇总了执行模块以及模块执行时所需的参数等信息的处理定义或处理本身。
玩法手册
这是一个任务的总结。
验证环境(Ansible的安装服务器)
CentOS Linux release 7.8.2003 (Core)
ansible.cfg配置文件
如果使用yum安装Ansible,则会自动创建/etc/ansible/ansible.cfg配置文件。因此,如果想要快速验证其功能,建议使用yum进行安装。
安装
以下是一种最简单的方法,但还有其他安装方法,如使用pip或从源码安装的方法。
安装Ansible
epel リポジトリを追加
# yum install epel-release
インストール
# yum install ansible
确认版本
[root@localhost ansible]# ansible --version
ansible 2.9.10
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/site-packages/ansible
executable location = /bin/ansible
python version = 2.7.5 (default, Apr 2 2020, 13:16:51) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
设置用于键认证的设定
在使用Ansible管理受控服务器之前,需要在连接源服务器上(即安装Ansible的服务器)提前进行密钥认证的设置。
在连接源服务器上创建密钥。
将创建的服务器公钥设置到受控服务器上。
实施SSH公钥认证。
Ansible命令
使用指定的库存文件以及要使用的模块来操作 ansible。
[root@localhost ansible]# cat /etc/ansible/test_inventory
[test_servers]
192.168.11.21
[root@localhost ansible]# ansible -i test_inventory test_servers -m ping
192.168.11.21 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
ansible-playbook命令
ansible-playbook -i inventory_file playbook_file
#/opt/tmpディレクトリの作成と/etc/ansible/test_inventoryファイルのコピーを実施する
[root@localhost ansible]# cat test_playbook.yml
- hosts: test_servers
tasks:
- name: create direvtory
file:
path: /opt/tmp
state: directory
owner: ansible
mode: 0755
- name: copy file
copy:
src: /etc/ansible/test_inventory
dest: /home/ansible/tmp/test_inventory
owner: ansible
mode: 0644
#Taskが成功し、ディレクトリの作成とファイルのコピーが成功しているので、ok=3 changed=2となっている
[root@localhost ansible]# ansible-playbook -i ./test_inventory ./test_playbook.yml
PLAY [test_servers] ***************************************************************************************************************************
TASK [Gathering Facts] ************************************************************************************************************************
ok: [192.168.11.21]
TASK [create direvtory] ***********************************************************************************************************************
changed: [192.168.11.21]
TASK [copy file] ******************************************************************************************************************************
changed: [192.168.11.21]
PLAY RECAP ************************************************************************************************************************************
192.168.11.21 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
#test_playbook.ymlを連続して実行した場合は、状態に変化ないのでchanged=0となっている
[root@localhost ansible]# ansible-playbook -i ./test_inventory ./test_playbook.yml
PLAY [test_servers] ***************************************************************************************************************************
TASK [Gathering Facts] ************************************************************************************************************************
ok: [192.168.11.21]
TASK [create direvtory] ***********************************************************************************************************************
ok: [192.168.11.21]
TASK [copy file] ******************************************************************************************************************************
ok: [192.168.11.21]
PLAY RECAP ************************************************************************************************************************************
192.168.11.21 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0