Ansible 实战

Ansible 实战

1. httpd 角色

  • 目录
root@ubuntu1904:~#tree  -f httpd/
httpd
├── httpd/default
│   └── httpd/default/main.yml
├── httpd/files
│   ├── httpd/files/httpd.conf
│   └── httpd/files/index.html
├── httpd/handlers
│   └── httpd/handlers/main.yml
├── httpd/tasks
│   ├── httpd/tasks/config.yml
│   ├── httpd/tasks/index.yml
│   ├── httpd/tasks/install.yml
│   ├── httpd/tasks/main.yml
│   ├── httpd/tasks/remove.yml
│   └── httpd/tasks/service.yml
├── httpd/templates
│   └── httpd/templates/httpd.conf.j2
└── httpd/vars└── httpd/vars/main.yml
  • 各文件内容

httpd/tasks/main.yml

---
#- include: remove.yml
- include: install.yml
- include: config.yml
- include: index.yml
- include: service.yml

httpd/tasks/install.yml

---
- name: install httpdyum: name=httpd

httpd/tasks/config.yml

---
- name: configtemplate: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf backup=yesnotify: restart

httpd/tasks/index.yml

---
- name: indexcopy: src=index.html dest=/var/www/html

httpd/tasks/service.yml

---
- name: start serviceservice: name=httpd enabled=yes state=started

httpd/handlers/main.yml

---
- name: restartservice: name=httpd state=restarted

httpd/tasks/remove.yml

# remove httpd
- hosts: websrvsremote_user: roottasks:- name: remove httpd packageyum: name=httpd state=absent- name: remove apache useruser: name=apache state=absent- name: remove data filefile: name=/etc/httpd  state=absent
...

httpd/templates/httpd.conf.j2

ServerRoot "/etc/httpd"
Listen {{ 80 }}
Include conf.modules.d/*.conf
User apache
Group apache
ServerAdmin root@localhost
<Directory />AllowOverride noneRequire all denied
</Directory>
DocumentRoot "/var/www/html"
<Directory "/var/www">AllowOverride None# Allow open access:Require all granted
</Directory>
<Directory "/var/www/html">Options Indexes FollowSymLinksAllowOverride NoneRequire all granted
</Directory>
<IfModule dir_module>DirectoryIndex index.html index.php index.htm
</IfModule>
<Files ".ht*">Require all denied
</Files>
ErrorLog "logs/error_log"
LogLevel warn<IfModule log_config_module>LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combinedLogFormat "%h %l %u %t \"%r\" %>s %b" common<IfModule logio_module># You need to enable mod_logio.c to use %I and %OLogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio</IfModule>CustomLog "logs/access_log" combined
</IfModule><IfModule alias_module>ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>
<Directory "/var/www/cgi-bin">AllowOverride NoneOptions NoneRequire all granted
</Directory><IfModule mime_module>TypesConfig /etc/mime.typesAddType application/x-compress .ZAddType application/x-gzip .gz .tgzAddType text/html .shtmlAddOutputFilter INCLUDES .shtml
</IfModule>
AddDefaultCharset UTF-8<IfModule mime_magic_module>MIMEMagicFile conf/magic
</IfModule>
#EnableMMAP off
EnableSendfile on# Load config files in the "/etc/httpd/conf.d" directory, if any.
IncludeOptional conf.d/*.conf

role_httpd.yml

root@ubuntu1904:/data/ansible_exercise#cat role_httpd.yml
---
- hosts: websrvsremote_user: rootroles:- role: httpd

2. nginx 角色

  • 目录
root@ubuntu1904:/data/ansible_exercise/roles#tree nginx/ -f
nginx
├── nginx/default
│   └── nginx/default/main.yml
├── nginx/files
│   ├── nginx/files/index.html
│   └── nginx/files/nginx.repo
├── nginx/handlers
│   └── nginx/handlers/main.yml
├── nginx/tasks
│   ├── nginx/tasks/config.yml
│   ├── nginx/tasks/file.yml
│   ├── nginx/tasks/install.yml
│   ├── nginx/tasks/main.yml
│   ├── nginx/tasks/repo.yml
│   └── nginx/tasks/service.yml
├── nginx/templates
│   ├── nginx/templates/nginx7.conf.j2
│   └── nginx/templates/nginx8.conf.j2
└── nginx/vars└── nginx/vars/main.yml
  • 各文件内容

nginx/tasks/main.yml

---
- include: repo.yml
- include: install.yml
- include: config.ymltags: config
- include: file.yml
- include: service.yml

nginx/tasks/repo.yml

---
- name: copy yum repo for nginxcopy: src=nginx.repo dest=/etc/yum.repos.d/notify: yum repolisttags: repo

nginx/tasks/install.yml

---
- name: installyum: name=nginx

nginx/tasks/config.yml

---
- name: config file for 7template: src=nginx7.conf.j2 dest=/etc/nginx/nginx.confwhen: ansible_distribution_major_version=="7"notify: restart
- name: config file for 8template: src=nginx8.conf.j2 dest=/etc/nginx/nginx.confwhen: ansible_distribution_major_version=="8"notify: restart

nginx/tasks/file.yml

---
- name: index.htmlcopy: src=index.html dest=/usr/share/nginx/html/

nginx/tasks/service.yml

---
- name: serviceservice: name=nginx state=started enabled=yes

nginx/handlers/main.yml

---
- name: yum repolistshell: yum clean all
- name: restartservice: name=nginx state=restarted

nginx/files/nginx.repo

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

nginx/files/index.html

<DOCTYPE! html><head><p1>Hello There!</p1></head><body><a>A test message!!</a></body></DOCTYPE!
>

nginx/templates/nginx7.conf.j2

user  {{ user }};
worker_processes  {{ ansible_processor_vcpus**2 }};error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;events {worker_connections  1024;
}http {include       /etc/nginx/mime.types;default_type  application/octet-stream;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  /var/log/nginx/access.log  main;sendfile        on;#tcp_nopush     on;keepalive_timeout  65;#gzip  on;include /etc/nginx/conf.d/*.conf;
}

nginx/templates/nginx8.conf.j2

user nginx;
worker_processes  {{ ansible_processor_vcpus**2 }};error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;events {worker_connections  1024;
}http {include       /etc/nginx/mime.types;default_type  application/octet-stream;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  /var/log/nginx/access.log  main;sendfile        on;#tcp_nopush     on;keepalive_timeout  65;#gzip  on;include /etc/nginx/conf.d/*.conf;
}

nginx/vars/main.yml

---
user: daemon

role_nginx.yml

root@ubuntu1904:/data/ansible_exercise#cat role_nginx.yml
---
- hosts: websrvsremote_user: rootroles:- role: nginx

3. memcached 角色

  • 目录
root@ubuntu1904:/data/ansible_exercise/roles#tree memcached/ -f
memcached
├── memcached/default
├── memcached/handlers
├── memcached/tasks
│   ├── memcached/tasks/config.yml
│   ├── memcached/tasks/install.yml
│   ├── memcached/tasks/main.yml
│   └── memcached/tasks/service.yml
├── memcached/templates
│   └── memcached/templates/memcached.j2
└── memcached/vars
  • 各文件内容

memcached/tasks/main.yml

- include: install.yml
- include: config.yml
- include: service.yml

memcached/tasks/install.yml

- name: installyum: name=memcached

memcached/tasks/config.yml

- name: config filetemplate: src=memcached.j2  dest=/etc/sysconfig/memcached

memcached/tasks/service.yml

- name: serviceservice: name=memcached state=started enabled=yes

memcached/templates/memcached.j2

PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="{{ ansible_memtotal_mb//4 }}"
OPTIONS=""

role_memcached.yml

root@ubuntu1904:/data/ansible_exercise#cat role_memcached.yml
---
- hosts: websrvsroles:- role: memcached

4. mysql 角色

  • 目录
root@ubuntu1904:/data/ansible_exercise#tree roles/mysqld/ -f
roles/mysqld
├── roles/mysqld/default
├── roles/mysqld/files
│   ├── roles/mysqld/files/my.cnf
│   ├── roles/mysqld/files/mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz
│   └── roles/mysqld/files/secure_mysql.sh
├── roles/mysqld/handlers
│   └── roles/mysqld/handlers/main.yml
├── roles/mysqld/tasks
│   ├── roles/mysqld/tasks/config.yml
│   ├── roles/mysqld/tasks/data.yml
│   ├── roles/mysqld/tasks/group.yml
│   ├── roles/mysqld/tasks/install.yml
│   ├── roles/mysqld/tasks/link.yml
│   ├── roles/mysqld/tasks/main.yml
│   ├── roles/mysqld/tasks/path.yml
│   ├── roles/mysqld/tasks/remove_mysql.yml
│   ├── roles/mysqld/tasks/secure.yml
│   ├── roles/mysqld/tasks/service.yml
│   ├── roles/mysqld/tasks/unarchive.yml
│   └── roles/mysqld/tasks/user.yml
├── roles/mysqld/templates
└── roles/mysqld/vars└── roles/mysqld/vars/mysql_vars.yml
  • 各文件内容

roles/mysqld/tasks/main.yml

---
- include: install.yml
- include: group.yml
- include: user.yml
- include: unarchive.yml
- include: link.yml
- include: data.yml
- include: config.yml
- include: service.yml
- include: path.yml
- include: secure.yml

roles/mysqld/tasks/config.yml

- name: config my.cnfcopy: src=my.cnf  dest=/etc/my.cnf

roles/mysqld/tasks/data.yml

- name: data dirshell: chdir=/usr/local/mysql/  ./scripts/mysql_install_db --datadir=/data/mysql --user=mysql

roles/mysqld/tasks/group.yml

- name: create mysql groupgroup: name=mysql gid=666

roles/mysqld/tasks/install.yml

- name: install deps libsyum: name=libaio,perl-Data-Dumper,perl-Getopt-Long

roles/mysqld/tasks/link.yml

- name: mkdir /usr/local/mysqlfile: src=/usr/local/mysql-5.6.46-linux-glibc2.12-x86_64 dest=/usr/local/mysql state=link

roles/mysqld/tasks/path.yml

- name: PATH variablecopy: content='PATH=/usr/local/mysql/bin:$PATH' dest=/etc/profile.d/mysql.sh

roles/mysqld/tasks/remove_mysql.yml


roles/mysqld/tasks/secure.yml

- name: secure scriptscript: secure_mysql.sh

roles/mysqld/tasks/service.yml

- name: service scriptshell: /bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld;/etc/init.d/mysqld start;chkconfig --add mysqld;chkconfig mysqld on

roles/mysqld/tasks/unarchive.yml

- name: copy tar to remote host and file modeunarchive: src=mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz dest=/usr/local/ owner=root group=root

roles/mysqld/tasks/user.yml

- name: create mysql useruser: name=mysql uid=667 group=mysql shell=/sbin/nologin system=yes create_home=no home=/data/mysql

roles/mysqld/handlers/main.yml

- name: restartshell: /etc/init.d/mysqld restart

roles/mysqld/files/my.cnf

[mysqld]
log-bin
socket=/data/mysql/mysql.sock
user=mysql
symbolic-links=0
datadir=/data/mysql
innodb_file_per_table=1[client]
port=3306
socket=/data/mysql/mysql.sock[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/data/mysql/mysql.pid

roles/mysqld/files/secure_mysql.sh

#!/bin/bash
/usr/local/mysql/bin/mysql_secure_installation <<EOFy
stevenux
stevenux
y
y
y
y
EOF

role_mysqld.yml

root@ubuntu1904:/data/ansible_exercise#cat role_mysqld.yml
---
- hosts: websrvsremote_user: rootroles:- role: mysqldtags: ["mysql", "db"]

5. PXC 角色

  • 目录

  • 配置文件可在我的 github 得到

    Github-ansible_exercise

  • github 中目录如下

root@ubuntu1904:/data/ansible_exercise#tree
.
├── common_scripts
│   ├── loop.sh
│   ├── show_dir.sh
│   └── systeminfo.sh
├── README.rst
├── role_httpd.yml
├── role_memcached.yml
├── role_mysqld.retry
├── role_mysqld.yml
├── role_nginx.retry
├── role_nginx.yml
├── role_pxc.yml
└── roles├── httpd│   ├── default│   │   └── main.yml│   ├── files│   │   ├── httpd.conf│   │   └── index.html│   ├── handlers│   │   └── main.yml│   ├── tasks│   │   ├── config.yml│   │   ├── index.yml│   │   ├── install.yml│   │   ├── main.yml│   │   ├── remove.yml│   │   └── service.yml│   ├── templates│   │   └── httpd.conf.j2│   └── vars├── memcached│   ├── default│   ├── handlers│   ├── tasks│   │   ├── config.yml│   │   ├── install.yml│   │   ├── main.yml│   │   └── service.yml│   ├── templates│   │   └── memcached.j2│   └── vars├── mysqld│   ├── default│   ├── files│   │   ├── my.cnf│   │   ├── mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz│   │   └── secure_mysql.sh│   ├── handlers│   │   └── main.yml│   ├── tasks│   │   ├── config.yml│   │   ├── data.yml│   │   ├── group.yml│   │   ├── install.yml│   │   ├── link.yml│   │   ├── main.yml│   │   ├── path.yml│   │   ├── remove_mysql.yml│   │   ├── secure.yml│   │   ├── service.yml│   │   ├── unarchive.yml│   │   └── user.yml│   ├── templates│   └── vars│       └── mysql_vars.yml├── nginx│   ├── default│   │   └── main.yml│   ├── files│   │   ├── index.html│   │   └── nginx.repo│   ├── handlers│   │   └── main.yml│   ├── tasks│   │   ├── config.yml│   │   ├── file.yml│   │   ├── install.yml│   │   ├── main.yml│   │   ├── repo.yml│   │   └── service.yml│   ├── templates│   │   ├── nginx7.conf.j2│   │   └── nginx8.conf.j2│   └── vars│       └── main.yml├── pxc│   ├── default│   │   └── main.yml│   ├── files│   │   ├── percona.repo│   │   └── wsrep.cnf│   ├── handlers│   │   └── main.yml│   ├── tasks│   │   ├── install_pxc.retry│   │   ├── install_pxc.yml│   │   └── main.yml│   ├── templates│   └── vars│       └── main.yml└── self_report├── self_report.j2├── self_report.retry└── self_report.yml

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

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

相关文章

【数据结构】 单链表面试题讲解

文章目录 引言反转单链表题目描述示例&#xff1a;题解思路代码实现&#xff1a; 移除链表元素题目描述&#xff1a;示例思路解析&#xff1a; 链表的中间结点题目描述&#xff1a;示例&#xff1a;思路解析代码实现如下&#xff1a; 链表中倒数第k个结点题目描述示例思路解析&…

自动拉取 GitHub 仓库更新的脚本

更好的阅读体验 \huge{\color{red}{更好的阅读体验}} 更好的阅读体验 由于将 HAUE-CS-WIKI 部署到了我自己的服务器上作为国内镜像站&#xff0c;每次在源站更新后都需要手动拉取镜像站的更新实在是太麻烦了&#xff0c;因此产生了编写该脚本的需求&#xff08; 读者可根据该…

腾讯大佬用了8小时讲完的Python,整整315集,拿走不谢!

Python在近几年越来越受追捧&#xff0c;很多童鞋或者职场小伙伴想要提升技能-学习Python。 这是非常好的事情&#xff0c;但问题在于很多人不知道学Python做什么&#xff0c;所以什么零碎细末、艰难晦涩、长篇大论的都去看&#xff0c;很容易陷入学不下去的困境。必须要有针对…

【docker练习】

1.安装docker服务&#xff0c;配置镜像加速器 看这篇文章https://blog.csdn.net/HealerCCX/article/details/132342679?spm1001.2014.3001.5501 2.下载系统镜像&#xff08;Ubuntu、 centos&#xff09; [rootnode1 ~]# docker pull centos [rootnode1 ~]# docker pull ubu…

【全面概览 stable diffusion】:从安装到完整使用指南

在CSDN上&#xff0c;我写了一系列关于 stable diffusion 的专栏文章&#xff0c;涵盖了从安装到使用的完整指南。在这个专栏中&#xff0c;我详细介绍了stable diffusion的基本概念、使用方法以及相关的插件和模型。 以下是该专栏的总结&#xff0c;其中包含了相关文章的链接…

前端HTML进阶

day02&#xff1a;列表、表格、表单 目标&#xff1a;掌握嵌套关系标签的写法&#xff0c;使用列表标签布局网页 01-列表 作用&#xff1a;布局内容排列整齐的区域。 列表分类&#xff1a;无序列表、有序列表、定义列表。 无序列表 作用&#xff1a;布局排列整齐的不需要规…

小程序-uni-app:hbuildx uni-app 安装 uni-icons 及使用

一、官方文档找到uni-icons uni-app官网 二、下载插件 三、点击“打开HBuildX” 四、选择要安装的项目 五、勾选要安装的插件 六、安装后&#xff0c;项目插件目录 根目录uni_modules目录下增加uni-icons、uni-scss 七、引入组件&#xff0c;使用组件 <uni-icons type&qu…

kubernetes — 常用命令介绍

目录 一、基础操作 1、简化别名设置 2、查看kubernetes的资源对象 3、创建、查看namespace 4、Pod 增删改查 二、YAML文件创建Pod 1、标签部分作用 1.生成yaml文件 2.修改yaml中的标签名 2、imagePullPolicy 1.增加镜像拉取策略 2.yaml批量创建pod 3.通过label查询…

十、Linux的root用户、用户和用户组的问题

目录 1、Linux的root用户 &#xff08;1&#xff09;基础 &#xff08;2&#xff09;如何进入root模式 &#xff08;3&#xff09;如何给普通用户配置root权限&#xff1f; 注意点&#xff1a; 配置方法&#xff1a; 2、用户/用户组问题 &#xff08;1&#xff09;用户/用…

pdf怎么合并在一起?这几个合并方法了解一下

pdf怎么合并在一起&#xff1f;在日常工作、学习和生活中&#xff0c;我们常常会遇到需要将多个PDF文件合并成一个文件的情况。比如&#xff0c;在学术论文写作中&#xff0c;我们可能需要将多篇论文合并成一个文件进行打印和提交。在工作中&#xff0c;我们可能需要将多个报告…

MacOS 安装Redis并设置密码

在开发过程中&#xff0c;需要本地进行安装Redis进行测试&#xff0c;记录了下MacOS环境下安装Redis&#xff0c;以及设置密码。 Brew 安装 $ brew install redis启动服务 # 启动服务 brew services start redis # 关闭服务 brew services stop redis # 重启服务 brew servic…

深入竞品:解读竞品分析的艺术与策略

引言&#xff1a;为何竞品分析至关重要&#xff1f; 在当今的产品环境中&#xff0c;市场变得越来越拥挤。每个角落都有新的创业公司试图创造下一个行业的颠覆者&#xff0c;同时也有成熟的巨头在不断地迭代和优化他们的产品。在这样的环境中&#xff0c;不了解您的竞争对手是…

03架构管理之测试管理

专栏说明&#xff1a;针对于企业的架构管理岗位&#xff0c;分享架构管理岗位的职责&#xff0c;工作内容&#xff0c;指导架构师如何完成架构管理工作&#xff0c;完成架构师到架构管理者的转变。计划以10篇博客阐述清楚架构管理工作&#xff0c;专栏名称&#xff1a;架构管理…

学习笔记十七:node节点选择器,亲和性

node节点选择器&#xff0c;污点、容忍度、亲和性 node节点选择器nodeName&#xff0c;指定pod节点运行在哪个具体node上nodeSelector&#xff1a;指定pod调度到具有哪些标签的node节点上 亲和性node节点亲和性使用requiredDuringSchedulingIgnoredDuringExecution硬亲和性使用…

Vue_3:声明周期钩子(组件化开发)

Vue_03_note 文章目录 Vue_03_note01-生命周期 和 **生命周期的四个阶段**什么是生命周期&#xff1f;生命周期的四个阶段 02-Vue 声明周期函数&#xff08;钩子函数&#xff09;什么是钩子函数八大钩子&#xff08;四对&#xff09;代码示例&#xff1a;八个钩子函数 03-生命周…

Intellij中直接运行ts配置:run configuration for typescript

在Intellij中可以借助插件run configuration for typescript直接运行typescript&#xff1a; run configuration for typescript插件本质还是依赖于 ts-node 来运行&#xff0c;只是其可以帮助我们自动配置好 ts-node 运行参数&#xff0c;简化使用。 第一步&#xff1a;安装…

Linux Mint 21.3 计划于 2023 年圣诞节发布

导读Linux Mint 项目近日公布了基于 Ubuntu 的 Linux Mint 发行版下一个重要版本的一些初步细节&#xff0c;以及备受期待的基于 Debian 的 LMDE 6&#xff08;Linux Mint Debian Edition&#xff09;版本。 近日&#xff0c;Linux Mint 项目负责人克莱门特-勒菲弗&#xff08;…

【STM32 学习】电源解析(VCC、VDD、VREF+、VBAT)

VCC电源电压GND电源供电负电压&#xff08;通常接地&#xff09;VDD模块工作正电压VSS模块工作负电压VREFADC参考正电压VREF-ADC参考负电压VBAT电池或其他电源供电VDDA模拟供电正电压VSSA模拟供电负电压 一、VCC&#xff08;供电电压&#xff09; VCC是指芯片的电源电压&#…

cuda、cuDNN、深度学习框架、pytorch、tentsorflow、keras这些概念之间的关系

当讨论CUDA、cuDNN、深度学习框架、pytorch、tensorflow、keras这些概念的时候&#xff0c;我们讨论的是与GPU加速深度学习相关的技术和工具。 CUDA&#xff08;Compute Unified Device Architecture&#xff09;&#xff1a; CUDA是由NVIDIA开发的一种并行计算平台和编程模型&…

快解析内网穿透便捷访问内网私有云

快解析内网穿透软件的首要优势在于其不改变企业现有IT架构的特点。传统的内网穿透解决方案常常需要对企业网络进行重构&#xff0c;这不仅增加了工作量&#xff0c;还可能带来不稳定的因素。而快解析则巧妙地绕过了这一问题&#xff0c;让您能够在保持原有网络设备和配置的前提…