大型网站构架 ansible

ansible简介

abstract
在这里插入图片描述
ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,
实现了批量系统配置、批量程序部署、批量运行命令等功能。
无客户端。

  • 工作原理
    在这里插入图片描述

ansible部署

(1)dns resolvej解析
vim /etc/hosts
(2)Ansible客户机不需要配置
需:有ip yum源
安装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

然后检查部署是否完成

Rpm –ql ansible  列出所有文件
Rpm –qc ansible  查看配置文件
Ansible –help 查看ansible帮助
Ansible-doc-l  看所有模块(A10,华为,docker,EC2,aws等等广大厂商设备)(轻易不要尝试  内容比较繁多 看不过来(慎重))
Ansible-doc-s yum  查看yum板块


Ssh-key 免密登录
Ssh-keygen 获得免密内容
Ssh-copy-id  ip地址 推送公钥



ansible 基础配置

1.	定义主机清单
Vim /etc/ansible/hosts  添加主机
2.	测试连通性
Ansible localhost –m ping  
-m 指定模块。ping只是其中一个模块。还有shell,yum等等 

3.	简介输出
Ansible localhost –m ping –o
4.	Know_hosts
Ansible host1 –m ping –u root –k –o  增加用户名选项、密码选项
去掉(yes/no)的询问
Vim /etc/ssh/ssh_config
#strictHostKeyCheking yes > no
重启服务
Systemctl restart sshd

Ansible host2 –m ping –u root –k –o  成功就不提示了

  • inventory 主机清单
    含义:清查;存货清单;财产目录;主机清单
1.	增加主机组
Vim /etc/ansible/hosts
[webserver]
Host1
Host2
Ansible webserver –m ping –o 
输出提示
Host1 | SUCCESS => {"changed": false, "ping": "pong"}
Host2 | SUCCESS => {"changed": false, "ping": "pong"}
2.	增加用户名 密码 
Vim /etc/ansible/hosts
[webserver]
host[1:4] ansible_ssh_user='root' ansible_ssh_pass='666666' 注意不能折行
ansible webserver –m ping –o 免用户和密码成功
用户名密码不同单独设置
[webservers]
host1 ansible_ssh_user='root' ansible_ssh_pass='777777'
host[2:4] ansible_ssh_user='root' ansible_ssh_pass='666666'
3.	增加端口
更改host1的sshd程序端口修改2222
Vim /etc/ssh/sshd_config
Port 2222
Systemctl restart sshd
Ansible webserver –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'
改完之后记得恢复原状
4.	组:变量
ansible内部变量可以帮助我们简化主机清单的设置
vim /etc/ansible/hosts
[webserver]
host[1:4]
[webserver:vars]
ansible_ssh_user='root'
ansible_ssh_pass='666666'
常用变量

在这里插入图片描述

5.	子分组
将不同的分组进行组合
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'
6.	自定义主机列表
Vim  hostlist
[dockers]
host1
host2
[dockers:vars]
ansible_ssh_user='root'
ansible_ssh_pass='666666'
ansible –i hostlist dockers –m ping –o
新文件 –i 加文件名 加组名

Ad-hoc 点对点模式
临时的,在ansible中是指需要快速执行的单条命令,并且不需要保存的命令。对于复杂的命令则为 playbook。

  • Ad-hoc 点对点模式
    临时的,在ansible中是指需要快速执行的单条命令,并且不需要保存的命令。对于复杂的命令则为 playbook。
1.	Shell模块
Ansible-doc shell  帮助
Ansible webserver –m shell –a ‘hostname’ –o  获取主机名
Ansible webserver –m shell –a ‘hostname’ –o –f 2     -f 2 指定线程数
  -f FORKS, --forks=FORKS  Ansible一次命令执行并发的线程数。NUM被指定为一个整数,默认是5
specify number of parallel processes to use
(default=5)
Ansible host1 –m shell –a ‘yum –y install httpd‘ –o 部署apache
Absible host1 –m shell –a ‘uptime’ –o  查询系统负载
2.	复制模块
Ansible-doc copy 复制帮助
Ansible webserver –m copy ‘src=/etc/hosts dest=/tmp/1.txt owner=root group=root mode=777’  复制
ansible webserver -m copy -a 'src=/etc/hosts dest=/tmp/2.txt owner=root group=bin mode=777 backup=yes'  追加备份
3.	用户模式
Ansible-doc user  用户帮助
(1)创建用户
ansible webserver -m user -a 'name=qianfeng state=present'
(2)修改密码
先生成加密密码 :echo '777777' | openssl passwd -1 –stdin
再修改密码
ansible webserver -m user -a 'name=zhangsan 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'


4.	软件包管理
Absible-doc yum
Absible host1 –m yum –a ‘name=”*” state=latest ‘  升级所有包
Absible host1 –m yum –a ‘name=“httpd“ state=latest‘  安装apache
Absible host1 –m yum –a ‘name=“httpd“ state=absent‘ 删除apache
5.	服务模块
Ansible-doc server 服务帮助
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'  开机禁止启动
6.	文件模块
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'  创建目录
7.	收集模块
ansible-doc setup 收集帮助
ansible host3 -m setup  查询所有信息
ansible host3 -m setup -a 'filter=ansible_all_ipv4_addresses'  查询ip地址

YAML-非标记语言

  • 语法
    (1)列表Fruits:
    -apple ;-orange ;-strawberry ;-mango
    (2)字典martin:
    name:martin d’vloper
    job:developer
    skill:elite

  • 示例

1.	准备工作
Ansible all –m yum –a ‘name=httpd  state=remove’ –o  		清理环境
Yum –y install httpd  准备配置文件
Mkdir apache
Cp –rf /etc/httpd/conf/httpd.conf .  别忘了后面有个点 (当前目录)
Grep ‘^Listen’ httpd.conf
Listen 8080  修改配置,用作配送
2.	编写剧本
Vim apache.yaml
- hosts: host2
  tasks:  任务
  - name: install apache packages
    yum: name=httpd state=present  yum模块 安装
  - name: copy apache conf
    copy: src=./httpd.conf dest=/etc/httpd/conf/httpd.conf  拷贝到httpd.Conf
  - name: ensure apache is running
service: name=httpd state=started enabled=yes  开启httpd服务  开机自启

3.	测试
Ansible-playbook apache.yaml -syntax-check 检验语法
Ansible-playbook apache.yaml -list-tasks  列出任务
Ansible-playbook apache.yaml -list-hosts  列出主机
Ansible-playbook apache.yaml 执行脚本
这里要注意端口号 默认80  上面修改端口是8080
4.	Handlers
这里假设配置文件发生变化  端口Listen 9000
Ansible-playbook apache.yaml
执行脚本,成功并没有报错  但配置未生效,所以在此增加处理程序,触发器
Vim apache.yaml

在这里插入图片描述
然后修改配置文件 Listen 9080
Ansible-playbook apache.yaml
执行脚本,配置生效,触发成功

  • role 角色扮演
    roles则是在ansible中,playbooks的目录组织结构。将代码或文件进行模块化,成为roles的文件目录组织结构,易读,代码可重用,层次清晰。
    目的就是为了通过role远程部署ngiinx并配置
  1. 目录结构
    在这里插入图片描述
    nginx 角色名
    files 普通文件
    handlers 触发器程序
    tasks 主任务
    templates 金甲模板(有变量的文件)
    vars 自定义变量
实验开始准备目录结构
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


2.	编写任务 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
3.	准备配置文件 vim roles/nginx/templates/nginx.conf.j2
worker_processes  {{ ansible_processor_cores }}; 调用内部已知变量
worker_connections {{ worker_connections }};  自定义变量
4.	编写变量 vim roles/nginx/vars/main.yaml  worker_connections: 10240
5.	编写处理程序 vim roles/nginx/handlers/main.yaml
---
- name: restart nginx
  service: name=nginx state=restarted
6.	编写剧本 vim roles/site.yaml
 - hosts: host4
  roles:
  - nginx
7. 实验开始
Cd roles
Ansible-playbook site.yaml  -syntax-check  测试一下
Ansible-playbook site.yaml 执行脚本
验证host4

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值