官方:https://www.ansible.com/node
Ansible is a radically simple IT automation engine that automates cloud provisioning, configuration management, application deployment, intra-service orchestration, and many other IT needs.python
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.shell
ansible的好處是快速、簡單、易用、不須要安裝agent,能夠方便的用於配置管理和應用部署等自動化場景;app
yum install -y ansiblepython2.7
# ansible --versionssh
ansible 2.7.5測試
config file = /etc/ansible/ansible.cfgfetch
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']spa
ansible python module location = /usr/lib/python2.7/site-packages/ansibleorm
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)]
# ls /etc/ansible/
ansible.cfg hosts roles
首先在/etc/ansible/hosts中根據部署結構將server劃分到多個group,而後能夠針對某個group執行某些操做;
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
格式
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/'
好比查看某個group的全部server的磁盤佔用狀況:
# ansible all-servers -m shell -a "df -h"
另外也能夠直接指定ip來操做,好比:
# ansible 192.168.0.55 -m ping
其實若是隻須要遠程執行命令,ssh也能夠
ssh 192.168.0.55 "date"