ansible是基于 Python paramiko 开发,分布式,无需客户端,轻量级,配置语法使用 YMAL 及 Jinja2模板语言,更强的远程命令执行操作。
架构图及工作逻辑
Ansible 在管理节点将 Ansible 模块通过 SSH 协议(或者 Kerberos、LDAP)推送到被管理端执
行,执行完之后自动删除,可以使用 SVN 等来管理自定义模块及编排。
- Ansible:核心
- Modules:包括 Ansible 自带的核心模块及自定义模块.
- Plugins:完成模块功能的补充,包括连接插件、邮件插件等.
- Playbooks:网上很多翻译为剧本,个人觉得理解为编排更为合理;定义 Ansible 多任务配置文件,有 Ansible 自动执行.
- Inventory:定义 Ansible 管理主机的清单
常用模块
ansible提供上千个模块,常用的模块就一小部分列出常用模块一些
linux
#拷贝本地的/etc/hosts 文件到 atlanta 主机组所有主机的/tmp/hosts
ansible atlanta -m copy -a "src=/etc/hosts dest=/tmp/hosts"
#file 模块允许更改文件的用户及权限和创建文件
ansible webservers -m file -a "dest=/srv/foo/b.txt mode=600 owner=mdehaan group=mdehaan"
ansible webservers -m file -a "dest=/path/to/c mode=755 owner=mdehaan group=mdehaan state=directory“
#service更改服务状态
ansible webservers -m service -a "name=httpd state=started"
#shell模块远程执行命令
ansible raleigh -m shell -a 'echo $TERM'
#yum模块rpm软件包管理
ansible webservers -m yum -a "name=acme state=present"
#user 模块对于创建新用户和更改、删除已存在用户
ansible all -m user -a "name=foo password=<crypted password here>"
ansible all -m user -a "name=foo state=absent"
window
- win_feature : 安装和卸载功能.
- win_get_url : 从给定的 url 下载文件
- win_group : 添加和删除本地组
- win_msi : 安装和卸载 MSI 文件
- win_ping : windows 版本的 ping 模块
- win_service : 管理 windows 服务
- win_stat : 返回关于 windows 文件的信息
- win_user : 管理本地账号
playbook
Playbooks 是 Ansible 管理配置、部署应用和编排的语言,可以使用 Playbooks 来描述你想在远
程主机执行的策略或者执行的一组步骤过程等
-
Playbooks 组成:
- Target section
定义将要执行 playbook 的远程主机组 - Variable section
定义 playbook 运行时需要使用的变量 - Task section
定义将要在远程主机上执行的任务列表 - Handler section
定义 task 执行完成以后需要调用的任务
- Target section
例子:
- 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
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。