常用自动化运维工具
Ansible:python,Agentless, 中小型应用环境 基于 Python 开发出来工具
Saltstack:python, 一般需部署 agent, 执行效率更高
Puppet:ruby, 功能强大, 配置复杂, 重型, 适合大型环境
Fabric:python,agentless
Chef: ruby, 国内应用少
Cfengine
func
Ansible 发展史
Ansible
创始人, Michael DeHaan(Cobbler 与 Func 的作者)
2012-03-09, 发布 0.0.1 版, 红帽收购
2015-10-17,Red Hat 宣布收购
同类自动化工具 GitHub 关注程度 (2016-07-10)
ansible 特性
模块化: 调用特定的模块, 完成特定任务
有 Paramiko,PyYAML,Jinja2(模板语言) 三个关键模块
支持自定义模块
基于 Python 语言实现
部署简单, 基于 python 和 SSH(默认已安装),agentless
安全, 基于 OpenSSH
支持 playbook 编排任务
幂等性: 一个任务执行 1 遍和执行 n 遍效果一样, 不因重复执行带来意外情况
无需代理不依赖 PKI(无需 ssl)
可使用任何编程语言写模块
YAML 格式, 编排任务, 支持丰富的数据结构
较强大的多层解决方案
Ansible 主要组成部分
ANSIBLE PLAYBOOKS: 任务剧本 (任务集), 编排定义 Ansible 任务集的配置文件, 由 Ansible 顺序依次执行, 通常是 JSON 格式的 YML 文件
INVENTORY:Ansible 管理主机的清单 / etc/anaible/hosts
MODULES:Ansible 执行命令的功能模块, 多数为内置核心模块, 也可自定义
PLUGINS: 模块功能的补充, 如连接类型插件, 循环插件, 变量插件, 过滤插件等, 该功能不常用
API: 供第三方程序调用的应用程序编程接口
ANSIBLE: 组合 INVENTORY,API,MODULES,PLUGINS 的绿框, 可以理解为是 ansible 命令工具, 其为核心执行工具
- ntp.magedu.com
- [webservers]
- www1.magedu.com:2222
- www2.magedu.com
- [dbservers]
- db1.magedu.com
- db2.magedu.com
- db3.magedu.com
- yum -y install python-jinja2 PyYAML python-paramiko python-babel python-crypto
- tar xf ansible-1.5.4.tar.gz
- cd ansible-1.5.4
- python setup.py install
- mkdir /etc/ansible
- cp -r examples/*/etc/ansible
- git clone git://github.com/ansible/ansible.git --recursive
- cd ./ansible
- source ./hacking/env-setup
- yum install python-pip python-devel
- yum install gcc glibc-devel zibl-devel rpm-bulid openssl-devel
- pip install --upgrade pip
- pip install ansible --upgrade
- [websrvs]
- www[01:100].example.com
- [dbsrvs]
- db-[a:f].example.com
- [defaults]
- #inventory = /etc/ansible/hosts # 主机列表配置文件
- #library = /usr/share/my_modules/ # 库文件存放目录
- #remote_tmp = $HOME/.ansible/tmp #临时 py 命令文件存放在远程主机目录
- #local_tmp = $HOME/.ansible/tmp # 本机的临时命令执行目录
- #forks = 5 # 默认并发数
- #sudo_user = root # 默认 sudo 用户
- #ask_sudo_pass = True #每次执行 ansible 命令是否询问 ssh 密码
- #ask_pass = True
- #remote_port = 22
- #host_key_checking = False # 检查对应服务器的 host_key, 建议取消注释
- #log_path=/var/log/ansible.log #日志文件
- vim /etc/ansible/hosts
- 172.20.102.70
- 172.20.102.71
- 172.20.113.166
- 172.20.113.167
- vim /etc/ansible/hosts
- [Webservers]
- 172.20.102.70
- 172.20.102.71
- [mariaservers]
- 172.20.113.166
- 172.20.113.167
- [ddservers]
- SSH password:
- 172.20.113.167 | SUCCESS => {
- "changed": false,
- "ping": "pong"
- }
- 172.20.102.70 | SUCCESS => {
- "changed": false,
- "ping": "pong"
- }
- 172.20.102.71 | SUCCESS => {
- "changed": false,
- "ping": "pong"
- }
- vim /etc/ansible/ansible.cfg
- host_key_checking = False
- vim /etc/ansible/ansible.conf
- log_path = /var/log/ansible.log
- ansible "" -m ping
- ansible 192.168.1. -m ping
- ansible "srvs" -m ping
- ansible "websrvs:appsrvs" -m ping
- ansible "192.168.1.10:192.168.1.20" -m ping
- ansible "websrvs:&dbsrvs" -m ping
- ansible "~(web|db)..magedu.com" -m ping
- vim /data/haha.sh
- echo "Hello word"
- chmod +x /data/haha.sh
- ansible all -m script -a '/data/haha.sh'
- vim /etc/sysconfig/selinux
- SELINUX=disabled
- cp /etc/sysconfig/selinux /data
- 172.20.102.70 | SUCCESS | rc=0>>
- Disabled
- 172.20.113.166 | SUCCESS | rc=0>>
- Disabled
- 172.20.113.167 | SUCCESS | rc=0>>
- Disabled
- 172.20.102.71 | SUCCESS | rc=0>>
- Disabled
- ansible srv -m file -a "path=/root/a.sh owner=wang mode=755"
- ansible web -m file -a 'src=/app/testfile dest=/app/testfile-link state=link
- centos7:/etc/hostname
- centos6:/etc/sysconfig/network
- ansible srv -m service -a 'name=httpd state=stopped'
- ansible srv -m service -a 'name=httpd state=started'
- ansible srv -m service -a 'name=httpd state=reloaded'
- ansible srv -m service -a 'name=httpd state=restarted'
- ansible srv -m user -a 'name=user1 comment="test user"uid=2048 home=/app/user1 group=root'
- ansible srv -m user -a 'name=sysuser1 system=yes home=/app/sysuser1'
- ansible srv -m group -a "name=testgroup system=yes"
- ansible srv -m group -a "name=testgroup state=absent
- cd .ansible/roles
- cp -rp geerlingguy.nginx/chen.nginx
- ansible-galaxy list
- ansible-pull
- Ansible-playbook
- ansible-playbook hello.yml
- cat hello.yml
- #hello world yml file
- - hosts: websrvs
- remote_user: root
- tasks:
- vim ansible/play.yml
- - hosts: mariadbservers
- remote_user: root
- #运行的用户
- tasks:
- - name: play
- command: hostname
- ansible-playbook play.yml
- Ansible-vault
- root@all (2)[f:5]$ list
- root@all (2)[f:5]$ cd appsrvs
- root@appsrvs (2)[f:5]$ list
- root@appsrvs (2)[f:5]$ yum name=httpd state=present
- root@appsrvs (2)[f:5]$ service name=httpd state=started
来源: http://www.bubuko.com/infodetail-2641033.html