实验环境:
主控机: 192.168.1.9(centos 7.5)
被控机: 192.168.1.8(centos 7.5);192.168.1.7(centos 7.4);192.168.1.6(cnetos 6.9)
虚拟机: vmware 14
做 ansible 实验前, 必须做好主控机的基于被控机的 key 验证登录
具体参照前面的博文中的基于 key 验证.
http://blog.51cto.com/13698281/2118972
安装 ansible
rpm 包安装: EPEL 源
yum install ansible
编译安装:
- 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 build
- python setup.py install
- mkdir /etc/ansible
- cp -r examples/*/etc/ansible
Git 方式:
- git clone git://github.com/ansible/ansible.git --recursive
- cd ./ansible
- source ./hacking/env-setup
pip 安装: pip 是安装 Python 包的管理器, 类似 yum
- 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
确认安装: ansible --version
相关文件说明
配置文件
/etc/ansible/ansible.cfg 主配置文件, 配置 ansible 工作特性; 建议去掉# host_key_checking = False ;disabled SSH key host checking; 前提基于 key 验证
/etc/ansible/hosts 主机清单
/etc/ansible/roles/ 存放角色的目录
程序
/usr/bin/ansible 主程序, 临时命令执行工具
/usr/bin/ansible-doc 查看配置文档, 模块功能查看工具
/usr/bin/ansible-galaxy 下载 / 上传优秀代码或 Roles 模块的官网平台
/usr/bin/ansible-playbook 定制自动化任务, 编排剧本工具 / usr/bin/ansible-pull 远程执行命令的工具
/usr/bin/ansible-vault 文件加密工具
/usr/bin/ansible-console 基于 Console 界面与用户交互的执行工具
hosts 清单
- # Ex 1: Ungrouped hosts, specify before any group headers.
- ## green.example.com
- ## blue.example.com
- ## 192.168.100.1
- ## 192.168.100.10
- # Ex 2: A collection of hosts belonging to the 'webservers' group
- ## [webservers]
- ## alpha.example.org
- ## beta.example.org
- ## 192.168.1.100:9527 #如果此被控机 ssh 端口为 9527, 则加一项
- ## 192.168.1.110
- # Here's another example of host ranges, this time there are no
- # leading 0s:
- ## db-[99:101]-node.example.com
- ansible
特性
模块化: 调用特定的模块, 完成特定任务有 Paramiko,PyYAML,Jinja2(模板语言)三个关键模块
支持自定义模块
基于 Python 语言实现
部署简单, 基于 python 和 SSH(默认已安装),agentless
安全, 基于 OpenSSH
支持 playbook 编排任务
幂等性: 一个任务执行 1 遍和执行 n 遍效果一样, 不因重复执行带来意外情况
无需代理不依赖 PKI(无需 ssl)
可使用任何编程语言写模块
YAML 格式, 编排任务, 支持丰富的数据结构
较强大的多层解决方案
主要组成
ANSIBLE PLAYBOOKS: 任务剧本(任务集), 编排定义 Ansible 任务集的配置文件, 由 Ansible 顺序依次执行, 通常是 JSON 格式的 YML 文件
INVENTORY:Ansible 管理主机的清单 / etc/anaible/hosts
MODULES:Ansible 执行命令的功能模块, 多数为内置核心模块, 也可自定义
PLUGINS: 模块功能的补充, 如连接类型插件, 循环插件, 变量插件, 过滤插件等, 该功能不常用
API: 供第三方程序调用的应用程序编程接口
ANSIBLE: 组合 INVENTORY,API,MODULES,PLUGINS 的绿框, 可以理解为是 ansible 命令工具, 其为核心执行工具
- ansible srvs -m command -a 'service vsftpd start'
- ansible srvs -m command -a 'echo password |passwd --stdin user'
- #不成功, 此命令不支持 $VARNAME <> | ; & 等, 用 shell 模块实现
- ansible srv -m shell -a 'echo password |passwd -stdin user'
- # 调用 bash 执行命令 类似 cat /tmp/stanley.md | awk -F'|' '{print $1,$2}' &>/tmp/example.txt 这些复杂命令, 即使使用 shell 也可能会失败, 解决办法: 写到脚本时, copy 到远程, 执行, 再把需要的结果拉回执行命令的机器
- ansible srv -m copy -a "src=/root/f1.sh dest=/tmp/f2.sh owner=user mode=600 backup=yes"
- # 如目标存在, 默认覆盖, 此处指定先备份
- ansible srv -m copy -a "content='test content\n'dest=/tmp/f1.txt" # 利用内容, 直接生成目标文件
- ansible srv -m file -a "path=/root/a.sh owner=user mode=755"
- ansible web -m file -a 'src=/app/testfile dest=/app/testfile-link state=link'
- # 支持时间: minute,hour,day,month,weekday
- ansible srv -m cron -a "minute=*/5 job='/usr/sbin/ntpdate 172.16.0.1 &>/dev/null'name=Synctime" #创建任务
- ansible srv -m cron -a 'state=absent name=Synctime' #删除任务
- ansible srv -m yum -a 'name=httpd state=latest' #安装
- ansible srv -m yum -a 'name=httpd state=absent' #删除
- Ansible-vault
- #!/bin/bash
- # 安装 Apache
- yum install --quiet -y httpd
- # 复制配置文件
- cp /tmp/httpd.conf /etc/httpd/conf/httpd.conf
- cp/tmp/vhosts.conf /etc/httpd/conf.d/
- # 启动 Apache, 并设置开机启动
- service httpd start
- chkconfig httpd on
- ---
- - hosts: all
- tasks:
- - name: "安装 Apache"
- yum: name=httpd
- - name: "复制配置文件"
- copy: src=/tmp/httpd.conf dest=/etc/httpd/conf/
- copy: src=/tmp/vhosts.conf dest=/etc/httpd/conf.cd/
- - name: "启动 Apache, 并设置开机启动"
- service: name=httpd state=started enabled=yes
- vars:
- var1: value1
- var2: value2
- tasks:
- - name: install conf file to centos7
- template: src=nginx.conf.c7.j2
- when: ansible_distribution_major_version == "7"
- ---
- - hosts: all
- remote_user: root
- vars:
- ports:
- - listen_port: 81
- name: web1
- rootdir: web1.com
- - listen_port: 82
- name: web2
- rootdir: web2.com
- - listen_port: 83
- name: web3
- rootdir: web3.com
- tasks:
- - name: copy templates conf
- template: src=forif.conf.j2 dest=/data/forif.conf
- templates/for.conf.j2
- {% for p in ports %}
- server{
- listen {{ p.listen_port }}
- name {{ p.name }}
- rootdir {{ p.rootdir }}
- }
- {% endfor %}
- ---
- - hosts: websrvs
- remote_user: root
- tasks:
- - name: install httpd package
- yum: name=httpd
- - name: copy configuration file for httpd
- copy: src=files/httpd.conf dest=/etc/httpd/conf/httpd.conf
- notify: restart service
- - name: start httpd service
- service: name=httpd state=started
- handlers:
- service: name=httpd state=restarted
- roles
- roles
来源: http://www.bubuko.com/infodetail-2625763.html