一、简介
ansible是新出现的白动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric ) 的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。无客户端。
我粗俗的理解就是将脚本批量下发执行的过程
1. 命令执行过程
- 加载自己的配置文件,默认路径为:/etc/ansible/ansible.cfg
- 查找对应的主机配置文件,找到要执行的主机或主机组
- 加载自己对应的模块文件,如:command
- 通过ansible将模块或命令生成对应的临时py文件,并将文件传输至远程服务器
- 对应执行用户家目录的:.ansible/tmp/xxx/xxx.py文件
- 给文件+x执行权限
- 执行并返回结果
- 删除临时的py文件,sleep 0 退出
2. 工作原理
二、Ansible部署
1. 环境准备
我使用的是VMware+云服务器组合的方式
ansible服务器:192.168.1.120
ansible客户机:192.168.1.131、114.115.179.133
创建好环境之后,做系统的基础配置:IP、yum源、防火墙等等。此处就不细说了
2. Ansible安装
ansible只需要再主服务器进行安装即可,被控主机无需安装
- 安装
yum install -y ansible
- 查询,列出和ansible相关文件
rpm -ql ansible
- 查询配置文件
rpm -qc ansible
ll /etc/ansible
- 查询ansible控件
ansible-doc -l
3. 客户机登录(可选)
后续在连接客户机时,需要用到ssh远程工具,可以通过ssh-key进行连接
ssh-keygen
ssh-copy-id 114.115.179.133
ssh root@114.115.179.133
4. Ansible基础
ansible 所有命令都是基于ssh连接进行通信的,所以当目标主机ssh无法连接时,ansible也将失效
ansible localhost -m ping
5. Inventory-主机清单
ansible默认的主机清单在/etc/ansible/hosts
一般情况下需要自己指定主机清单
接下来将以 /server/script/hosts为例
- 主机清单的大致格式如下
- 其中包含了主机组 [date:children] 、变量 [vmware:vars] 、用户名密码的配置方式
6. Ad-Hoc 点对点模式
ansible的工作模块
1. shell模块
直接执行bash命令
ansible -i /server/script/hosts data -m shell -a 'hostname'
2. 复制模块-copy
ansible-doc copy # 通过手册查看帮助示例
ansible data -m copy -a 'src=/opt/date.sh dest=/tmp/date.sh owner=root group=bin mode=777 backup=yes'
# src 为源文件地址,ansible主机上的地址
# dest 为目标主机地址
# owner 为文件拥有者
# mode 为文件属性
# backup 当文件发生变化,将备份文件并传输新文件
3. 用户模块-user
- 创建用户
ansible -i /server/script/hosts /data -m user -a 'name=wow state=present'
# state 状态,present为生成,absent为删除
2. 修改密码
- 生成加密密码
通过ssl生成一个加密密码
echo '112233' | openssl passwd -1 -stdin
- 修改密码
ansible -i /server/script/hosts data -m user -a 'name=wow password="$1$Zcy7H0Ys$tJ0Drh0R8nWG4WKgmno4V0"'
- 修改shell
ansible -i /server/script/hosts data -m user -a 'name=wow shell=/sbin/nologin append=yes'
修改wow用户的登录权限成功
4. 软件包管理模块-yum
ansible -i /server/script/hosts data -m yum -a 'name="httpd" state=latest'
5. 服务管理模块-service
ansible -i /server/script/hosts data -m service -a 'name=httpd state=started enable=yes'
# state状态可以是started、stoped、restarted
# enable 开机自启
6. 文件模块-file
- 创建文件
ansible -i /server/script/hosts data -m file -a 'path=/tmp/123.txt mode=777 state=touch'
- 创建目录
ansible -i /server/script/hosts data -m file -a 'path=/tmp/123.txt mode=777 state=directory'
7. 收集模块-setup
用于查看目标主机的消息信息
ansible -i /server/script/hosts data -m setup -a 'filter=ansible_all_ipv4_addresses'
8. 拉取文件模块-fetch
- 从远程主机获取文件到本地
执行后会在本地生成新的目录
ansible -i /server/script/hosts huawei -m fetch -a 'src=/opt/fff.txt dest=/tmp/'
9. 计划任务模块-cron
计划任务管理
ansible -i /server/script/hosts data -m cron -a 'name="sync time from ntpserver" minute="*/10" job="/sbin/ntpdate ntp.aliyun.com & /dev/null"'
10. 用户组模块-group
ansible -i /server/script/hosts data -m group -a 'name=g1 gid=1010 state=present system=yes'
11. 脚本模块-script
ansible -i /server/script/hosts data -m script -a '/opt/date.sh'
12. 解压模块-unaichive
ansible -i /server/script/hosts data -m unarchive -a 'src=/opt/ceshi.tar.gz dest=/opt/ceshi/'
三、YAML非标记语言
1. 语法
- 列表
必须定格写,最后要有冒号
第二行的属性先敲两个空格,然后加一个 - 号
- 字典
如:通过yaml编写一个简单的剧本,完成web的部署,配置,启动全过程
2. 示例
- 准备工作
- 清理环境
ansible -i /server/script/hosts data -m yum -a 'name=httpd state=removed' -o
- 主服务器安装httpd
yum install httpd -y
- 编辑httpd的配置文件
# 将/etc/apache/conf/httpd.conf文件备份出来进行编辑
# 修改Listen监听端口为 9090
- 编写剧本
cat httpd.conf
- hosts: data
tasks:
- name: install apache packages-httpd
yum: name=httpd state=present
- name: copy httpd.conf
copy: src=/opt/httpd.conf dest=/etc/httpd/conf/httpd.conf
- name: ensure apache is running
service: name=httpd state=started enabled=yes
- 测试
- 检查语法是否有误
ansible-playbook -i /server/script/hosts apache.yaml --syntax-tasks
2. 列出任务、host
ansible-playbook -i /server/script/hosts apache.yaml --list-tasks
ansible-playbook -i /server/script/hosts apache.yaml --list-hosts
- 执行剧本
ansible-playbook -i /server/script/hosts apache.yaml
- 检查目标主机是否配置成功
- handlers
在配置文件发生变化时,通知restar apache,执行下面的操作
- hosts: data
tasks:
- name: install apache packages-httpd
yum: name=httpd state=present
- name: copy httpd.conf
copy: src=/opt/httpd.conf dest=/etc/httpd/conf/httpd.conf
notify: restart apache
- name: ensure apache is running
service: name=httpd state=started enabled=yes
handlers:
- name: restart apache
service: name=httpd state=restaretd
四、Role-角色扮演
roles 则是在anoible中,playbooks的目录组织结构。
将代码或文件进行模块化,成为roles的文件目录组织结构,易读,代码可重用,层次清晰。
1. 准备配置文件
- 下面会用到nginx的配置文件,所以先在ansible主机上进行安装生成该文件
yum install -y nginx
2. 目录结构
环境准备
mkdir roles/nginx/{files,handlers,tasks,templates,vars} -p
touch roles/site.yaml roles/nginx/{handlers,tasks,vars}/main.yaml
echo 1223311 > roles/nginx/files/index.html
# 将nginx的配置文件拷贝到指定目录下
cp /etc/nginx/nginx.conf roles/nginx/templates/nginx.conf.j2
3. 编写任务-tasks
- 编辑ansible任务
vim roles/nginx/tasks/main.yaml
---
# 安装nginx依赖
- name: install epel-release packge
yum: name=epel-release state=latest
# 安装nginx
- name: install nginx packge
yum: name=nginx state=latest
# 推送自定义主页
- name: copy index.html
copy: src=index.html dest=/usr/share/nginx/html/index.html
# 推送配置文件
- name: copy nginx.conf template
template: src=/nginx.conf.j2 dest=/etc/nginx/nginx.conf
notify: restart nginx
# 确保服务启动
- name: make sure nginx service is running
service: name=nginx state=started enabled=yes
4. 编辑配置文件-templates
template 又名 :金甲模板
框出来的部分为ansible可识别变量(目标主机的服务器核心数)
框出来的为自定义变量(nginx可同时服务多少用户,多并发)
5. 编写变量-vars
自定义设置变量值
vim roles/nginx/vars/main.yaml
6. 编写处理程序-handlers
vim roles/nginx/handlers/main.yaml
7. 编写剧本-site.yaml
vim roles/site.yaml
8. 实施测试
注意:在执行剧本的时候,最好进入到roles目录下,这样的话,脚本就能自动找到引用的文件了
# 先检查斯特.yaml语法
ansible-playbook -i /server/script/hosts site.yaml --syntax-check
# 没问题的话就执行
ansible-playbook -i /server/script/hosts site.yaml
9. 验证金甲模板
在目标主机上查看nginx的配置文件
vim /etc/nginx/nginx.conf
主机192.168.1.131
主机114.115.179.133