一,ansible使用role的用途?

roles分别将变量/文件/任务/模板/handler等放置于单独的目录中,

并可以方便的include各目录下的功能

roles使playbook能实现代码被调用,避免了代码的重复

说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest

对应的源码可以访问这里获取: https://github.com/liuhongdi/

说明:作者:刘宏缔 邮箱: 371125307@qq.com

二,ansible例子:安装nginx

1,配置hosts

[root@centos8 roles]# vi /etc/ansible/hosts

内容:

[web]
172.18.1.1:22
172.18.1.2:22
172.18.1.3:22

2,role的目录结构

[root@centos8 roles]# tree
.
├── nginx
│ ├── files
│ │ ├── installnginx.sh
│ │ ├── nginx-1.18.0.tar.gz
│ │ └── nginx.service
│ ├── tasks
│ │ └── main.yml
│ ├── templates
│ │ └── nginx.conf.j2
│ └── vars
└── webinstallnginx.yml 5 directories, 6 files

各文件的用途说明:

webinstallnginx.yml是nginx这个role被执行的playbook的入口文件

tasks目录下的main.yml是task的执行入口文件

files目录存放需要用到的文件

installnginx.sh是安装nginx的脚本

nginx-1.18.0.tar.gz是下载好的nginx源码

nginx.conf:配置文件

nginx.service: 供systemd管理用的service文件

三,role目录下各文件的代码

1,webinstallnginx.yml

# roles: 调用role

[root@centos8 roles]# more webinstallnginx.yml
- hosts: web
remote_user: root
roles:
- nginx

2,nginx/tasks/main.yml

#gcc,make,pcre-devel,openssl-devel是编译nginx需要的软件

#/usr/local/soft:  软件安装目录

#/usr/local/source:   源文件、安装包保存的目录

#/data/nginx/logs:  保存nginx日志的目录

#user/group:添加用户nginx,用来运行nginx服务

[root@centos8 roles]# more nginx/tasks/main.yml
- name: install gcc
dnf: name=gcc disable_gpg_check=yes
- name: install make
dnf: name=make disable_gpg_check=yes
- name: install pcre-devel
dnf: name=pcre-devel disable_gpg_check=yes
- name: install openssl-devel
dnf: name=openssl-devel disable_gpg_check=yes
- name: Configure soft dir
file: path=/usr/local/soft/ state=directory mode=0755
- name: Configure source dir
file: path=/usr/local/source/ state=directory mode=0755
- name: copy nginx source file
copy: src=nginx-1.18.0.tar.gz dest=/usr/local/source/
- name: install nginx
script: installnginx.sh
- name: Configure log dir
file: path=/data/nginx/logs/ state=directory mode=0755
- name: add group:nginx
group: name=nginx
- name: add user:nginx
user: name=nginx group=nginx createhome=no shell=/sbin/nologin
- name: template conf file
template: src=nginx.conf.j2 dest=/usr/local/soft/nginx-1.18.0/conf/
- name: copy service file
copy: src=nginx.service dest=/usr/lib/systemd/system/
- name: start service
service: name=nginx state=started enabled=yes

3,nginx/files/installnginx.sh

#--with-http_stub_status_module:查看http状态的模块

#--with-http_ssl_module:实现对https的支持

[root@centos8 roles]# more nginx/files/installnginx.sh
cd /usr/local/source/;
tar -zxvf nginx-1.18.0.tar.gz;
cd /usr/local/source/nginx-1.18.0/;
./configure --prefix=/usr/local/soft/nginx-1.18.0 --with-http_stub_status_module --with-http_ssl_module;
make && make install;

4,nginx/templates/nginx.conf.j2

说明:在nginx.conf后加j2,表示这是一个jinja2文件,

也可以不加,不会影响ansible对它的处理

说明:{{ ansible_processor_cores }}  这个变量代表受控端机器的核心数量,

是供nginx优化使用的,因为受控机上的核心数量可能并不一致

这个变量的值通过setup模块可以看到,例子:

        [root@centos8 roles]# ansible 172.18.1.1 -m setup | grep processor_cores
"ansible_processor_cores": 1,

nginx.conf.j2 的内容:

[root@centos8 roles]# more nginx/templates/nginx.conf.j2
user nginx nginx;
worker_processes {{ ansible_processor_cores }};
error_log /data/nginx/logs/error.log;
pid logs/nginx.pid; events {
worker_connections 1024;
} http {
include mime.types;
default_type application/octet-stream;
server_tokens off;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /data/nginx/logs/access.log main;
sendfile on;
keepalive_timeout 60 45; gzip on;
gzip_disable "MSIE [1-6]\.";
gzip_comp_level 9;
gzip_types application/json text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip_http_version 1.1;
gzip_vary on;
gzip_proxied any; server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}

5,nginx/files/nginx.service

[root@centos8 roles]# more nginx/files/nginx.service
[Unit]
Description=nginx-The High-performance HTTP Server
After=network.target [Service]
Type=forking
PIDFile=/usr/local/soft/nginx-1.18.0/logs/nginx.pid
ExecStartPre=/usr/local/soft/nginx-1.18.0/sbin/nginx -t -c /usr/local/soft/nginx-1.18.0/conf/nginx.conf
ExecStart=/usr/local/soft/nginx-1.18.0/sbin/nginx -c /usr/local/soft/nginx-1.18.0/conf/nginx.conf
ExecReload=/usr/local/soft/nginx-1.18.0/sbin/nginx -s reload
ExecStop=/usr/local/soft/nginx-1.18.0/sbin/nginx -s stop
PrivateTmp=true [Install]
WantedBy=multi-user.target

四,安装nginx功能的执行效果:

1,执行playbook

[root@centos8 roles]# ansible-playbook  webinstallnginx.yml

2,完成后登录到服务器,检查状态

[root@web2 sbin]# systemctl status nginx
● nginx.service - nginx-The High-performance HTTP Server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2020-05-14 09:24:59 UTC; 1h 18min ago
...

五,查看ansible的版本

[root@centos8 roles]# ansible --version
ansible 2.9.7
config file = /etc/ansible/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.6/site-packages/ansible
executable location = /usr/bin/ansible
python version = 3.6.8 (default, Nov 21 2019, 19:31:34) [GCC 8.3.1 20190507 (Red Hat 8.3.1-4)]

ansible:安装nginx1.18.0(使用role功能)的更多相关文章

  1. centos7 安装 nginx-1.18.0 并设置开机自启动

    一.到官网下载nginx Mainline  version:  nginx主力版本,为开发版 Stable version: 稳定版,在生产环境中选择此版本进行安装 Legacy versions: ...

  2. centos8平台编译安装nginx1.18.0

    一,nginx的官网: http://nginx.org/ 说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest 对应的源码 ...

  3. 手动编译部署LNMP环境(CentOS7.5+Nginx-1.18.0+MySQL-5.7.30+PHP-7.4.14)

    在平时运维工作中,经常需要用到LNMP应用框架.LNMP环境是指在Linux系统下,由Nginx + MySQL + PHP组成的网站服务器架构. 可参考前面的文章: 如何在CentOS 7上搭建LA ...

  4. nginx集群:nginx配置负载均衡集群(nginx1.18.0)

    一,nginx的负载均衡集群的特点: 1,nginx集群和lvs的不同? lvs集群:工作在第4层(传输层) nginx集群:工作在第7层(应用层) lvs集群:性能更强 nginx集群:功能更强:可 ...

  5. CentOS 7.0编译安装Nginx1.6.0+MySQL5.6.19+PHP5.5.14

    准备篇: CentOS 7.0系统安装配置图解教程 http://www.osyunwei.com/archives/7829.html 一.配置防火墙,开启80端口.3306端口 CentOS 7. ...

  6. CentOS 6.2编译安装Nginx1.2.0+MySQL5.5.25+PHP5.3.13

    CentOS 6.2编译安装Nginx1.2.0+MySQL5.5.25+PHP5.3.132013-10-24 15:31:12标签:服务器 防火墙 file 配置文件 written 一.配置好I ...

  7. CentOS 7.0编译安装Nginx1.6.0+MySQL5.6.19+PHP5.5.14方法分享

    一.配置防火墙,开启80端口.3306端口 CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙. 1.关闭firewall: systemctl stop fi ...

  8. CentOS6.7上安装nginx1.8.0

    主题: CentOS6.7上安装nginx1.8.0 环境准备: 1.gcc-c++ 示例:yum install gcc-c++ 安装:gcc-c++ gcc-c++编译工具 2.PCRE(Perl ...

  9. nginx优化:配置gzip压缩页面提高访问速度(nginx1.18.0)

    一,为什么nginx要使用gzip 1,压缩的作用: 页面使用gzip压缩之后, 页面大小可以压缩到原来的1/7左右, 传输速度和页面打开时间都可以大幅度提高, 有利于用户访问页面体验的提升 2,Ng ...

随机推荐

  1. AD16

    第三集   制作光敏小夜灯的原理图 1.点击G切换栅格的精度 2.元器件放置好之后要先布局在布线 3.布线完成后要检查电路的合理性.对应查一下电阻的个数,位置是不是符合.在原理上大概的估计是否可以. ...

  2. 【吴恩达课程使用】pip安装pandas失败-anaconda各种玄学T-T-从新开始搭建环境

    [吴恩达课程使用]安装pandas失败-从新开始搭建环境 在第五课第二周的任务2中,虚拟环境缺少pandas,sklearn依赖,因为用pip比较顺手,就直接使用pip安装,结果各种anaconda环 ...

  3. 阿里巴巴内部Java成长笔记,首次曝光!真的香!

    前言 关于技术人如何成长的问题,一直以来都备受关注,因为程序员职业发展很快,即使是相同起点的人,经过几年的工作或学习,会迅速拉开极大的差距,所以技术人保持学习,提升自己,才能够扛得住不断上赶的后浪,也 ...

  4. php第三天-数组的定义,数组的遍历,常规数组的操作

    0x01 数组分类 在php中有两种数组:索引数组和关联数组 索引数组的索引值是整数,以0开始.当通过位置来标识东西时用索引数组. 关联数组是以字符串作为索引值,关联数组更像操作表.索引值为列名,用于 ...

  5. C语言实现顺序表的基本操作(从键盘输入 生成线性表,读txt文件生成线性表和数组生成线性表----三种写法)

    经过三天的时间终于把顺序表的操作实现搞定了.(主要是在测试部分停留了太长时间) 1. 线性表顺序存储的概念:指的是在内存中用一段地址连续的存储单元依次存储线性表中的元素. 2. 采用的实现方式:一段地 ...

  6. explain为mysql关键字,不能作为表字段创建

    在用jpa自动建表时,字段名命名为了explain,发现报实体类与数据库表字段不一致的错,查询才发现explain是mysql的关键字,无法作为表字段建立,特此记录

  7. 学习篇:NodeJS中的模板引擎:jade

    NodeJS 模板引擎作用:生成页面 在node常用的模板引擎一般是 1.jade --破坏式的.侵入式.强依赖(对原有的html体系不友好,走自己的一套体系)2.ejs --温和的.非侵入式的.弱依 ...

  8. Python-输入输出-input ouput

    输入.输出? 这种统称为IO流,也就是数据流向,在标准中,从终端输入称为标准输入 sidin,从终端输出为标准输出 stdout,从终端错误输出则为标准错误输出 stderr.这些只是IO流中终端方面 ...

  9. C#编写一个较完整的记事本程序

    开发环境 Visual Studio 2019 至少需安装 .NET桌面开发 创建项目并配置 创建窗体文件 配置项目名称及框架 设计界面 创建窗体文件,将控件摆放位置如下,参考系统自带的记事本程序 窗 ...

  10. 03 . Docker数据资源管理与网络

    Docker数据卷 在容器中管理数据主要有两种方式 # 数据卷(Data volumes) # 数据卷容器(Data volume containers) # 数据卷是一个可供一个或多个容器使用的特殊 ...