【原創】運維基礎之Ansible(1)簡介、安裝和使用
官方:https://www.ansible.com/
一 簡介
Ansible is a radically simple IT automation engine that automates cloud provisioning, configuration management, application deployment, intra-service orchestration, and many other IT needs.
It uses no agents and no additional custom security infrastructure, so it‘s easy to deploy - and most importantly, it uses a very simple language (YAML, in the form of Ansible Playbooks) that allow you to describe your automation jobs in a way that approaches plain English.
ansible的好處是快速、簡單、易用、不需要安裝agent,可以方便的用於配置管理和應用部署等自動化場景;
二 安裝
1 安裝
yum install -y ansible
2 測試
# ansible --version
ansible 2.7.5
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 = /usr/bin/ansible
python version = 2.7.5 (default, Jul 13 2018, 13:06:57) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]
3 配置目錄結構
# ls /etc/ansible/
ansible.cfg hosts roles
三 使用
首先在/etc/ansible/hosts中根據部署結構將server劃分到多個group,然後可以針對某個group執行某些操作;
1 添加server分組配置
vi /etc/ansible/hosts
[all-servers]
192.168.0.54
192.168.0.55
192.168.0.56
以上配置了一個server_group名為all-servers,其中包含3臺server;
可以通過--list查看某個server_group下的所有server:
# ansible all-servers --list
hosts (3):
192.168.0.54
192.168.0.55
192.168.0.56
2 執行操作
格式
ansible $server_group -m $module_name -a $module_args
例如ping:
# ansible all-servers -m ping
192.168.0.54 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.0.55 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.0.56 | SUCCESS => {
"changed": false,
"ping": "pong"
}
例如command:
# ansible all-servers -m command -a "date"
192.168.0.54 | CHANGED | rc=0 >>
Sun Jan 13 17:14:06 CST 2019192.168.0.56 | CHANGED | rc=0 >>
Sun Jan 13 17:14:06 CST 2019192.168.0.55 | CHANGED | rc=0 >>
Sun Jan 13 17:14:06 CST 2019
具體的module_name有哪些?可以通過命令查看
# ansible-doc -l
某個module_name的幫助也可以通過命令查看
# ansible-doc $module_name
比如:
# ansible-doc shell
> SHELL (/usr/lib/python2.7/site-packages/ansible/modules/commands/shell.py)
The `shell‘ module takes the command name followed by a list of space-delimited arguments. It is almost exactly like the [command] module but runs the command
through a shell (`/bin/sh‘) on the remote node. For Windows targets, use the [win_shell] module instead.
# ansible-doc copy
> COPY (/usr/lib/python2.7/site-packages/ansible/modules/files/copy.py)
The `copy‘ module copies a file from the local or remote machine to a location on the remote machine. Use the [fetch] module to copy files from remote locations to
the local box. If you need variable interpolation in copied files, use the [template] module. For Windows targets, use the [win_copy] module instead.
常用module:ping,command,shell,copy,file,script,cron等...
比如向某個group的所有server拷貝文件命令:
# ansible all-servers -m copy -a ‘src=/tmp/1.log dest=/tmp/‘
另外也可以直接指定ip來操作,比如:
# ansible 192.168.0.55 -m ping
【原創】運維基礎之Ansible(1)簡介、安裝和使用