文章目录
1.ANSIBLE-简介
简介
ansible是新出现的自动化运维工具,基于Python开发,
集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配
置、批量程序部署、批量运行命令等功能。
ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模
块,ansible只是提供一种框架。主要包括:
(1)、连接插件connection plugins:负责和被监控端实现通信;
(2)、host inventory:指定操作的主机,是一个配置文件里面定义监控的主机;
(3)、各种模块核心模块、command模块、自定义模块;
(4)、借助于插件完成记录日志邮件等功能;
(5)、playbook:剧本执行多个任务时,非必需可以让节点一次性运行多个任务。
工作原理图
2.部署
① dns resolve
环境
一台服务器
多台客服机
并在服务器上做域名解析,添加服务器名已经客服机名及IP
vim /etc/hosts
注:客户机无需配置
② install ansible
yum install -y epel-release
安装epel源,如果您在内网环境,请使用下方阿里YUM
rm -rf /etc/yum.repos.d/*
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum install -y ansible
检测部署是否完成
rpm -ql ansible
列出所有文件
rpm -qc ansible
查看配置文件
ansible --help
查看ansible帮助
ansible-doc -l
看所有模块(A10,华为,docker,EC2,aws等等广大厂商设备)
ansible-doc -s yum
看yum模块,了解其功能
install (`present' or `installed', `latest'),
or remove (`absent' or `removed')
yum list
Package name
enablerepo
3.ssh-key(可选)
免密码ssh-key的方式。
ssh-keygen
ssh-copy-id IP地址
推送公钥
4.ansible基础
①1.定义主机清单
vim /etc/ansible/hosts
host1
host2
*注意此处少一个主机。请留意*
② 测试连通性
ansible localhost -m ping
测试host1连通性
-m 指定模块。什么功能
ping只是其中一个模块。还有shell,yum等等
③简洁输出
ansible host1 -m ping -o
因为为添加用户与密码选项
添加用户与密码后
ansible host2 -m ping -uroot -k -o
去掉(yes/no)的询问
vim /etc/ssh/ssh_config
StrictHostKeyChecking no
systemctl restart sshd
修改前
修改后
④增加用户名 密码(添加变量)
vim /etc/ansible/hosts
[webservers]
host1
[webserver:vars]
ansible_ssh_user='root'
ansible_ssh_pass='777777'
效果
实现免密登录
5.Inventory -主机清单
①含义
清查;存货清单;财产目录;主机清单
② 增加端口
请将host1的sshd程序端口修改为2222
# vim /etc/ssh/sshd_config
Port 2222
# systemctl restart sshd
ansible webservers -m ping -o
失败,因为默认端口已更改
vim /etc/ansible/hosts
[webserver]
host1 ansible_ssh_user=‘root’ ansible_ssh_pass=‘777777’ ansible_ssh_port=‘2222’
host[2:4] ansible_ssh_user=‘root’ ansible_ssh_pass=‘666666’
请将用户名密码和端口回复原状
③ 子分组
将不同的分组进行组合
vim /etc/ansible/hosts
[apache]
host[1:2]
[nginx]
host[3:4]
[webserver:children]
apache
nginx
[webserver:vars]
ansible_ssh_user=‘root’
ansible_ssh_pass=‘666666’
④自定义主机列表
vim hostlist
[dockers]
host1
host2
[dockers:vars]
ansible_ssh_user=‘root’
ansible_ssh_pass=‘666666’
注意您的计算机密码
ansible -i hostlist dockers -m ping -o
6.Ad-Hoc-点对点模式
〇 简介
临时的,在ansible中是指需要快速执行的单条命令,并且不需要保存的命令。对于复杂的命令则为 playbook。
①复制模块
帮助
ansible-doc copy
案例
ansible webserver -m copy -a ‘src=/etc/hosts dest=/tmp/2.txt owner=root group=bin mode=777’
传输并设置属主,属组,权限
ansible webserver -m copy -a ‘src=/etc/hosts dest=/tmp/2.txt owner=root group=bin mode=777 backup=yes’
如果文件有多份,可以进行备份。
[root@localhost ~]# ls /tmp/
2.txt 2.txt.17037.2017-11-16@16:23:41~
②用户模块
帮助
ansible-doc user
创建用户
ansible webserver -m user -a 'name=qianfeng state=present'
创建
修改密码
1.生成加密密码
echo '777777' | openssl passwd -1 -stdin
生成加密密码值
$1$XVzsJMDr$5wI4oUaQ.emxap6s.N272.
2.修改密码
ansible webserver -m user -a 'name=qianfeng password="$1$XVzsJMDr$5wI4oUaQ.emxap6s.N272."'
3.修改shell
ansible webserver -m user -a 'name=qianfeng shell=/sbin/nologin append=yes'
4.删除用户
ansible webserver -m user -a 'name=qianfeng state=absent'
删除
添加remove=yes彻底删除
①②
③
④
③软件包管理
帮助
ansible-doc yum
ansible host1 -m yum -a ‘name="*" state=latest’
升级所有包
ansible host2 -m yum -a ‘name=“httpd” state=latest’
安装apache
④服务模块
帮助
ansible-doc service
ansible host2 -m service -a 'name=httpd state=started'
启动
ansible host2 -m service -a 'name=httpd state=started enabled=yes'
开机启动
ansible host2 -m service -a 'name=httpd state=stopped'
停止
ansible host2 -m service -a 'name=httpd state=restarted'
重启
ansible host2 -m service -a 'name=httpd state=started enabled=no'
开机禁止启动
⑤文件模块
帮助
ansible-doc file
ansible host1 -m file -a 'path=/tmp/88.txt mode=777 state=touch'
创建文件
ansible host1 -m file -a 'path=/tmp/99 mode=777 state=directory'
⑥收集模块
帮助
ansible-doc setup
ansible host3 -m setup
查询所有信息
ansible host3 -m setup -a 'filter=ansible_all_ipv4_addresses'
⑦shell模块
帮助
ansible-doc shell
ansible webserver -m shell -a 'hostname' -o
获取主机名
ansible webserver -m shell -a 'hostname' -o -f 2
-f 2 指定线程数
ansible host2 -m shell -a 'yum -y install httpd' -o
部署apache
ansible host3 -m shell -a 'uptime' -o
查询系统负载
7.YAML-YAML Ain’t Markup Language-非标记语言
①语法
列表
fruits:
- Apple
- Orange
- Strawberry
- Mango
字典
martin:
name: Martin D'vloper
job: Developer
skill: Elite
需求
通过YAML编写一个简单的剧本,完成web的部署,配置,启动的全过程。
②准备工作
ansible all -m yum -a 'name=httpd state=removed' -o
清理一下环境
yum install -y httpd
准备配置文件
mkdir apache
cd apache
cp -rf /etc/httpd/conf/httpd.conf .
grep '^Listen' httpd.conf
Listen 8080
修改配置,用作推送
③编写剧本
vim apache.yaml
- hosts: host2
tasks:
- name: install apache packages
yum: name=httpd state=present
- name: copy apache conf
copy: src=./httpd.conf dest=/etc/httpd/conf/httpd.conf
- name: ensure apache is running
service: name=httpd state=started enabled=yes
④ 测试:
ansible-playbook apache.yaml --syntax-check
检验语法
ansible-playbook apache.yaml --list-tasks
列出任务
ansible-playbook apache.yaml --list-hosts
列出主机
ansible-playbook apache.yaml
执行
http://192.168.2.142:8080/
注意端口
⑤handlers
如果配置文件发生变化。
Listen 9000
ansible-playbook apache.yaml
再次执行,命令成功,但配置未生效,所以要增加处理程序。设置触发器
vim apache.yaml
ansible-playbook apache.yaml
再次执行,配置生效,触发成功
8.Role-角色扮演
简介
roles则是在ansible中,playbooks的目录组织结构。
将代码或文件进行模块化,成为roles的文件目录组织结构,
易读,代码可重用,层次清晰。
目标
通过role远程部署nginx并配置
①目录结构
mkdir roles/nginx/{files,handlers,tasks,templates,vars} -p
touch roles/site.yaml roles/nginx/{handlers,tasks,vars}/main.yaml
echo 1234 > roles/nginx/files/index.html
yum install -y nginx && cp /etc/nginx/nginx.conf roles/nginx/templates/nginx.conf.j2
nginx 角色名
files 普通文件
handlers 触发器程序
tasks 主任务
templates 金甲模板(有变量的文件)
vars 自定义变量
准备目录结构
②编写任务
vim roles/nginx/tasks/main.yaml
---
- name: install epel-release packge
yum: name=epel-release state=latest
- 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 running
service: name=nginx state=started enabled=yes
对迭代项的引用,固定变量名为"item”,使用with_item属性给定要迭代的元素;
③准备配置文件
vim roles/nginx/templates/nginx.conf.j2
worker_processes {{ ansible_processor_cores }};
调用内部已知变量
worker_connections {{ worker_connections }};
自定义变量
④编写变量
vim roles/nginx/vars/main.yaml
worker_connections: 10240
5.编写处理程序
vim roles/nginx/handlers/main.yaml
---
- name: restart nginx
service: name=nginx state=restarted
⑥编写剧本
vim roles/site.yaml
- hosts: host4
roles:
- nginx
⑦实施
cd roles
ansible-playbook site.yaml --syntax-check
测试
ansible-playbook site.yaml
实施剧本
验证host4
注意:当出现
是nginx端口冲突原因
vim roles/nginx/templates/nginx.conf.j2
以及
vim /etc/nginx/nginx.conf
将80端口修改即可成功