环境准备
安装ansible
[root@ansible ~]# yum install -y epel-release [root@ansible ~]# yum install -y ansible
配置ansible
配置免密登录
[root@ansible ~]# ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Created directory '/root/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: SHA256:yoEbuPUA2JXWy6Sv/0XmVJFa2+kPDoeUBOHXlEuuX/M root@master The key's randomart image is: +---[RSA 2048]----+ | .o oo.... | | o .o o . ++o | |. o. + . .+o*.o | | o..o .o+ = | | . =.. S +. + | | o *.o = + +..| | . ..+ o = +o| | . . o E| | .... | +----[SHA256]-----+ [root@ansible ~]# ssh-copy-id 192.168.75.16 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" The authenticity of host '192.168.75.16 (192.168.75.16)' can't be established. ECDSA key fingerprint is SHA256:zEsox3y0l5PHw5nPJwEx/6fDmULkhd0ad82TQQ/XkFU. ECDSA key fingerprint is MD5:92:5b:3a:24:6f:0f:09:33:35:78:4c:de:93:74:bd:9b. Are you sure you want to continue connecting (yes/no)? yes /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@192.168.75.16's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh '192.168.75.16'" and check to make sure that only the key(s) you wanted were added. [root@ansible ~]# ssh-copy-id 192.168.75.17 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" The authenticity of host '192.168.75.17 (192.168.75.17)' can't be established. ECDSA key fingerprint is SHA256:YP7fswNmzmBhCbu33ZfuJ1G7e+Cyi8dy+o48oWtmRqU. ECDSA key fingerprint is MD5:b6:c3:2e:95:2f:c4:f5:1f:7c:e0:e7:ca:ca:4b:b7:c8. Are you sure you want to continue connecting (yes/no)? yes /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@192.168.75.17's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh '192.168.75.17'" and check to make sure that only the key(s) you wanted were added.
配置ansible文件
[root@ansible ~]# vim /etc/ansible/hosts # 在结尾添加如下内容 [node1] 192.168.75.16 [node2] 192.168.75.17
搭建wordpress
创建相关目录
[root@ansible ~]# mkdir -pv /etc/ansible/lamp/roles/{prepare,httpd,mysql,php}/{tasks,files,templates,vars,han dlers}
ansible服务器配置
准备相关服务的配置文件、以及wordpress源码包
[root@ansible ~]# yum install -y httpd mariadb-server php php-mysql # 搭建lamp环 境 [root@ansible ~]# wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz # 下载wordpress
修改httpd文件
[root@ansible ~]# vim /etc/httpd/conf/httpd.conf # 找到如下内容,修改index.html为 index.php <IfModule dir_module> DirectoryIndex index.html </IfModule>
拷贝对应的文件
[root@ansible ~]# cp /etc/httpd/conf/httpd.conf /etc/ansible/lamp/roles/httpd/files/ [root@ansible ~]# cp wordpress-4.9.4-zh_CN.tar.gz /etc/ansible/lamp/roles/httpd/files/
编写环境剧本
[root@ansible ~]# vim /etc/ansible/lamp/roles/prepare/tasks/main.yaml - name: install wget yum: name=wget state=present - name: rm -rf *.repo shell: rm -rf /etc/yum.repos.d/* - name: wget huawei.repo shell: wget -O /etc/yum.repos.d/CentOS-Base.repo https://repo.huaweicloud.com/repository/conf/CentOS-7-reg.repo - name: updaet yum shell: yum repolist - name: stop firewalld service: name=firewalld state=stopped enabled=no - name: stop iptables shell: iptables -F
编写httpd剧本
[root@ansible ~]# vim /etc/ansible/lamp/roles/httpd/tasks/main.yaml - name: install httpd yum: name=httpd state=present - name: copy wordpress copy: src=wordpress-4.9.4-zh_CN.tar.gz dest=/opt - name: copy httpd.conf copy: src=httpd.conf dest=/etc/httpd/conf/httpd.conf notify: - restart httpd - name: tar wordperss shell: tar -xf /opt/wordpress-4.9.4-zh_CN.tar.gz -C /var/www/html/ - name: start httpd service: name=httpd state=started enabled=yes # 编写触发条件 [root@ansible ~]# vim /etc/ansible/lamp/roles/httpd/handlers/main.yaml - name: restart httpd service: name=httpd state=restarted
编写mysql剧本
[root@ansible ~]# vim /etc/ansible/lamp/roles/mysql/tasks/main.yaml - name: install mysql yum: name=mariadb-server state=present - name: start mariadb service: name=mariadb state=started enabled=yes - name: create database wordpress shell: mysql -e "create database wordpress;" - name: cretae wordpress user shell: mysql -e "grant all privileges on wordpress.* to 'wordpress'@'localhost' identified by '123456';"
构建PHP剧本
[root@ansible ~]# vim /etc/ansible/lamp/roles/php/tasks/main.yaml - name: install php yum: name=php state=present - name: install php-mysql yum: name=php-mysql state=present
构建总剧本
[root@ansible ~]# vim /etc/ansible/lamp/roles/all.yaml - hosts: all remote_user: root roles: - prepare - httpd - mysql - php
查看目录结构
[root@ansible ~]# yum install tree -y [root@ansible ~]# cd /etc/ansible/lamp/roles/ [root@ansible roles]# tree . . ├── all.yaml ├── httpd │ ├── files │ │ ├── httpd.conf │ │ └── wordpress-4.9.4-zh_CN.tar.gz │ ├── handlers │ │ └── main.yaml │ ├── tasks │ │ └── main.yaml │ ├── templates │ └── vars ├── mysql │ ├── files │ ├── handlers │ ├── tasks │ │ └── main.yaml │ ├── templates │ └── vars ├── php │ ├── files │ ├── handlers │ ├── tasks │ │ └── main.yaml │ ├── templates │ └── vars └── prepare ├── files ├── handlers ├── tasks │ └── main.yaml ├── templates └── vars 24 directories, 8 files
执行总剧本
[root@ansible roles]# ansible-playbook /etc/ansible/lamp/roles/all.yaml
出现上面的图片,证明执行完成。
浏览器访问测试
连接数据库
写入文件
# 在IP地址的服务器按照要求完成,wp-config.php文件使用下面的命令创建 [root@node1 ~]# vim /var/www/html/wordpress/wp-config.php <?php /** * WordPress基础配置文件。 * * 这个文件被安装程序用于自动生成wp-config.php配置文件, * 您可以不使用网站,您需要手动复制这个文件, * 并重命名为“wp-config.php”,然后填入相关信息。 * * 本文件包含以下配置选项: * * * MySQL设置 * * 密钥 * * 数据库表名前缀 * * ABSPATH * * @link https://codex.wordpress.org/zh-cn:%E7%BC%96%E8%BE%91_wp-config.php * * @package WordPress */ // ** MySQL 设置 - 具体信息来自您正在使用的主机 ** // /** WordPress数据库的名称 */ define('DB_NAME', 'wordpress'); /** MySQL数据库用户名 */ define('DB_USER', 'wordpress'); /** MySQL数据库密码 */ define('DB_PASSWORD', '123456'); /** MySQL主机 */ define('DB_HOST', 'localhost'); /** 创建数据表时默认的文字编码 */ define('DB_CHARSET', 'utf8mb4'); /** 数据库整理类型。如不确定请勿更改 */ define('DB_COLLATE', ''); /**#@+ * 身份认证密钥与盐。 * * 修改为任意独一无二的字串! * 或者直接访问{@link https://api.wordpress.org/secret-key/1.1/salt/ * WordPress.org密钥生成服务} * 任何修改都会导致所有cookies失效,所有用户将必须重新登录。 * * @since 2.6.0 */ define('AUTH_KEY', 'BQwZw%P{]*H=fEz_f-@;)/D|F9H!+&fy`sAe~0fx<=]XU]z7.5IToi54Pvs#ZvQ'); define('SECURE_AUTH_KEY', 'xF%$T`0gGL>>s%<jnZGn7F+YA%D= <V5~F,_;Lw//:z+>QCES0mX9ni0;eF8=Tw;g'); define('LOGGED_IN_KEY', 'h*Mm+<cqUY0emZJ+%4L6Rx.VVvz}$Pym8*YcN:lk7qF|j(,_%1 L=#=>^? [?4v%'); define('NONCE_KEY', '_ vKAo*e/2Tbo(#YwoPq>+yMEfh@9aWE)x :v`,.B[b!F@oL0q<6@IrX7HYraX$6'); define('AUTH_SALT', 'yQp+Xyl^xp;0h2 RzxB@# Nk27Cp6*DMXobs|=Tl`5?- hd$soAw*7*$,-feYkRHD'); define('SECURE_AUTH_SALT', 'OuExS2o9eUdud0kOl!)j1,0:[/UydLK*35n@y(,l . <qb+OC>-/[*}ct:H,x.ueP'); define('LOGGED_IN_SALT', 'EpzF1tv;-UWQtbT}tElaeD<o&LIdaKSo3I2p$JfuMw!Z(npxmY? +6l##BZNw~JBw'); define('NONCE_SALT', 'Ef3)$7ye.+!)b&7bn{~n+i!+XQ|iUKfVJuf)mO>F+uN4*{- P<N&UOmau|v:]U Qa'); /**#@-*/ /** * WordPress数据表前缀。 * * 如果您有在同一数据库内安装多个WordPress的需求,请为每个WordPress设置 * 不同的数据表前缀。前缀名只能为数字、字母加下划线。 */ $table_prefix = 'wp_'; /** * 开发者专用:WordPress调试模式。 * * 将这个值改为true,WordPress将显示所有用于开发的提示。 * 强烈建议插件开发者在开发环境中启用WP_DEBUG。 * * 要获取其他能用于调试的信息,请访问Codex。 * * @link https://codex.wordpress.org/Debugging_in_WordPress */ define('WP_DEBUG', false); /** * zh_CN本地化设置:启用ICP备案号显示 * * 可在设置→常规中修改。 * 如需禁用,请移除或注释掉本行。 */ define('WP_ZH_CN_ICP_NUM', true); /* 好了!请不要再继续编辑。请保存本文件。使用愉快! */ /** WordPress目录的绝对路径。 */ if ( !defined('ABSPATH') ) define('ABSPATH', dirname(__FILE__) . '/'); /** 设置WordPress变量和包含文件。 */ require_once(ABSPATH . 'wp-settings.php');
填写信息安装完成
登录wordpress搭建完成
查看数据是否写入数据库
[root@node1 ~]# mysql -uwordpress -p123456 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 40 Server version: 5.5.68-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> use wordpress; # 进入数据库 Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed MariaDB [wordpress]> show tables; # 查看表 +-----------------------+ | Tables_in_wordpress | +-----------------------+ | wp_commentmeta | | wp_comments | | wp_links | | wp_options | | wp_postmeta | | wp_posts | | wp_term_relationships | | wp_term_taxonomy | | wp_termmeta | | wp_terms | | wp_usermeta | | wp_users | +-----------------------+ 12 rows in set (0.00 sec) MariaDB [wordpress]> select * from wp_users; # 查看是否与上方填写的w +----+------------+------------------------------------+---------------+--------- ----------+----------+---------------------+---------------------+-------------+- -------------+ | ID | user_login | user_pass | user_nicename | user_email | user_url | user_registered | user_activation_key | user_status | display_name | +----+------------+------------------------------------+---------------+--------- ----------+----------+---------------------+---------------------+-------------+- -------------+ | 1 | root | $P$BvWh7wNToYPOS11grHdFg1vC/Tersu. | root | 2924739965@qq.com | | 2022-11-17 05:33:07 | | 0 | root | +----+------------+------------------------------------+---------------+--------- ----------+----------+---------------------+---------------------+-------------+- -------------+ 1 row in set (0.00 sec)