【ansible】通过role角色部署lnmp架构

目录

1.部署nginx

2.部署MySQL

3.部署php

4.编写测试文件

[5.Roles 模块](#5.Roles 模块)

[5.1 roles 内各目录含义解释](#5.1 roles 内各目录含义解释)


1.部署nginx

复制代码
cd /opt
mkdir nginx
cd nginx/
上传nginx.repo、nginx.conf,并且修改nginx.conf为nginx.conf.j2
vim nginx.conf.j2
37、38行
listen       {{nginx_addr}}:{{nginx_port}};
         server_name  {{nginx_hostname}};
45行
root   {{root_dir}};
68行
fastcgi_pass   {{php_addr}}:{{php_port}};
70行
fastcgi_param  SCRIPT_FILENAME  {{root_dir}}$fastcgi_script_name;

vim lnmp-playbook.yaml
- name: nginx play
  hosts: webservers
  remote_user: root
  gather_facts: false
  vars:
  - nginx_addr: 192.168.9.113
  - nginx_port: 80
  - nginx_hostname: www.xy101.com
  - root_dir: /var/www/html
  - php_addr: 192.168.9.115
  - php_port: 9000
  tasks:
  - name: disable firewalld
    service: name=firewalld state=stopped enabled=no
  - name: disable selinux
    command: 'setenfoce 0'
    ignore_errors: true
  - name: copy nginx repo
    copy: src=/opt/nginx/nginx.repo dest=/etc/yum.repos.d/
  - name: install nginx
    yum: name=nginx state=latest
  - name: create root dir
    file: path={{root_dir}} state=directory
  - name: copy nginx config template file
    template: src=/opt/nginx/nginx.conf.j2 dest=/etc/nginx/nginx.conf
    notify: 'reload nginx'
  - name: create nfs config
    copy: content="{{root_dir}} 192.168.9.0/24(rw,sync,no_root_squash)" dest=/etc/exports
  - name: restart rpcbind,nfs,nginx
    service: name={{item}} state=restarted enabled=yes
    with_items:
    - rpcbind
    - nfs
    - nginx

  handlers:
  - name: reload nginx
    service: name=nginx state=reloaded

ansible-playbook lnmp-playbook.yaml

2.部署MySQL

复制代码
- name: mysql play
  hosts: dbservers
  remote_user: root
  gather_facts: false
  tasks:
  - name: disable mysql_server firewalld
    service: name=firewalld  state=stopped  enabled=no
  - name: disable mysql_server selinux
    command: 'setenforce 0'
    ignore_errors: true
  - name: remove mariadb
    yum: name=mariadb*  state=absent
  - name: copy mysql repo
    copy: src=/opt/mysql/mysql-community.repo  dest=/etc/yum.repos.d/
  - name: modify mysql repo
    replace: path=/etc/yum.repos.d/mysql-community.repo  regexp="gpgcheck=1"  replace="gpgcheck=0"
  - name: install mysql
    yum: name=mysql-server state=present
  - name: start mysql
    service: name=mysqld  state=started  enabled=yes
  - name: init mysql
    script: '/opt/mysql/mysql-init.sh'

3.部署php

复制代码
- name: php play
  hosts: phpservers
  remote_user: root
  gather_facts: false
  vars:
  - php_username: nginx
  - php_addr: 192.168.80.12:9000
  - nginx_addr: 192.168.80.10
  - root_dir: /var/www/html
  tasks:
  - name: disable php_server firewalld
    service: name=firewalld  state=stopped  enabled=no
  - name: disable php_server selinux
    command: 'setenforce 0'
  - name: unarchive php tar pkg
    unarchive: copy=yes  src=/opt/php/php.tar.gz  dest=/mnt/
  - name: copy local repo
    copy: src=/opt/php/local.repo  dest=/etc/yum.repos.d/
  - name: create repo
    shell: 'createrepo /mnt && yum clean all && yum makecache'
  - name: install php
    yum: name=php72w,php72w-cli,php72w-common,php72w-devel,php72w-embedded,php72w-gd,php72w-mbstring,php72w-pdo,php72w-xml,php72w-fpm,php72w-mysqlnd,php72w-opcache,php72w-ldap,php72w-bcmath  state=present
  - name: create php user
    user: name={{php_username}}  shell=/sbin/nologin  create_home=no
  - name: modify php.ini
    replace: path=/etc/php.ini  regexp=";date.timezone ="  replace="date.timezone = Asia/Shanghai"
  - name: modify user and group in www.conf
    replace: path=/etc/php-fpm.d/www.conf  regexp="apache"  replace="{{php_username}}"
    notify: "reload php-fpm"
  - name: modify listen in www.conf
    replace: path=/etc/php-fpm.d/www.conf  regexp="127.0.0.1:9000"  replace="{{php_addr}}"
    notify: "reload php-fpm"
  - name: modify listen.allowed_clients in www.conf
    replace: path=/etc/php-fpm.d/www.conf  regexp="127.0.0.1"  replace="{{nginx_addr}}"
    notify: "reload php-fpm"
  - name: start php-fpm
    service: name=php-fpm  state=started  enabled=yes
  - name: create www root dir
    file: path={{root_dir}}  state=directory
  - name: mount nfs
    mount: src="{{nginx_addr}}:{{root_dir}}"  path={{root_dir}}  fstype=nfs  state=mounted  opts="defaults,_netdev"
  handlers:
  - name: reload php-fpm
    service: name=php-fpm  state=reloaded

4.编写测试文件

复制代码
cd /var/www/html
vim index.php

浏览器访问测试

5.Roles 模块

复制代码
roles用于层次性、结构化地组织playbook。roles能够根据层次型结构自动装载变量文件、tasks以及handlers等。要使用roles只需要在playbook中使用include指令引入即可。
简单来讲,roles就是通过分别将变量、文件、任务、模板及处理器放置于单独的目录中,并可以便捷的include它们的一种机制。roles一般用于基于主机构建服务的场景中,但也可以是用于构建守护进程等场景中。主要使用场景代码复用度较高的情况下。

5.1 roles 内各目录含义解释

●files
用来存放由 copy 模块或 script 模块调用的文件。

●templates
用来存放 jinjia2 模板,template 模块会自动在此目录中寻找 jinjia2 模板文件。

●tasks
此目录应当包含一个 main.yml 文件,用于定义此角色的任务列表,此文件可以使用 include 包含其它的位于此目录的 task 文件。

●handlers
此目录应当包含一个 main.yml 文件,用于定义此角色中触发条件时执行的动作。

●vars
此目录应当包含一个 main.yml 文件,用于定义此角色用到的变量。

●defaults
此目录应当包含一个 main.yml 文件,用于为当前角色设定默认变量。 这些变量具有所有可用变量中最低的优先级,并且可以很容易地被任何其他变量覆盖。所以生产中我们一般不在这里定义变量

●meta
此目录应当包含一个 main.yml 文件,用于定义此角色的元数据信息及其依赖关系。

相关推荐
明航咨询_贾老师1 天前
RHCA Ansible高级自动化(DO374)认证信息整理
运维·自动化·ansible·rhca·红帽认证
有毒的教程5 天前
Ansible批量操作自动化完整教程:批量部署服务、配置同步、软件更新实战
运维·自动化·ansible
Hy行者勇哥10 天前
用Cursor智能编写Ansible/Terraform脚本,打通CI/CD链路
ci/cd·ansible·terraform
芷栀夏12 天前
飞牛NAS怎么部署Immich?家庭照片自动备份与远程访问教程
服务器·nginx·ansible
悠然南风23 天前
Ansible常见模块总结及LDAP Role 编写与调试
ansible
祺风挽楠1 个月前
ansible编辑
网络·ansible
芳心粽伙饭1 个月前
Ansible课后作业
ansible
烁3471 个月前
Ansible初识
ansible
烁3471 个月前
Ansible安装部署调试
ansible
烁3471 个月前
Ansible命令
ansible