配置主机
[root@server ~]# cd /etc/ansible/
[root@server ansible]# ls
ansible.cfg hosts roles
[root@server ansible]# vim hosts
[dev]
node1
创建角色
[root@server ansible]# cd roles/
[root@server roles]# ls
[root@server roles]# ansible-galaxy init apache
- Role apache was created successfully
[root@server roles]# ansible-galaxy init mysql
- Role mysql was created successfully
[root@server roles]# ansible-galaxy init php
- Role php was created successfully
[root@server roles]# ls
apache mysql php
通过apache角色配置httpd服务
先编写tasks任务
[root@server roles]#
[root@server roles]# vim apache/tasks/main.yml
[root@server roles]# cat apache/tasks/main.yml
---
# tasks file for apache
- name: set firewalld
service:
name: firewalld
state: stopped
enabled: no
- name: stop selinux
replace:
path: /etc/sysconfig/selinux
regexp: '^SELINUX='
replace: 'SELINUX=disabled'
- name: setenforce 0
shell: setenforce 0
- name: yum.sh
script: yum.sh
- name: install pkgs
yum:
name: "{{ httpdpkgs }}"
state: present
- name: uzip pkgs
unarchive:
src: apr-1.6.5.tar.bz2
dest: /opt/
- name: uzip pkgs1
unarchive:
src: apr-util-1.6.1.tar.bz2
dest: /opt/
- name: uzip pkgs2
unarchive:
src: httpd-2.4.54.tar.bz2
dest: /opt/
- name: create user
user:
name: apache
system: yes
shell: /sbin/nologin
create_home: no
state: present
- name: make install
script: httpd.sh
- name: bianliang
script: bianliang.sh
- name: set service
template:
src: httpd.service.j2
dest: /usr/lib/systemd/system/httpd.service
- name: daemon-reload
shell: systemctl daemon-reload
- name: restart httpd
service:
name: httpd
state: started
enabled: yes
yum源脚本
[root@server roles]# cd apache/
[root@server apache]# vim files/yum.sh
[root@server apache]# cat files/yum.sh
#/bin/bash
rm -rf /etc/yum.repos.d/*
/usr/bin/curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
/usr/bin/sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
/usr/bin/sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
编译脚本
[root@server apache]# vim files/httpd.sh
[root@server apache]# cat files/httpd.sh
#/bin/bash
cd /opt/apr-1.6.5
sed -i '/$RM "$cfgfile"/d' configure
./configure --prefix=/usr/local/apr
make
make install
cd /opt/apr-util-1.6.1
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
make
make install
cd /opt/httpd-2.4.54
./configure --prefix=/usr/local/apache \
--sysconfdir=/etc/httpd24 \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-zlib \
--with-pcre \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util/ \
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=prefork
make
make install
环境变量脚本
[root@server apache]# vim files/bianliang.sh
[root@server apache]# cat files/bianliang.sh
export PATH=/usr/local/apache/bin/:$PATH
vars变量
[root@server apache]# vim vars/main.yml
[root@server apache]# cat vars/main.yml
---
# vars file for apache
httpdpkgs:
- bzip2
- make
- wget
- openssl-devel
- pcre-devel
- expat-devel
- libtool
- gcc
- gcc-c++
- libxml2-devel
模板文件
[root@server apache]# vim templates/httpd.service.j2
[root@server apache]# cat templates/httpd.service.j2
[Unit]
Description=httpd server daemon
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/apache/bin/apachectl start
ExecStop=/usr/local/apache/bin/apachectl stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
上传软件包
[root@server files]# ls
apr-1.6.5.tar.bz2 apr-util-1.6.1.tar.bz2 bianliang.sh httpd-2.4.54.tar.bz2 httpd.sh yum.sh
调用apache角色
[root@server ansible]# ansible-playbook httpd.yml
PLAY [use apache roles] *******************************************************************************
TASK [Gathering Facts] ********************************************************************************
ok: [node1]
TASK [apache : set firewalld] *************************************************************************
ok: [node1]
TASK [apache : stop selinux] **************************************************************************
changed: [node1]
TASK [apache : setenforce 0] **************************************************************************
changed: [node1]
TASK [apache : yum.sh] ********************************************************************************
changed: [node1]
TASK [apache : install pkgs] **************************************************************************
[WARNING]: Consider using the yum module rather than running 'yum'. If you need to use command
because yum is insufficient you can add 'warn: false' to this command task or set
'command_warnings=False' in ansible.cfg to get rid of this message.
changed: [node1]
TASK [apache : uzip pkgs] *****************************************************************************
changed: [node1]
TASK [apache : uzip pkgs1] ****************************************************************************
changed: [node1]
TASK [apache : uzip pkgs2] ****************************************************************************
changed: [node1]
TASK [apache : create user] ***************************************************************************
ok: [node1]
TASK [apache : make install] **************************************************************************
changed: [node1]
TASK [apache : bianliang] *****************************************************************************
changed: [node1]
TASK [apache : set service] ***************************************************************************
changed: [node1]
TASK [apache : daemon-reload] *************************************************************************
changed: [node1]
TASK [apache : restart httpd] *************************************************************************
ok: [node1]
PLAY RECAP ********************************************************************************************
node1 : ok=15 changed=11 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
通过mysql角色配置mysql
[root@server ansible]# cd roles/
[root@server roles]# cd mysql/
[root@server mysql]# ls
defaults files handlers meta README.md tasks templates tests vars
tasks任务
[root@server mysql]# vim tasks/main.yml
[root@server mysql]# cat tasks/main.yml
---
# tasks file for mysql
- name: create user mysql
user:
name: mysql
system: yes
shell: /sbin/nologin
create_home: no
state: present
- name: install pkgs
yum:
name: "libncurses*"
state: present
- name: unzip
unarchive:
src: mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
dest: /usr/local/
- name: link
file:
src: /usr/local/mysql-5.7.37-linux-glibc2.12-x86_64
dest: /usr/local/mysql
group: mysql
owner: mysql
state: link
- name: create shuju dir
file:
name: /opt/data
group: mysql
owner: mysql
state: directory
- name: chu shi hua
script: haha-chushihua.sh
- name: set /etc/my.cnf
template:
src: my.cnf.j2
dest: /etc/my.cnf
- name: replace1
replace:
path: /usr/local/mysql/support-files/mysql.server
regexp: '#^(basedir=).*'
replace: "basedir=/usr/local/mysql"
- name: replace2
replace:
path: /usr/local/mysql/support-files/mysql.server
regexp: '#^(datadir=).*'
replace: "datadir=/opt/data"
- name: set mysqld.service.j2
template:
src: mysqld.service.j2
dest: /usr/lib/systemd/system/mysqld.service
- name: systemctl daemon-reload
shell: systemctl daemon-reload
- name: systemctl start mysqld
service:
name: mysqld
state: started
enabled: yes
- name: set mysql-password
shell: /usr/local/mysql/bin/mysql -uroot -e "set password=password('123456')"
- name: link env
script: mysql-hjbl.sh
初始化脚本
[root@server mysql]# vim files/zhan-chushihua.sh
[root@server mysql]# cat files/zhan-chushihua.sh
#!/bin/bash
/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/opt/data/
ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
ldconfig
上传二进制包
[root@server mysql]# ls
defaults handlers mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz tasks tests
files meta README.md templates vars
模板文件
[root@server mysql]# vim templates/my.cnf.j2
[root@server mysql]# cat templates/my.cnf.j2
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
[root@server mysql]# vim templates/mysqld.service.j2
[root@server mysql]# cat templates/mysqld.service.j2
[Unit]
Description=mysql server daemon
After=network.targe
[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStop=/usr/local/mysql/support-files/mysql.server stop
ExecReload=/bin/kill -HUP \$MAINPID
[Install]
WantedBy=multi-user.target
[root@server mysql]#
变量脚本
[root@server mysql]# vim files/mysql-hjbl.sh
[root@server mysql]# cat files/mysql-hjbl.sh
#!/bin/bash
echo 'export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile.d/mysql.sh
[root@server mysql]#
调用mysql角色
[root@server ansible]# vim mysqld.yml
[root@server ansible]# cat mysqld.yml
---
- name: use mysql roles
hosts: dev
roles:
- mysql
[root@server ansible]# ansible-playbook mysqld.yml
PLAY [use mysql roles] **************************************************************************************
TASK [Gathering Facts] **************************************************************************************
ok: [node1]
TASK [create user mysql] ************************************************************************************
ok: [node1]
TASK [mysql : install pkgs] *********************************************************************************
ok: [node1]
TASK [mysql : unzip] ****************************************************************************************
changed: [node1]
TASK [mysql : link] *****************************************************************************************
ok: [node1]
TASK [mysql : create shuju dir] *****************************************************************************
ok: [node1]
TASK [mysql : chu shi hua] **********************************************************************************
changed: [node1]
TASK [mysql : set /etc/my.cnf] ******************************************************************************
ok: [node1]
TASK [mysql : replace1] *************************************************************************************
ok: [node1]
TASK [mysql : replace2] *************************************************************************************
ok: [node1]
TASK [set mysqld.service.j2] ********************************************************************************
ok: [node1]
TASK [mysql : systemctl daemon-reload] **********************************************************************
changed: [node1]
TASK [systemctl start mysqld] *******************************************************************************
changed: [node1]
TASK [set mysql-password] ***********************************************************************************
changed: [node1]
TASK [mysql : link env] *************************************************************************************
changed: [node1]
PLAY RECAP **************************************************************************************************
node1 : ok=15 changed=6 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@node1 ~]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.37 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
[root@node1 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 128 127.0.0.1:6010 0.0.0.0:*
LISTEN 0 128 127.0.0.1:6011 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631 [::]:*
LISTEN 0 128 [::1]:6010 [::]:*
LISTEN 0 128 [::1]:6011 [::]:*
调用php角色配置php
上传软件包
[root@server roles]# cd php/
[root@server php]# ls
defaults files handlers meta README.md tasks templates tests vars
[root@server php]# cd files/
[root@server files]# ls
[root@server files]# ls
php-7.1.10.tar.gz
[root@server files]#
编写tasks任务
[root@server php]# vim tasks/main.yml
[root@server php]# cat tasks/main.yml
---
# tasks file for php
- name: unzip
unarchive:
src: php-7.1.10.tar.gz
dest: /opt/
- name: make install php
script: php-make.sh
- name: set config
replace:
path: /etc/httpd24/httpd.conf
regexp: "index.html"
replace: "index.php index.html"
- name: rm index.html
shell: rm -rf /usr/local/apache/htdocs/index.html
- name: create index.php
copy:
content: ""
dest: /usr/local/apache/htdocs/index.php
- name: restart httpd
service:
name: httpd
state: restarted
enabled: yes
vars变量
[root@server php]# vim vars/main.yml
[root@server php]# cat vars/main.yml
---
# vars file for php
phppkgs:
- libjpeg
- ibjpeg-devel
- libpng
- libpng-devel
- freetype
- freetype-devel
- libxml2
- libxml2-devel
- zlib
- zlib-devel
- curl
- curl-devel
php编译脚本
[root@server files]# vim php.sh
[root@server files]# cat php.sh
#!/bin/bash
cd /opt/php-7.1.10
./configure --prefix=/usr/local/php \
--with-apxs2=/usr/local/apache/bin/apxs \
--with-mysql-sock=/tmp/mysql.sock \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip
make
make install
cp php.ini-development /usr/local/php/lib/php.ini
sed -i 's/;date.timezone =/date\.timezone = \Asia\/Shanghai/' /usr/local/php/lib/php.ini
echo "AddType application/x-httpd-php .php" >> /etc/httpd24/httpd.conf
echo "AddType application/x-httpd-php-source .phps" >> /etc/httpd24/httpd.conf
sed -i 's/index.html/index\.php index\.html/' /etc/httpd24/httpd.conf
调用php角色
[root@server ansible]# vim php.yml
[root@server ansible]# cat php.yml
---
- name: use php roles
hosts: dev
roles:
- php
[root@server ansible]# ansible-playbook php.yml
PLAY [use php roles] **********************************************************************************
TASK [Gathering Facts] ********************************************************************************
ok: [node1]
TASK [php : unzip] ************************************************************************************
ok: [node1]
TASK [make install php] *******************************************************************************
changed: [node1]
TASK [php : set config] *******************************************************************************
changed: [node1]
TASK [php : rm index.html] ****************************************************************************
[WARNING]: Consider using the file module with state=absent rather than running 'rm'. If you need to
use command because file is insufficient you can add 'warn: false' to this command task or set
'command_warnings=False' in ansible.cfg to get rid of this message.
changed: [node1]
TASK [create index.php] *******************************************************************************
changed: [node1]
TASK [php : restart httpd] ****************************************************************************
changed: [node1]
PLAY RECAP ********************************************************************************************
node1 : ok=7 changed=5 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0