roles安装wordpress

debug模块

1.如何查看ansible-playbook执行过程中产生的具体信息

vim test3.yaml
---
- hosts: all
  remote_user: root
  tasks:
  - name: ls
  	shell: ls /root
  	register: var_stdout	# register:将var_stdout注册为变量
  - name: debug
  	debug:
  	  var: var_stdout		# 查看所有的输出信息
  	  #var: var_stdout["stdout"]	#只看想看的信息

wait_for模块

[root@control roles]# vim /opt/roles/db/tasks/main.yaml
# 判断数据库启动后才能建库
# 代码见/opt/roles/db/tasks/main.yaml

角色role

执行ansible的方式:

ad-hoc模式(点对点模式):使用单个模块,支持批量执行单条命令,相当于bash中的一句话shell

playbook模式(剧本):将多个任务集中在一个剧本中执行,类似于shell脚本

​ 以上两种方式的缺陷:无法实现复用性

role模式(角色):类似于多个脚本导入。roles就是通过分别将变量(vars)、文件(file)、任务(tasks)、模块(modules)及处理器(handlers)放置于单独的目录中,并可以便捷地include它们的一种机制。

1.创建角色

ansible-galaxy init 角色名字

[root@control ~]# mkdir /opt/roles
[root@control ~]# cd /opt/roles
[root@control roles]# ansible-galaxy init nginx			# 创建角色
- Role nginx was created successfully
[root@control roles]# ansible-galaxy init php			# 创建角色
- Role php was created successfully
[root@control roles]# ansible-galaxy init wordpress		 # 创建角色
- Role wordpress was created successfully
[root@control roles]# ansible-galaxy init db			 # 创建角色
- Role db was created successfully
[root@control roles]# tree nginx/
nginx/
├── README.md
├── defaults				# 默认
│   └── main.yml
├── files					# 存放需要使用的文件
├── handlers				# 处理器,触发器 需要触发的任务 
						    # tasks里面定义的notify除法handlers里面的任务
│   └── main.yml
├── meta
│   └── main.yml
├── tasks					# 写一些任务
│   └── main.yml
├── templates				# 存放需要渲染传递的文件
├── tests
│   ├── inventory
│   └── test.yml
└── vars					# 变量
    └── main.yml

8 directories, 8 files

role部署wordpress

1.角色的使用

1.1 写数据库

[root@control roles]# ls
db  nginx  php  wordpress
[root@control roles]# cd db
[root@control db]# ls
README.md  defaults  files  handlers  meta  tasks  templates  tests  vars
[root@control db]# cd tasks
[root@control tasks]# ls
main.yml
[root@control tasks]# vim main.yml
---
# tasks file for db
- name: Install MySQL
  yum:
    name:  mysql-server
    state: present


- name: Start and enable MySQL
  service:
    name: mysqld
    state: started
    enabled: yes

- name: 等待数据库启动
  wait_for:
    hosts: localhost		# 本机访问
    port: 3306
    timeout: 10				# 等待超时10秒
- name: INIT MySQL passwd
  shell: mysqladmin -p"`awk '/temporary password /{print $NF}' /var/log/mysqld.log`" password "Qq111111."


- name: Create MySQL database
  shell: mysql -p'Qq111111.' -e "CREATE DATABASE if not exists {{ MYSQL_DB_NAME }};create user '{{ MYSQL_USER }}'@'{{ MYSQL_HOST }}' identified by '{{ MYSQL_PASSWORD }}';grant all privileges on {{ MYSQL_DB_NAME }}.* to '{{ MYSQL_USER }}'@'{{ MYSQL_HOST }}';flush privileges;"

[root@control tasks]# vim ../vars/main.yml
---
# vars file for db
MYSQL_DB_NAME: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: "Qwertyuiop@123"
MYSQL_HOST: "%"

[root@control tasks]# cd /opt/roles/
[root@control roles]# ls
db  nginx  php  wordpress
[root@control roles]# vim roles.yaml
- hosts: databases
  remote_user: root
  gather_facts: no
  roles:
    - db

[root@control roles]# ansible-playbook roles.yaml --syntax-check

playbook: roles.yaml
[root@control roles]# ansible-playbook roles.yaml --list-tasks

playbook: roles.yaml

  play #1 (databases): databases        TAGS: []
    tasks:
      db : Install MySQL        TAGS: []
      db : Start and enable MySQL       TAGS: []
      db : INIT MySQL passwd    TAGS: []
      db : Create MySQL database        TAGS: [create]
[root@control roles]# ansible-playbook roles.yaml

PLAY [databases] **************************************************************************

TASK [db : Install MySQL] *****************************************************************
changed: [node1]

TASK [db : Start and enable MySQL] ********************************************************
changed: [node1]

TASK [db : INIT MySQL passwd] *************************************************************
changed: [node1]

TASK [db : Create MySQL database] *********************************************************
changed: [node1]

PLAY RECAP ********************************************************************************
node1                      : ok=4    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0



1.2 nginx

# 准备配置文件
[root@control tasks]# cp /root/nginx.conf /opt/roles/nginx/templates/
[root@control tasks]# vim /opt/roles/nginx/tasks/main.yml
---
# tasks file for nginx
#- name: Install PHP packages and epel-release
#  yum: name={{ item }} state=present disable_gpg_check=yes
 ##with_items:
 ##- epel-release
 ##- http://rpms.remirepo.net/enterprise/remi-release-9.rpm
 ##when: name == "web"
- name: Install nginx
  yum:
    name: nginx
    state: present

- name: Config Nginx
  template:
    src: nginx.conf
    dest: /etc/nginx/nginx.conf

- name: Start nginx
  service:
    name: nginx
    state: started

[root@control ~]# vim /opt/roles/nginx/templates/nginx.conf
user {{ NGINX_USER | default("nginx") }};
#{{ NGINX_USER | default(nginx) }}; 是错的,应该加""
worker_processes {{ NGINX_FORKS | default("auto") }};
。。。。。
    server {
        listen       {{ NGINX_PORT | default(80) }};
        listen       [::]:{{ NGINX_PORT | default(80) }};
        server_name  _;
        root    {{ BASE_DIR }}/wordpress;   # /usr/share/nginx/html;
	# {{ BASE_DIR | default(/usr/share/nginx) }}有问题
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
# 添加内容
        location / {
            root   {{ BASE_DIR }}/wordpress;
            index index.php;
        }
        location ~ \.php$ {
            root     {{ BASE_DIR }}/wordpress;     #/usr/share/nginx/html;  #指定网站目录
            fastcgi_pass  unix:///var/opt/remi/php80/run/php-fpm/www.sock;  #指定访问地址(>旧版为:127.0.0.1:9000)
            fastcgi_index  index.php;       #指定默认文件
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; #站点根目录
,取决于root配置项
            include        fastcgi_params;  #包含nginx常量定义
                }
        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }

# 变量
[root@control ~]# vim /opt/roles/nginx/vars/main.yml
---
# vars file for nginx
NGINX_USER: nginx
NGINX_FORKS: auto
NGINX_PORT: 80

1.3 php

[root@control ~]# vim /opt/roles/php/tasks/main.yml
---
# tasks file for php
#- name: Install PHP repository
#  yum:
#   name: http://rpms.remirepo.net/enterprise/remi-release-9.rpm
#   state: present
#   disable_gpg_check: yes

- name: Install PHP
  yum:
    name: "{{ package }}"
    state: present

- name: Start php
  service:
    name: php80-php-fpm
    state: started

# listen默认监听sock文件
- name: mode
  file:
    path: /var/opt/remi/php80/run/php-fpm/www.sock
    mode: "777"

[root@control ~]# vim /opt/roles/php/vars/main.yml
---
# vars file for php
package: php80-php-xsl,php80-php,php80-php-cli,php80-php-devel,php80-php-gd,php80-php-pdo,php80-php-mysql,php80-php-fpm

1.4 上传包

[root@control ~]# cp /opt/latest-zh_CN.zip /opt/roles/wordpress/files/

[root@control ~]# vim /opt/roles/wordpress/tasks/main.yml
---
# tasks file for wordpress
- name: Cp Wordpress to web
  unarchive:
    src: latest-zh_CN.zip
    dest: "{{ BASE_DIR }}" # 解压之后直接在BASE_DIR下产生wordpress目录
    mode: "777"


[root@control ~]# vim /etc/ansible/hosts
[databases]
node1  name="db"
[webserver]
node2  name="web"  BASE_DIR=/usr/share/nginx	# BASE_DIR定义

1.5 运行

[root@control ~]# vim /opt/roles/roles.yaml
- hosts: "{{ host }}"
  remote_user: root
  gather_facts: no
  roles:
    - "{{ role }}"
[root@control ~]# ansible-playbook -e host=databases -e role=db roles.yaml
[root@control ~]# ansible-playbook -e host=webserver -e role=php roles.yaml
[root@control ~]# ansible-playbook -e host=webserver -e role=nginx roles.yaml
[root@control ~]# ansible-playbook -e host=webserver -e role=wordpress roles.yaml

相关推荐

  1. roles安装wordpress

    2024-06-13 23:40:02       31 阅读
  2. Ubuntu22.04安装 wordpress

    2024-06-13 23:40:02       49 阅读
  3. mysql role

    2024-06-13 23:40:02       36 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-13 23:40:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-13 23:40:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-13 23:40:02       87 阅读
  4. Python语言-面向对象

    2024-06-13 23:40:02       96 阅读

热门阅读

  1. Kafka中的RPC:Server端代码流程简单概述

    2024-06-13 23:40:02       31 阅读
  2. React 事件函数传播及捕获

    2024-06-13 23:40:02       26 阅读
  3. devops自动化运维平台的核心原则有哪些?

    2024-06-13 23:40:02       35 阅读
  4. C++学习步骤

    2024-06-13 23:40:02       52 阅读
  5. hadoop-cos

    2024-06-13 23:40:02       33 阅读