官方: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"