Ansible-综合练习-生产案例

斌的招儿

        网上教程大多都是官网模板化的教程和文档,这里小斌用自己实际生产环境使用的例子给大家做一个详解。涉及到一整套ansible的使用,对于roles的使用,也仅涉及到tasks和files目录,方便大家快速上手并规范化管理。

0.环境配置

192.168.255.120 Ansible控制机

192.168.255.123 Ansible被控机

1.安装Ansible

作为学习模拟使用,这里就使用yum安装

[root@120 ~]# yum install -y ansible

2.配置免密

[root@120 ~]# ssh-keygen

[root@120 ~]# ssh-copy-id root@192.168.255.123

3.配置主机列表

配置主机列表,我们一般会选择在hosts文件中定义,但是面临一个问题,后续管理主机多,不同项目也有可能设计同一台主机,我们每次都要在一个文件里追加追加,极为不便,所以我们单独建一个目录用来管理主机列表。

我们修改一下配置文件里的主机清单位置,指定一个目录,目录下文件以.conf结尾,书写格式于hosts中一致。

4.测试主机连通性

[root@120 ansible]# ansible yzb -m ping
192.168.255.123 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
[root@120 ansible]#

5.生成角色

这里不需要手动创建目录和文件,可以借助ansible自带的ansible-galaxy工具。

[root@120 roles]# pwd
/etc/ansible/roles

[root@120 roles]# ls

[root@120 roles]# ansible-galaxy init init-nginx
- Role init-nginx was created successfully

[root@120 roles]# ls
init-nginx

[root@120 roles]# tree init-nginx/
init-nginx/
├── defaults
│   └── main.yml
├── files
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── README.md
├── tasks
│   └── main.yml
├── templates
├── tests
│   ├── inventory
│   └── test.yml
└── vars
    └── main.yml

8 directories, 8 files
[root@120 roles]#

我们可以看到使用 ansible-galaxy init 命令创建了一个新角色的框架

6.配置

开头说过了,仅涉及到tasks和files目录,方便大家快速上手并规范化管理。所以它来了:

这里我们以安装nginx为例子,yum没难度是吧,好的,我们用源码安装的形式教学。

1.熟悉源码安装nginx

只有熟悉怎么源码安装nginx,才能编写剧本自动化安装。源码安装其实就四步:

1.包弄上来解压

2.添加补丁

3.编译安装

4.添加systemed管理

2.准备文件

如图,我们看到四个文件,文件作用如下:

    • nginx.tar.gz:包含nginx源码包和补丁包
    • install.sh:内容为将补丁文件应用到源代码文件中,并执行编译安装,写成脚本减少playbook的篇幅
    • nginx.conf:nginx的配置文件,如果业务有特殊模块需求,可以提前编写好配置文件发放到目标主机
    • nginx.service:nginx的systemed管理文件

nginx.conf

[root@120 files]# cat nginx.conf
pid /usr/local/nginx/nginx.pid;

worker_processes 4;
worker_cpu_affinity 1000 0100 0010 0001;
worker_rlimit_nofile 102400;

events {
    worker_connections 102400;
    multi_accept on;
    use epoll;
}

http {

    vhost_traffic_status_zone;
    
    log_format json_format '{"timestamp":"$msec",'
        '"time_iso":"$time_iso8601",'
        '"time_local":"$time_local",'
        '"request_time":"$request_time",'
        '"remote_user":"$remote_user",'
        '"remote_addr":"$remote_addr",'
        '"http_x_forwarded_for":"$http_x_forwarded_for",'
        '"request":"$request",'
        '"status":"$status",'
        '"body_bytes_send":"$body_bytes_sent",'
        '"upstream_addr":"$upstream_addr",'
        '"upstream_response_time":"$upstream_response_time",'
        '"upstream_http_content_type":"$upstream_http_content_type",'
        '"upstream_http_content_disposition":"$upstream_http_content_disposition",'
        '"upstream_status":"$upstream_status",'
        '"http_user_agent":"$http_user_agent",'
        '"http_referer":"$http_referer",'
        '"connection":"$connection",'
        '"connection_requests":"$connection_requests",'
        '"scheme":"$scheme",'
        '"host":"$host",'
        '"http_via":"$http_via",'
        '"request_id":"$request_id"}';

    map $time_iso8601 $logdate {
        '~^(?<ymd>\d{4}-\d{2}-\d{2})' $ymd;
        default  'date-not-found';
    }

    # 日志文件名中带有变量时由子进程创建,需要子进程具有目录写入权限
    access_log /ware/logs/nginx/access-$logdate.log json_format;
    error_log /ware/logs/nginx/error.log error;

    server_tokens off;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 30;
    keepalive_requests 100000;
    client_header_timeout 10;
    client_body_timeout 10;
    client_max_body_size 100m;
    reset_timedout_connection on;
    send_timeout 10;
    include mime.types;
    default_type application/octet-stream;
    charset UTF-8;

    gzip on;
    gzip_vary on;
    gzip_disable "MSIE [1-6].";
    gzip_http_version 1.0;
    gzip_comp_level 4;
    # gzip_static on;
    gzip_min_length 1024;
    gzip_buffers 4 16k;
    gzip_proxied expired no-cache no-store private auth;
    gzip_types text/plain text/css text/javascript application/x-javascript application/xml application/json application/xml+rss;

    open_file_cache max=100000 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 2;
    open_file_cache_errors on;
    proxy_connect_timeout 75;
    proxy_read_timeout 300;
    proxy_send_timeout 300;
    proxy_buffer_size 64k;
    proxy_buffers 4 64k;
    proxy_busy_buffers_size 128k;
    proxy_temp_file_write_size 128k;
   
    include /opt/env/nginx/servs/*.upstreams;
    include /opt/env/nginx/servs/http-*.conf;
}

stream {
    include /opt/env/nginx/servs/tcp-*.conf;
}

 nginx.service

[root@120 files]# cat nginx.service 
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

install.sh 

[root@120 files]# cat install.sh 
#!/bin/bash

dir=/server/storage

cd $dir

tar xf nginx.tar.gz

cd /server/storage/nginx

unzip  nginx_upstream_check_module.zip
unzip  nginx-upstream-fair.zip
unzip  nginx-module-vts.zip
unzip  ngx_http_substitutions_filter_module.zip
tar zxvf nginx-1.16.1.tar.gz

cd $dir/nginx/nginx-upstream-fair-master
sed -i 's/default_port/no_port/g' ngx_http_upstream_fair_module.c
patch -p1 < $dir/nginx/nginx_upstream_check_module-master/upstream_fair.patch

cd $dir/nginx/nginx-1.16.1
patch -p1 < $dir/nginx/nginx_upstream_check_module-master/check_1.16.1+.patch

cd $dir/nginx/nginx-1.16.1

./configure \
--prefix=/usr/local/nginx \
--with-http_realip_module \
--with-http_ssl_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-stream \
--with-http_stub_status_module \
--add-module=${dir}/nginx/nginx_upstream_check_module-master \
--add-module=${dir}/nginx/nginx-upstream-fair-master \
--add-module=${dir}/nginx/nginx-module-vts-master \
--add-module=${dir}/nginx/ngx_http_substitutions_filter_module-master

make && make install
3.编写剧本
[root@120 tasks]# cd /opt/ansible/roles/init-nginx/tasks

[root@120 tasks]# cat main.yml 
---
# tasks file for ./nginx

- name: create nginx dir
  file: 
    path: /opt/env/nginx/servs
    state: directory
    owner: root
    group: root
    mode: 0755

- name: create nginx certs
  file:
    path: /opt/env/nginx/certs
    state: directory
    owner: root
    group: root
    mode: 0755

- name: create nginx logs
  file:
   path: /ware/logs/nginx
   state: directory
   owner: root
   group: root
   mode: 0755
      
- name: modify logs 
  shell: chmod -R 777 /ware/logs/nginx


- name: yum install
  yum: 
    name: "{{ with_item }}"
    state: latest
  vars:
    with_item:
      - wget
      - zip
      - unzip
      - patch
      - gcc
      - gcc-c++
      - pcre
      - pcre-devel
      - zlib
      - zlib-devel
      - openssl
      - openssl-devel


- name: copy nginx.tar.gz
  vars:
  - item: /server/storage/
  copy:
    src: nginx.tar.gz
    dest: "{{ item }}"
    owner: root
    group: root
    mode: 0644

- name: copy install.sh
  copy:
    src: install.sh
    dest: /server/storage/nginx/
    mode: 755


- name: install nginx
  shell: /server/storage/nginx/install.sh

- name: copy nginx conf
  copy:
    src: nginx.conf
    dest: /usr/local/nginx/conf/nginx.conf
    owner: root
    group: root
    mode: 0644

- name: copy nginx.service
  copy:
    src: nginx.service
    dest: /usr/lib/systemd/system
    owner: root
    group: root
    mode: 0644

- name: daemon reload
  shell: systemctl daemon-reload

- name: enable nginx
  service:
    name: nginx
    enabled: yes
    
[root@120 tasks]#

7.你写的不对

我们写的playbook都是以hosts开头的,你这直接以具体的task开头,这咋运行?

        没错,一般的剧本,都是以上图的格式书写的,但是我们学习Ad-Hoc和roles是干啥的,为了我们的规范化和模块化,要让不同的主机组都能使用一个roles。所以我们需要建一个文件,内容如下:

[root@120 ansible]# cat init-nginx.yml 
- hosts: yzb
  remote_user: root
  roles:
    - role: init-nginx

        我们单独拉出来一个文件,这样可以使不同的主机组、不同的任务都有单独的配置文件,清晰明了,也实现了roles的复用!

8.执行剧本,完成目标主机nginx安装

ansible-playbook init-nginx.yml

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/751395.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

私接路由器导致部分终端(电脑、手机等)无法上网问题分析

目录 【1】私接路由器场景 【2】进行网络基本配置&#xff0c;模拟终端可以正常上网 【2.1】Http-Server配置 【2.2】ISP配置 【2.3】R-hefa配置 【2.4】Client1配置 【2.5】PC配置 【2.6】测试验证上网是否正常 【3】私接路由器后再测试验证公司内网各终端访问外网是…

大模型AI技术实现语言规范练习

人工智能技术可以为语言规范练习提供多种有效的解决方案&#xff0c;帮助学习者更有效地掌握语言规范。以下是一些常见的应用场景。北京木奇移动技术有限公司&#xff0c;专业的软件外包开发公司&#xff0c;欢迎交流合作。 1. 智能纠错 利用自然语言处理技术&#xff0c;可以…

代码随想录-Day42

1049. 最后一块石头的重量 II 有一堆石头&#xff0c;用整数数组 stones 表示。其中 stones[i] 表示第 i 块石头的重量。 每一回合&#xff0c;从中选出任意两块石头&#xff0c;然后将它们一起粉碎。假设石头的重量分别为 x 和 y&#xff0c;且 x < y。那么粉碎的可能结果…

定制型汽车传感器在汽车中的应用

定制型汽车霍尔传感器在汽车中的应用及功能 曲轴和凸轮轴位置传感器&#xff1a; 这些传感器用于监测发动机的曲轴和凸轮轴的位置&#xff0c;帮助发动机管理系统精确控制点火时机和燃油喷射&#xff0c;提高发动机效率。 变速器控制系统&#xff1a; 在自动变速器中&#xf…

通达信短线抄底主升浪幅图指标公式源码

通达信短线抄底主升浪幅图指标公式源码&#xff1a; A1:REF(C,1); A2:SMA(MAX(C-A1,0),5,1)/SMA(ABS(C-A1),5,1)*1000; A3:BARSLAST(REF(CROSS("RSI.RSI1"(6,12,24),"RSI.RSI2"(6,12,24)),1)); A4:A2-LLV(A2,10); A5:(MA(A4,2)*3A4*13)/16; A6:IF(A5>1…

PTE-靶场训练-1

PTE-靶场训练实战笔记 靶场搭建 靶场下载链接&#xff1a; https://pan.baidu.com/s/1ce1Kk0hSYlxrUoRTnNsiKA?pwdha1x vim /etc/sysconfig/network-scripts/ifcfg-eth0 设置好后reboot重启一下即可&#xff0c;然后访问81-85端口&#xff0c;共5题。 因为靶场出了问题&a…

学生信息管理系统

DDL和DML -- 创建学生表 CREATE TABLE students (student_id INT PRIMARY KEY AUTO_INCREMENT,name VARCHAR(50),age INT,gender VARCHAR(10) );-- 创建课程表 CREATE TABLE courses (course_id INT PRIMARY KEY AUTO_INCREMENT,course_name VARCHAR(50) );-- 创建教师表 CREA…

酷瓜云课堂(内网版)v1.1.5 发布,局域网在线学习+考试系统

更新内容 更新layui-v2.9.10更新docker国内镜像地址增加导入镜像构建容器的方式教师不批阅非首次考试试卷轮播图增加专栏类型目标链接增加课程能否发布检查去除初始化kindeditor语言文件去除选择题EF选项优化富文本内容显示样式优化内容图片点击放大监听优化试题题干答案等图片…

基于STM32的智能水质监测系统

目录 引言环境准备智能水质监测系统基础代码实现&#xff1a;实现智能水质监测系统 4.1 数据采集模块4.2 数据处理与分析4.3 控制系统实现4.4 用户界面与数据可视化应用场景&#xff1a;水质管理与优化问题解决方案与优化收尾与总结 1. 引言 智能水质监测系统通过使用STM32嵌…

ONLYOFFICE桌面编辑器8.1版:个性化编辑和功能强化的全面升级

ONLYOFFICE是一款全面的办公套件&#xff0c;由Ascensio System SIA开发。该软件提供了一系列与微软Office系列产品相似的办公工具&#xff0c;包括处理文档&#xff08;ONLYOFFICE Document Editor&#xff09;、电子表格&#xff08;ONLYOFFICE Spreadsheet Editor&#xff0…

Pycharm主题切换(禁用)导致UI界面显示异常解决

安装其他主题 Material Theme UI One Dark theme One Dark theme安装 (Material Theme UI主题同理) Pycharm 打开 Settings > Plugins&#xff0c;搜索One Dark theme 安装即可 One Dark theme 效果显示 问题记录 UI显示异常 安装多个主题时&#xff0c;当禁用某些主题&…

通信协议总结

IIC 基本特点 同步&#xff0c;半双工 标准100KHz&#xff0c;最高400KHz&#xff08;IIC主要应用于低速设备&#xff09; 硬件组成 需外接上拉电阻 通信过程 空闲状态 SDA和SCL都处于高电平 开始信号S和终止信号P 在数据传输过程中&#xff0c;当SCL0时&#xff0c;SDA才…

Redis-主从复制-测试主从模式下的读写操作

文章目录 1、在主机6379写入数据2、在从机6380上写数据报错3、从机只能读数据&#xff0c;不能写数据 1、在主机6379写入数据 127.0.0.1:6379> keys * (empty array) 127.0.0.1:6379> set uname jim OK 127.0.0.1:6379> get uname "jim" 127.0.0.1:6379>…

高中数学:不等式-常见题型解题技巧

一、“1”的代换 练习 例题1 例题2 解 二、基本不等式中的“变形” 就是&#xff0c;一般情况下&#xff0c;我们在题目中&#xff0c;是不能够直接使用基本不等式进行求解的。 而是要对条件等式进行变形&#xff0c;满足基本不等式的使用条件 练习 例题1 解析 两边同…

002关于Geogebra软件的介绍及与MatLab的区别

为什么要学Geogebra&#xff1f; 因为和MatLab的科学计算相比&#xff0c;GeoGebra重点突出教学展示&#xff0c;对于教师、学生人群来讲再合适不过了&#xff0c;尤其是可以融入到PPT里边呈现交互式动画&#xff0c;想想听众的表情&#xff01;这不就弥补了看到PPT播放数学公…

关于ONLYOFFICE8.1版本桌面编辑器测评——AI时代的领跑者

关于作者&#xff1a;个人主页 目录 一.产品介绍 1.关于ONLYOFFICE 2.关于产品的多元化功能 二.关于产品体验方式 1.关于套件的使用网页版登录 2.关于ONLYOFFICE本地版 三.关于产品界面设计 四.关于产品文字处理器&#xff08;Document Editor&#xff09; 1.电子表格&a…

1954springboot VUE 天然气系统隐患管理系统开发mysql数据库web结构java编程计算机网页源码maven项目

一、源码特点 springboot VUE天然气系统隐患管理系统是一套完善的完整信息管理类型系统&#xff0c;结合springboot框架和VUE完成本系统&#xff0c;对理解JSP java编程开发语言有帮助系统采用springboot框架&#xff08;MVC 模式开发&#xff09;&#xff0c;系统具有完整的…

【HarmonyOS4学习笔记】《HarmonyOS4+NEXT星河版入门到企业级实战教程》课程学习笔记(十九)

课程地址&#xff1a; 黑马程序员HarmonyOS4NEXT星河版入门到企业级实战教程&#xff0c;一套精通鸿蒙应用开发 &#xff08;本篇笔记对应课程第 29 节&#xff09; P29《28.网络连接-第三方库axios》 要想使用第三方库axios&#xff0c;需要先安装ohpm&#xff0c;因为 axios…

Jupyter Notebook 说明 和 安装教程【WIN MAC】

一、Jupyter Notebook 简介&#xff08;来源百度百科&#xff09; Jupyter Notebook&#xff08;此前被称为 Python notebook&#xff09;是一个交互式笔记本&#xff0c;支持运行40多种编程语言。 Jupyter Notebook 的本质是一个Web应用程序&#xff0c;便于创建和共享程序文…

git基本使用(二):git分支的操作命令

Git 的多分支管理是指在同一个仓库中创建和管理多个分支&#xff0c;每个分支可以独立开发&#xff0c;互不干扰。分支是 Git 中的一种强大功能&#xff0c;允许开发人员同时在多个不同的功能、修复或实验上工作&#xff0c;而不会影响主分支或其他分支。通过多分支管理&#x…