ansible-playbook roles模块编写lnmp剧本

news2024/9/24 13:24:11

目录

一:集中式编写lnmp剧本

二:分布式安装lnmp

1、nginx 配置

  2、mysql配置

 3、php配置

 4、运行剧本


一:集中式编写lnmp剧本

vim /etc/ansible/lnmp.yml

- name: lnmp play
  hosts: dbservers
  remote_user: root
 
  tasks:
  - name: perpare condifure
    copy: src=/etc/yum.repos.d/nginx.repo dest=/etc/yum.repos.d/nginx.repo
  - name: install nginx
    yum: name=nginx state=latest
  - name: start nginx
    service: name=nginx state=started enabled=yes
    
  - name: install mysql
    yum: name=mysql57-community-release-el7-10.noarch.rpm state=latest
  - name: modify file
    replace:
      path: /etc/yum.repos.d/mysql-community.repo
      regexp: 'gpgcheck=1'
      replace: 'gpgcheck=0'
  - name: install mysql-community-server
    yum: name=mysql-community-server state=latest
  - name: start mysql
    service: name=mysqld state=started enabled=yes
 
  - name: add yum file
    command: 'wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm -P /etc/yum.repos.d' - name: rpm epel
    command: 'rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm'
  - name: rpm el7
    command: 'rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm'
  - name: install php
    command: 'yum install -y php72w php72w-cli php72w-common php72w-devel php72w-embedded php72w-gd php72w-mbstring php72w-pdo php72w-xml php72w-fpm php72w-mysqlnd php72w-opcache'
  - name: start php-fpm
    service: name=php-fpm state=started  enabled=yes
  - name: copy configure
    copy: src=/usr/local/nginx/conf/nginx.conf dest=/etc/nginx/conf.d/default.conf
  - name: restart nginx
    service: name=nginx state=started enabled=yes

ansible-playbook lnmp.yml  运行

 

二:分布式安装lnmp

1、nginx 配置

#创建各个服务的节点
vim /etc/ansible/hosts

[webservers]
192.168.231.102

[dbservers]
192.168.231.103

[phpservers]
192.168.231.110

#免交互
ssh-keygen -t rsa
sshpass -p '123456' ssh-copy-id  192.168.231.102 

#创建文件
mkdir /etc/ansible/roles/nginx/{files,templates,tasks,handlers,vars,defaults,meta} -p
mkdir /etc/ansible/roles/mysql/{files,templates,tasks,handlers,vars,defaults,meta} -p
mkdir /etc/ansible/roles/php/{files,templates,tasks,handlers,vars,defaults,meta} -p
 
touch /etc/ansible/roles/nginx/{defaults,vars,tasks,meta,handlers}/main.yml
touch /etc/ansible/roles/mysql/{defaults,vars,tasks,meta,handlers}/main.yml
touch /etc/ansible/roles/php/{defaults,vars,tasks,meta,handlers}/main.yml

cd /etc/ansible/roles/nginx/files
 index.php  nginx.repo
#编写php测试文件
 vim /etc/ansible/roles/nginx/files/index.php
<?php
phpinfo();
?>

#编辑nginx配置源
vim /etc/ansible/roles/nginx/files/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

 vim /etc/ansible/roles/nginx/main.yml
- include: "init.yml"
 
- name: copy nginx repo
  copy: src=nginx.repo dest=/etc/yum.repos.d/
- name: install nginx
  yum: name=nginx state=latest
- name: copy index.php
  copy: src=index.php dest=/var/www/html
- name: transmit nginx configuration
  template: src=default.conf.j2 dest=/etc/nginx/conf.d/default.conf
- name: start nginx
  service: name=nginx state=started enabled=yes

vim /etc/ansible/roles/index.php
- name: stop firewalld
  service: name=firewalld state=stopped enabled=no
- name: stop selinux
  command: 'setenforce 0'

vim /etc/ansible/roles/nginx/template/default.conf.j2
server {
    listen       80;
    server_name  localhost;
 
    #access_log  /var/log/nginx/host.access.log  main;
 
    location / {
        root   /var/www/html;
        index  index.php index.html index.htm;
    }
 
    #error_page  404              /404.html;
 
    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
 
    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           html;
        fastcgi_pass   192.168.321.110:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/html$fastcgi_script_name;
        include        fastcgi_params;
    }
 
    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}
 

  2、mysql配置

vim /etc/ansible/roles/mysql/tasks/init.yml
- name: stop firewalld
  service: name=firewalld state=stopped enabled=no
- name: stop selinux
  command: 'setenforce 0'

vim /etc/ansible/roles/mysql/main.yml
- include: "init.yml"
 
- name: remove mariadb
  shell: 'yum remove mariadb* -y'
- name: wget
  shell: 'wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm -P /etc/yum.repos.d'
- name: install mysql57-community-release-el7-10.noarch.rpm
  yum: name=epel-release
- name: sed
  replace: path=/etc/yum.repos.d/mysql-community.repo regexp="gpgcheck=1" replace="gpgcheck=0"
- name: install mysql-community-server
  yum: name=mysql-community-server
- name: start mysql
  service: name=mysqld.service state=started
- name: passd
  shell: passd=$(grep "password" /var/log/mysqld.log | awk 'NR==1 {print $NF}')
- name: mysql 1
  shell: mysql -uroot -p'passd' --connect-expired-password -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'admin@123';"
  ignore_errors: true
- name: mysql 2
  shell: mysql -uroot -padminabc@123 -e "grant all privileges on *.* to root@'%' identified by 'admin@123' with grant option;"
  ignore_errors: true

 3、php配置

vim /etc/ansible/roles/php/tasks/init.yml
- name: stop firewalld
  service: name=firewalld state=stopped enabled=no
- name: stop selinux
  command: 'setenforce 0'


vim /etc/ansible/rolesphp/tasks/main.yml
- include: "init.yml"
 
- name: install yum repo
  shell: "rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm && rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm"
  ignore_errors: true
- name: install php
  command: 'yum install -y php72w php72w-cli php72w-common php72w-devel php72w-embedded php72w-gd php72w-mbstring php72w-pdo php72w-xml php72w-fpm php72w-mysqlnd php72w-opcache'
- name: add user
  user:
    name: php
    shell: /sbin/nologin
    system: yes
- name: copy php.ini
  copy: src=php.ini dest=/etc/php.ini
- name: copy www.conf
  copy: src=www.conf dest=/etc/php-fpm.d/www.conf
- name: copy index.php
  copy: src=index.php dest=/var/www/html
- name: start php-fpm
  service: name=php-fpm state=started

 4、运行剧本

vim /etc/ansible/lnmp.yml
- name: nginx play
  hosts: webservers
  remote_user: root
  roles:
    - nginx
- name: mysql play
  hosts: dbservers
  remote_user: root
  roles:
    - mysql
 
- name: php play
  hosts: phpservers
  remote_user: root
  roles:
    - php

 

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

JSON对象

目录 简介 创建对象 ​编辑json对象作为属性值 json用于交换数据 简介 json&#xff1a;javascript object notation(js标记对象)是一种轻量化的数据交换模式&#xff0c;特点&#xff1a;体积小&#xff0c;数据量大 在js中&#xff0c;json是以对象的形式存在的&#x…

uniapp自定义消息语音

需求是后端推送的消息APP要响自定义语音&#xff0c;利用官方插件&#xff0c;总结下整体流程 uniapp后台配置 因为2.0只支持uniapp自己的后台发送消息&#xff0c;所以要自己的后台发送消息只能用1.0 插件地址和代码 插件地址: link let isIos (plus.os.name "iOS&qu…

30_万维网-The World Wide Web

前2篇我们深入讨论了电线、信号、交换机、数据包路由器以及协议&#xff0c;它们共同组成了互联网。 今天我们向上再抽象一层&#xff0c;来讨论万维网。 文章目录 1. 互联网(Internet)与万维网(World Wide Web)关系2. 万维网(World Wide Web)2.1 超链接"(hyperlinks)2.2…

【前端知识】React 基础巩固(四十三)——Effect Hook

React 基础巩固(四十三)——Effect Hook 一、Effect Hook的基本使用 Effect Hook 用来完成一些类似class中生命周期的功能。 在使用类组件时&#xff0c;不管是渲染、网路请求还是操作DOM&#xff0c;其逻辑和代码是杂糅在一起的。例如我们希望把计数器结果显示在标签上&…

CRM系统如何进行公海池线索分配自动化?

在销售过程中&#xff0c;线索分配是一个非常重要的环节。传统的线索分配方式往往是由销售主管手动进行&#xff0c;不仅效率低下&#xff0c;还存在着不公平、不灵活的问题。因此&#xff0c;许多企业通过CRM来实现公海池线索分配自动化。 1、基于规则的分配 CRM可以让用户设…

docker push 报错:unauthorized: unauthorized to access repository: library/xx处理方法

rootmaster:/home/data/harbor# sudo docker login 49.0.241.2 admin Harbor12345 1.报错原因分析 rootmaster:/home/data/harbor# docker push 49.0.241.2/library/nginx:latest #这种报错 The push refers to repository [49.0.241.2/library/nginx] Get "https://49.…

windows自动化点击大麦app抢购、捡漏,仅支持windows11操作系统

文章目录 必要条件程序运行必要条件 确保windows11版本操作系统,如果不是可以通过镜像升级为windows11如果已经是windows11操作系统,确保更新到最新版本 修改系统所在时区,将国家或地区改为美国 开启虚拟化 勾选Hyper-V,如果没有则不需要勾选 勾选虚拟机平台 勾选完毕,点…

pytest固件fixture不同层级作用域如何调用

之前的一篇文章中讲解了fixture结合conftest.py文件如何简单实现自动化。实际fixture结合conftest.py文件的使用就是一种fixture的会话层级session的实战用法。 下面开始fixture其他层级的详细讲解&#xff1a; 1、首先在使用fixture之前我们得先了解他是干什么的&#xff0c…

python_PyQt5开发验证K线视觉想法工具V1.1 _增加标记类型_线段

目录 运行情况&#xff1a; 代码&#xff1a; 承接 【python_PyQt5开发验证K线视觉想法工具V1.0】 博文 https://blog.csdn.net/m0_37967652/article/details/131966298 运行情况&#xff1a; 添加线段数据在K线图中用线段绘制出来 代码&#xff1a; 1 线段标记的数据格式…

M1中安装PD18.3.2

1.下载 在添加链接描述中直接搜索Paralles Desktop 点击右下角的免费下载(Z10 MB) 点击安装说明中的第一个选项直接下载链接 安装说明中第一条强调了必须关闭SIP 点击右上角的下载文件 下载后 2.双击解压文件 3.点击解压后的映像文件 4.点击安装&#xff0c;再次点击打开…

【嵌入式学习笔记】嵌入式基础7——认识HAL库

1.初识HAL库 1.1.CMSIS CMSIS (微控制器软件接口标准)&#xff1a;Cortex Microcontroller Software Interface Standard&#xff0c;是由ARM和与其合作的芯片厂商、软件工具厂商&#xff0c;共同制定的标准。 1.2.HAL库简介 直接操作寄存器&#xff1a;执行效率高&#x…

JVM基础篇-本地方法栈与堆

JVM基础篇-本地方法栈与堆 本地方法栈 什么是本地方法? 本地方法即那些不是由java层面实现的方法&#xff0c;而是由c/c实现交给java层面进行调用&#xff0c;这些方法在java中使用native关键字标识 public native int hashCode()本地方法栈的作用? 为本地方法提供内存空…

Vue中默认插槽,具名插槽,作用域插槽区别详解

默认插槽&#xff1a; App.vue : 在app.vue中使用MyCategory&#xff0c;里面包裹的结构是不显示的&#xff0c;要想在页面中显示&#xff0c;就需要用到插槽。在子组件MyCategory中定义 <template><div class"container"><MyCategory title"美…

号称永不限速的它抛弃初心,网盘界从此再无净土

自从百度网盘一家独大&#xff0c;带来免费用户 KB/s 级下载体验后&#xff0c;小忆一直在期待一款免费不限速网盘。 直到阿里云盘的出现可算是满足了小忆对网盘的所有期许。 新用户初始免费容量尽管只有 100G&#xff0c;但当初通过几个简单小任务就能轻松提升至数 TB。 最重…

渗透测试:Linux提权精讲(二)之sudo方法第二期

目录 写在开头 sudo expect sudo fail2ban sudo find sudo flock sudo ftp sudo gcc sudo gdb sudo git sudo gzip/gunzip sudo iftop sudo hping3 sudo java 总结与思考 写在开头 本文在上一篇博客的基础上继续讲解渗透测试的sudo提权方法。相关内容的介绍与背…

RISC-V公测平台发布 · 如何在SG2042上玩转k3s

前言 Kubernetes是一个开源的容器管理平台&#xff0c;通过Kubernetes的跨集群管理功能&#xff0c;用户可以方便地进行应用程序的复制、迁移和跨云平台的部署。 而k3s作为Kubernetes的轻量级发行版&#xff0c;相比传统的Kubernetes具有更小的二进制文件大小和更低的资源消耗…

在简历上写了“精通”后,拥有工作经验的我被面试官问到窒息

前言 如果有真才实学&#xff0c;写个精通可以让面试官眼前一亮&#xff01; 如果是瞎写&#xff1f;基本就要被狠狠地虐一把里&#xff01; 最近在面试&#xff0c;我现在十分后悔在简历上写了“精通”二字… 先给大家看看我简历上的技能列表&#xff1a; 熟悉软件测试理论基…

数据存储需求骤变,联想凌拓多招齐发推动数以致用

有人说&#xff0c;数据存储决定着未来数字化转型的深度。深以为然。 今年以来&#xff0c;千行百业的用户们愈发意识到数据存储的重要性。无论是席卷全球的生成式AI浪潮&#xff0c;需要存储“投喂”各种非结构化数据&#xff1b;还是多云环境下&#xff0c;用户们对于数据自…

ODIN_1靶机详解

ODIN_1靶机复盘 下载地址&#xff1a;https: //download.vulnhub.com/odin/odin.ova 靶场很简单&#xff0c;一会儿就打完了。 靶场说明里提醒说加一个dns解析。 我们在/etc/hosts加一条解析 就能正常打开网站了&#xff0c;要么网站打开css是乱的。 这里看到结尾就猜测肯定…