ansible简介
Ansible 是一个基于 Python 的自动化运维的工具,无需安装服务端和客户端,只需要有ssh即可,而且使用简单。同类的产品Puppet、Chef、SaltStack都是基于服务端+客户端的模式。 官方称之为 “Ansible is Simple IT Automation”。
安装
sudo apt-get install software-properties-common
sudo apt-add-repository ppa:ansible/ansible
sudo apt-get update
sudo apt-get install ansible
命令应用基础
ansible [-f forks] [-m module_name] [-a args]
-f forks:启动的并发线程数
-m module_name: 要使用的模块
-a args: 模块特有的参数
ansible-doc -l 查看所有模块
ansible-doc -s MODULE_NAME 查看指定模块的详细帮助
- 使用lineinfile模块向/tmp/test写入一行helloworld
ansible 127.0.0.1 -m lineinfile -a "dest=/tmp/test line='helloworld'"
- 使用git模块拉取资源到/tmp/ansible目录
ansible 127.0.0.1 -m git -a "repo='https://github.com/ansible/ansible.git' dest='/tmp/ansible'
playbook
playbook是ansible中批量执行命令的文件,基于yaml格式,类似于chef中的cookbook。
标准的playbook如下:
- hosts: webservers
vars:
http_port: 80
max_clients: 200
remote_user: root
tasks:
- name: ensure apache is at the latest version
yum: pkg=httpd state=latest
- name: write the apache config file
template: src=/srv/httpd.j2 dest=/etc/httpd.conf
notify:
- restart apache
- name: ensure apache is running
service: name=httpd state=started
handlers:
- name: restart apache
service: name=httpd state=restarted
- 检查一个playbook
ansible-playbook deploy.yml --check
- 执行一个playbook
ansible-playbook playbook.yml -f 10