5.5.2 案例2,实现nginx角色
——————————————
创建目录
[root@hdss7-11 ~]# cd /data/ansible/
[root@hdss7-11 ansible]# mkdir -pv roles/nginx/{tasks,handlers,template,vars}
mkdir: 已创建目录 "roles/nginx"
mkdir: 已创建目录 "roles/nginx/tasks"
mkdir: 已创建目录 "roles/nginx/handlers"
mkdir: 已创建目录 "roles/nginx/template"
mkdir: 已创建目录 "roles/nginx/vars"
如果没有安装nginx可以安装一下,需要它的配置文件
yum -y install nginx
[root@hdss7-11 ansible]# cp /etc/nginx/nginx.conf /data/ansible/roles/nginx/template/nginx7.conf.j2
[root@hdss7-11 ansible]# cp /etc/nginx/nginx.conf /data/ansible/roles/nginx/template/nginx8.conf.j2 注意这个是nginx8的
然后卸载安装的nginx,为了卸载的干净用下面方式
yum history undo 35 这是全部撤销的用法
检测centos8上和centos7上的nginx版本
[root@node2 ~]# yum info nginx
Loaded plugins: fastestmirror, langpacks
Repository base is listed more than once in the configuration
Loading mirror speeds from cached hostfile
* base: mirrors.njupt.edu.cn
* epel: hkg.mirror.rackspace.com
* extras: mirrors.163.com
* updates: mirrors.bupt.edu.cn
Available Packages
Name : nginx
Arch : x86_64
Epoch : 1
Version : 1.20.1
Release : 2.el7
Size : 586 k
Repo : epel/x86_64
Summary : A high performance web server and reverse proxy server
URL : https://nginx.org
License : BSD
Description : Nginx is a web server and a reverse proxy server for HTTP, SMTP, POP3 and
: IMAP protocols, with a strong focus on high concurrency, performance and low
: memory usage.
[root@localhost ~]# yum info nginx
CentOS-8 - AppStream 445 kB/s | 8.8 MB 00:20
CentOS-8 - Base 46 kB/s | 5.6 MB 02:03
CentOS-8 - Extras 3.5 kB/s | 10 kB 00:02
Available Packages
Name : nginx
Epoch : 1
Version : 1.14.1
Release : 9.module_el8.0.0+184+e34fea82
Arch : x86_64
Size : 570 k
Source : nginx-1.14.1-9.module_el8.0.0+184+e34fea82.src.rpm
Repo : AppStream
Summary : A high performance web server and reverse proxy server
URL : http://nginx.org/
License : BSD
Description : Nginx is a web server and a reverse proxy server for HTTP, SMTP, POP3 and
: IMAP protocols, with a strong focus on high concurrency, performance and low
: memory usage.
整体看下需要创建的文件
[root@hdss7-11 ansible]# tree roles/nginx/
roles/nginx/
├── handlers
│ └── main.yml
├── tasks
│ ├── config.yml
│ ├── html.yml
│ ├── install.yml
│ ├── main.yml
│ └── service.yml
├── templates
│ ├── nginx7.conf.j2
│ └── nginx8.conf.j2
└── vars
└── main.yml
4 directories, 9 files
一个个的创建
[root@hdss7-11 ansible]# cat roles/nginx/tasks/install.yml
- name: install
yum: name=nginx
[root@hdss7-11 ansible]# cat roles/nginx/tasks/config.yml
- name: config file for centos7
template: src=nginx7.conf.j2 dest=/etc/nginx/nginx.conf
when: ansible_distribution_major_version=="7"
notify: restart
- name: config file for centos8
template: src=nginx8.conf.j2 dest=/etc/nginx/nginx.conf
when: ansible_distribution_major_version=="8"
notify: restart
[root@hdss7-11 ansible]# cat roles/nginx/tasks/service.yml
- name: start service
service: name=nginx state=started enabled=yes
[root@hdss7-11 ansible]# cat roles/nginx/tasks/html.yml
- name: index.html
copy: src=roles/httpd/files/index.html dest=/usr/share/nginx/html/
[root@hdss7-11 ansible]# cat roles/nginx/tasks/main.yml
- include: install.yml
- include: config.yml
- include: html.yml
- include: service.yml
[root@hdss7-11 ansible]# cat roles/nginx/vars/main.yml
user: daemon
[root@hdss7-11 ansible]# cat roles/nginx/handlers/main.yml
- name: restart
service: name=nginx state=restarted
[root@hdss7-11 ansible]# head -10 roles/nginx/templates/nginx7.conf.j2
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user {{user}};
worker_processes {{ansible_processor_vcpus**2}};
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
[root@hdss7-11 ansible]# head -10 roles/nginx/templates/nginx8.conf.j2
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes {{ansible_processor_vcpus+3}};
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
[root@hdss7-11 ansible]# ansible-playbook -C role_nginx.yml
进行测试安装
netstat -aluntp |grep 80 检查下80端口有人用没
ansible-playbook role_nginx.yml
检查下端口
ansible websrvs -m shell -a 'ss -ntlp |grep 80'
然后可以去centos 8 上和7上看进程数是否对,centos8 我是双核双线程所以启动的进程数应该是7个kworker进程因为 22+3=7用命令ps -aux可以查看
centos7上应该是22**2=16
5.5.3 案例 memcached角色
这个软件主要是用缓存使用的
——————————————
首先安装个memcached软件把配置文件拷贝到主机上
[root@node2 ~]# yum -y install memcached 这个软件很小的
[root@node2 ~]# rpm -ql memcached
/etc/sysconfig/memcached
/usr/bin/memcached
/usr/bin/memcached-tool
/usr/lib/systemd/system/memcached.service
/usr/share/doc/memcached-1.4.15
/usr/share/doc/memcached-1.4.15/AUTHORS
/usr/share/doc/memcached-1.4.15/CONTRIBUTORS
/usr/share/doc/memcached-1.4.15/COPYING
/usr/share/doc/memcached-1.4.15/ChangeLog
/usr/share/doc/memcached-1.4.15/NEWS
/usr/share/doc/memcached-1.4.15/README.md
/usr/share/doc/memcached-1.4.15/protocol.txt
/usr/share/doc/memcached-1.4.15/readme.txt
/usr/share/doc/memcached-1.4.15/threads.txt
/usr/share/man/man1/memcached-tool.1.gz
/usr/share/man/man1/memcached.1.gz
[root@node2 ~]# scp /etc/sysconfig/memcached 10.4.7.11:
[root@node2 ~]# yum -y remove memcached
[root@hdss7-11 yum.repos.d]# cd -
/data/ansible
[root@hdss7-11 ansible]# mkdir -pv roles/memcached/{tasks,templates}
mkdir: 已创建目录 "roles/memcached"
mkdir: 已创建目录 "roles/memcached/tasks"
mkdir: 已创建目录 "roles/memcached/templates"
[root@hdss7-11 ansible]# cd roles/memcached/
[root@hdss7-11 ansible]# cd roles/memcached
[root@hdss7-11 memcached]# cat tasks/main.yml
- include: install.yml
- include: config.yml
- include: service.yml
[root@hdss7-11 memcached]# cat tasks/install.yml
- name: install
yum: name=memcached
[root@hdss7-11 memcached]# cat tasks/config.yml
- name: config file
template: src=memcached.j2 dest=/etc/sysconfig/memcached
[root@hdss7-11 memcached]# cat tasks/service.yml
- name: service
service: name=memcached state=started enabled=yes
[root@hdss7-11 memcached]# cat tasks/service.yml
- name: service
service: name=memcached state=started enabled=yes
[root@hdss7-11 memcached]# cd /data/ansible/
[root@hdss7-11 ansible]# cat roles/memcached/templates/memcached.j2
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="{{ansible_memtotal_mb//4}}" #修下然它整除
OPTIONS=""
[root@hdss7-11 ansible]# tree roles/memcached/
roles/memcached/
├── tasks
│ ├── config.yml
│ ├── install.yml
│ ├── main.yml
│ └── service.yml
└── templates
└── memcached.j2
2 directories, 5 files
[root@hdss7-11 ansible]# ansible-playbook -C role_memcached.yml --limit 10.4.7.22
然后执行安装
[root@hdss7-11 ansible]# ansible-playbook role_memcached.yml --limit 10.4.7.22
测试下是否正常
[root@hdss7-11 ansible]# ansible 10.4.7.22 -a "cat /etc/sysconfig/memcached"
10.4.7.22 | CHANGED | rc=0 >>
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="1950"
OPTIONS=""
[root@hdss7-11 ansible]# ansible 10.4.7.22 -a "ss -ntl |grep 11211"
10.4.7.22 | CHANGED | rc=0 >>
LISTEN 0 128 *:11211 *:*
LISTEN 0 128 :::11211 :::*
5.5.4 案例4:实现MySQL的角色
——————————————
[root@hdss7-11 ansible]# cd roles/mysql/
[root@hdss7-11 mysql]# tree
.
├── files
│ ├── my.cnf
│ ├── mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz
│ └── secure_mysql.sh
└── tasks
├── config.yml
├── data.yml
├── group.yml
├── install.yml
├── link.yml
├── main.yml
├── path.yml
├── secure.yml
├── service.yml
├── unarchive.yml
└── user.yml
2 directories, 14 files
[root@hdss7-11 mysql]# cat 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
[root@hdss7-11 mysql]# cat tasks/install.yml
- name: install packages
yum: name=libaio,perl-Data-Dumper,perl-Getopt-Long
[root@hdss7-11 mysql]# cat tasks/config.yml
- name: config my.cnf
copy: src=my.cnf dest=/etc/my.cnf
[root@hdss7-11 mysql]# cat tasks/data.yml
- name: data dir
shell: chdir=/usr/local/mysql/ ./scripts/mysql_install_db --datadir=/data/mysql --user=mysql
[root@hdss7-11 mysql]# cat tasks/group.yml
- name: create mysql group
group: name=mysql gid=306
[root@hdss7-11 mysql]# cat tasks/link.yml
- name: mkdir /usr/local/mysql
file: src=/usr/local/mysql-5.6.46-linux-glibc2.12-x86_64 dest=/usr/local/mysql state=link
[root@hdss7-11 mysql]# cat tasks/path.yml
- name: PATH variable
copy: content='PATH=/usr/local/mysql/bin:$PATH' dest=/etc/profile.d/mysql.sh
[root@hdss7-11 mysql]# cat tasks/secure.yml
- name: secure script
script: secure_mysql.sh
[root@hdss7-11 mysql]# cat tasks/service.yml
- name: service script
shell: /bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld;chkconfig --add mysqld;chkconfig mysqld on;/etc/init.d/mysqld start
[root@hdss7-11 mysql]# cat tasks/unarchive.yml
- name: copy tar to remote host and file mode
unarchive: src=mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz dest=/usr/local/ owner=root group=root
[root@hdss7-11 mysql]# cat tasks/user.yml
- name: create mysql user
user: name=mysql uid=306 group=mysql shell=/sbin/nologin system=yes create_home=no home=/data/mysql
[root@hdss7-11 mysql]# cat files/secure_mysql.sh
#!/bin/bash
/usr/local/mysql/bin/mysql_secure_installation <<EOF
y
lai123
lai123
y
y
y
y
EOF
[root@hdss7-11 mysql]# cat files/my.cnf
[mysqld]
socket=/tmp/mysql.sock
user=mysql
symbolic-links=0
datadir=/data/mysql
innodb_file_per_table=1
log-bin
pid-file=/data/mysql/mysqld.pid
[client]
port=3306
socket=/tmp/mysql.sock
[mysqld_safe]
log-error=/var/log/mysqld.log
[root@hdss7-11 mysql]#
[root@hdss7-11 mysql]# cd ../../
[root@hdss7-11 ansible]# vim role_mysql.yml
[root@hdss7-11 ansible]# cat role_mysql.yml
- hosts: dbsrvs
remote_user: root
roles:
- {role: mysql,tags: ["mysql","db"]}
- {role: nginx,tags: ["nginx","web"]}
[root@hdss7-11 ansible]# ansible-playbook role_mysql.yml
测试