推荐资料
http://galaxy.ansible.com
https://galaxy.ansible.com/explore#/
http://github.com/
http://ansible.com.cn/
https://github.com/ansible/ansible
https://github.com/ansible/ansible-examples
准备阶段
准备一台 CentOS7服务器,安装好 ansible,并配置一台被远程管理的服务器.前提关闭防火墙和selinux。
这里用到centos7 absible1主机作为ansible机器。IP:192.168.32.7
被控制机器为centos8 ansible2 主机。IP:192.168.32.8
[root@ansible1 ~]#ansible --version
ansible 2.9.1
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Oct 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
[root@ansible1 ~]#ansible all -m ping
192.168.32.8 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
[root@ansible1 ~]#
案例1:实现httpd角色
[root@ansible1 ~]#
[root@ansible1 ~]#mkdir /data/ansible/roles/httpd/{tasks,handlers,files,templates,vars} -pv
mkdir: created directory ‘/data/ansible/roles’
mkdir: created directory ‘/data/ansible/roles/httpd’
mkdir: created directory ‘/data/ansible/roles/httpd/tasks’
mkdir: created directory ‘/data/ansible/roles/httpd/handlers’
mkdir: created directory ‘/data/ansible/roles/httpd/files’
mkdir: created directory ‘/data/ansible/roles/httpd/templates’
mkdir: created directory ‘/data/ansible/roles/httpd/vars’
[root@ansible1 ~]#cd /data/ansible/
[root@ansible1 ansible]#tree roles/
roles/
└── httpd
├── files
├── handlers
├── tasks
├── templates
└── vars
6 directories, 0 files
[root@ansible1 ansible]#cd roles/httpd/
[root@ansible1 httpd]#
[root@ansible1 ansible]#cd roles/httpd/
[root@ansible1 httpd]#vim tasks/main.yml
[root@ansible1 httpd]#cat tasks/main.yml #运行的顺序,注意名称和之后调用的要一致
- include: install.yml
- include: config.yml
- include: index.yml
- include: service.yml
[root@ansible1 httpd]#
[root@ansible1 httpd]#vim tasks/install.yml
[root@ansible1 httpd]#cat tasks/install.yml
- name: install httpd package
yum: name=httpd
[root@ansible1 httpd]#
[root@ansible1 ~]#cp /etc/httpd/conf/httpd.conf /data/ansible/roles/httpd/files/ #找寻配置文件
[root@ansible1 httpd]#vim files/httpd.conf
[root@ansible1 httpd]#cat files/httpd.conf |grep "Listen"
# Listen: Allows you to bind Apache to specific IP addresses and/or
# Change this to Listen on specific IP addresses as shown below to
#Listen 12.34.56.78:80
Listen 8080 #修改默认端口为8080
[root@ansible1 httpd]#
[root@ansible1 httpd]#vim tasks/config.yml
[root@ansible1 httpd]#cat tasks/config.yml
- name: config file
copy: src=httpd.conf dest=/etc/httpd/conf/ backup=yes
notify: restart
[root@ansible1 httpd]#
[root@ansible1 httpd]#cat files/index.html
<h1>This is a ansible playbook test for roles !</h1>
[root@ansible1 httpd]#
[root@ansible1 httpd]#vim tasks/index.yml
[root@ansible1 httpd]#cat tasks/index.yml
- name: index.html
copy: src=index.html dest=/var/www/html/
[root@ansible1 httpd]#
[root@ansible1 httpd]#vim tasks/service.yml
[root@ansible1 httpd]#cat tasks/service.yml
- name: start service
service: name=httpd state=started enabled=yes
[root@ansible1 httpd]#
[root@ansible1 httpd]#vim handlers/main.yml
[root@ansible1 httpd]#cat handlers/main.yml
- name: restart
service: name=httpd state=restarted
[root@ansible1 httpd]#cd /data/ansible/
[root@ansible1 ansible]#vim httpd_role.yml
[root@ansible1 ansible]#cat httpd_role.yml
---
# httpd role
- hosts: 192.168.32.8
remote_user: root
roles:
- role: httpd
[root@ansible1 ansible]#
[root@ansible1 ansible]#tree
.
├── httpd_role.yml
└── roles
└── httpd
├── files
│ ├── httpd.conf
│ └── index.html
├── handlers
│ └── main.yml
├── tasks
│ ├── config.yml
│ ├── index.yml
│ ├── install.yml
│ ├── main.yml
│ └── service.yml
├── templates
└── vars
7 directories, 9 files
[root@ansible1 ansible]#ansible-playbook -C httpd_role.yml
PLAY [192.168.32.8] ***********************************************************************************************
TASK [Gathering Facts] ********************************************************************************************
ok: [192.168.32.8]
TASK [httpd : install httpd package] ******************************************************************************
changed: [192.168.32.8]
TASK [httpd : config file] ****************************************************************************************
changed: [192.168.32.8]
TASK [httpd : index.html] *****************************************************************************************
changed: [192.168.32.8]
TASK [httpd : start service] **************************************************************************************
changed: [192.168.32.8]
RUNNING HANDLER [httpd : restart] *********************************************************************************
changed: [192.168.32.8]
PLAY RECAP ********************************************************************************************************
192.168.32.8 : ok=6 changed=5 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@ansible1 ansible]#ansible-playbook httpd_role.yml
PLAY [192.168.32.8] ***********************************************************************************************
TASK [Gathering Facts] ********************************************************************************************
ok: [192.168.32.8]
TASK [httpd : install httpd package] ******************************************************************************
changed: [192.168.32.8]
TASK [httpd : config file] ****************************************************************************************
changed: [192.168.32.8]
TASK [httpd : index.html] *****************************************************************************************
changed: [192.168.32.8]
TASK [httpd : start service] **************************************************************************************
changed: [192.168.32.8]
RUNNING HANDLER [httpd : restart] *********************************************************************************
changed: [192.168.32.8]
PLAY RECAP ********************************************************************************************************
192.168.32.8 : ok=6 changed=5 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
查看远程主机的8080端口是否打开。
[root@ansible1 ansible]#ansible 192.168.32.8 -a "ss -ntl"
192.168.32.8 | CHANGED | rc=0 >>
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 32 192.168.122.1:53 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 128 0.0.0.0:45475 0.0.0.0:*
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 0.0.0.0:8080 0.0.0.0:*
[root@ansible1 ansible]#curl 192.168.32.8:8080
<h1>This is a ansible playbook test for roles !</h1>
[root@ansible1 ansible]#
访问一下网页看是否成功:
案例2:实现nginx角色
这里用到centos7 absible1主机作为ansible机器。IP:192.168.32.7
被控制机器为centos8 ansible2 主机。IP:192.168.32.8
被控制机器为centos7 ansible3 主机。IP:192.168.32.77
前提都基于了ssh验证
实现nginx角色,实现跨角色调用其他角色里面的文件或者模板。
[root@ansible1 ~]#vim /etc/ansible/hosts
[root@ansible1 ~]#cat /etc/ansible/hosts |grep -Ev "^#|^$"
[appsrvs]
192.168.32.8
192.168.32.77
[root@ansible1 ~]#ansible all -m ping
192.168.32.8 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
192.168.32.77 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
[root@ansible1 ~]#
[root@ansible1 ~]#cd /data/ansible/
[root@ansible1 ansible]#ll
total 4
-rw-r--r-- 1 root root 88 Dec 7 17:50 httpd_role.yml
drwxr-xr-x 3 root root 19 Dec 7 17:23 roles
[root@ansible1 ansible]#mkdir roles/nginx/{tasks,handlers,files,templates,vars} -pv
mkdir: created directory ‘roles/nginx’
mkdir: created directory ‘roles/nginx/tasks’
mkdir: created directory ‘roles/nginx/handlers’
mkdir: created directory ‘roles/nginx/files’
mkdir: created directory ‘roles/nginx/templates’
mkdir: created directory ‘roles/nginx/vars’
[root@ansible1 ansible]#tree roles/
roles/
├── httpd
│ ├── files
│ │ ├── httpd.conf
│ │ └── index.html
│ ├── handlers
│ │ └── main.yml
│ ├── tasks
│ │ ├── config.yml
│ │ ├── index.yml
│ │ ├── install.yml
│ │ ├── main.yml
│ │ └── service.yml
│ ├── templates
│ └── vars
└── nginx
├── files
├── handlers
├── tasks
├── templates
└── vars
12 directories, 8 files
[root@ansible1 ansible]#
[root@ansible1 ansible]#cd roles/nginx/
[root@ansible1 nginx]#ls
files handlers tasks templates vars
[root@ansible1 nginx]#vim tasks/main.yml
[root@ansible1 nginx]#cat tasks/main.yml
- include: install.yml
- include: config.yml
- include: file.yml
- include: service.yml
[root@ansible1 nginx]#
[root@ansible1 nginx]#vim tasks/install.yml
[root@ansible1 nginx]#cat tasks/install.yml
- name: install
yum: name=nginx
[root@ansible1 nginx]#
[root@ansible1 nginx]#vim tasks/config.yml
[root@ansible1 nginx]#cat 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@ansible1 nginx]#
[root@ansible1 nginx]#vim tasks/file.yml
[root@ansible1 nginx]#cat tasks/file.yml
- name: index.html #这里调用了roles/httpd/files/index.html这个文件
copy: src=roles/httpd/files/index.html dest=/usr/share/nginx/html/
[root@ansible1 nginx]#cat /data/ansible/roles/httpd/files/index.html #查看roles/httpd/files/index.html文件内容
<h1>This is a ansible playbook test for roles !</h1>
[root@ansible1 nginx]#vim tasks/service.yml
[root@ansible1 nginx]#cat tasks/service.yml
- name: start service
service: name=nginx state=started enabled=yes
创建files文件和templates和handlers
其中files文件不用重新创建,这里调用了roles/httpd/files/index.html这个文件
[root@ansible1 nginx]#tree
.
├── files
├── handlers
├── tasks
│ ├── config.yml
│ ├── file.yml
│ ├── install.yml
│ ├── main.yml
│ └── service.yml
├── templates
└── vars
5 directories, 5 files
#创建templates
[root@ansible1 ~]#cp /etc/nginx/nginx.conf /data/ansible/roles/nginx/templates/nginx7.conf.j2
#寻找相关的配置文件 这里再本机安装了一个直接拷贝到roles/nginx/templates下重命名为nginx7.conf.j2
#同理,这里再8上面也找了一个相关的配置文件拷贝过来
[root@ansible1 nginx]#ll templates/
total 8
-rw-r--r-- 1 root root 2471 Dec 7 19:47 nginx7.conf.j2
-rw-r--r-- 1 root root 2469 Dec 7 19:56 nginx8.conf.j2
[root@ansible1 nginx]#
#修改两个template文件
[root@ansible1 nginx]#vim 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 {{ username }}; #使用变量username
worker_processes {{ ansible_processor_vcpus+2 }}; #使用系统变量ansible_processor_vcpus 进行数值运算 +2
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
[root@ansible1 nginx]#vim 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 {{ username }}; #使用变量username
worker_processes {{ ansible_processor_vcpus+4 }}; #使用系统变量ansible_processor_vcpus 进行数值运算 +4
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
定义username变量,这里可以定义再vars里面
[root@ansible1 nginx]#vim vars/maim.yml
[root@ansible1 nginx]#cat vars/maim.yml
username: daemon
#定义handles
[root@ansible1 nginx]#vim handlers/main.yml
[root@ansible1 nginx]#cat handlers/main.yml
- name: restart
service: name=nginx state=restarted
[root@ansible1 nginx]#
[root@ansible1 nginx]#tree
.
├── files
├── handlers
│ └── main.yml
├── tasks
│ ├── config.yml
│ ├── file.yml
│ ├── install.yml
│ ├── main.yml
│ └── service.yml
├── templates
│ ├── nginx7.conf.j2
│ └── nginx8.conf.j2
└── vars
└── main.yml
5 directories, 9 files
[root@ansible1 nginx]#
#返回上一级的上一级,编写一个和roles同一级的调用文件nginx_role.yml
[root@ansible1 ansible]#vim nginx_role.yml
[root@ansible1 ansible]#cat nginx_role.yml
---
#nginx role
- hosts: appsrvs
roles:
- role: nginx
端口号我们这里没有进行修改,所以会启用默认的80端口,和前面一个实验开启的8080端口并不冲突。
语法检测以及运行角色脚本
[root@ansible1 ansible]#ansible-playbook -C nginx_role.yml
PLAY [appsrvs] ****************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************
ok: [192.168.32.77]
ok: [192.168.32.8]
TASK [nginx : install] ********************************************************************************************
changed: [192.168.32.77]
ok: [192.168.32.8]
TASK [nginx : config file for centos7] ****************************************************************************
skipping: [192.168.32.8]
changed: [192.168.32.77]
TASK [nginx : config file for centos8] ****************************************************************************
skipping: [192.168.32.77]
changed: [192.168.32.8]
TASK [nginx : index.html] *****************************************************************************************
changed: [192.168.32.77]
changed: [192.168.32.8]
TASK [nginx : start service] **************************************************************************************
changed: [192.168.32.77]
changed: [192.168.32.8]
RUNNING HANDLER [nginx : restart] *********************************************************************************
changed: [192.168.32.77]
changed: [192.168.32.8]
PLAY RECAP ********************************************************************************************************
192.168.32.77 : ok=6 changed=5 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
192.168.32.8 : ok=6 changed=4 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
[root@ansible1 ansible]#ansible-playbook nginx_role.yml
PLAY [appsrvs] ****************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************
ok: [192.168.32.77]
ok: [192.168.32.8]
TASK [nginx : install] ********************************************************************************************
ok: [192.168.32.8]
changed: [192.168.32.77]
TASK [nginx : config file for centos7] ****************************************************************************
skipping: [192.168.32.8]
changed: [192.168.32.77]
TASK [nginx : config file for centos8] ****************************************************************************
skipping: [192.168.32.77]
changed: [192.168.32.8]
TASK [nginx : index.html] *****************************************************************************************
changed: [192.168.32.77]
changed: [192.168.32.8]
TASK [nginx : start service] **************************************************************************************
changed: [192.168.32.77]
changed: [192.168.32.8]
RUNNING HANDLER [nginx : restart] *********************************************************************************
changed: [192.168.32.77]
changed: [192.168.32.8]
PLAY RECAP ********************************************************************************************************
192.168.32.77 : ok=6 changed=5 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
192.168.32.8 : ok=6 changed=4 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
[root@ansible1 ansible]#curl 192.168.32.8
<h1>This is a ansible playbook test for roles !</h1>
[root@ansible1 ansible]#curl 192.168.32.77
<h1>This is a ansible playbook test for roles !</h1>
[root@ansible1 ansible]#
[root@ansible1 ansible]#ansible all -m shell -a "ps aux |grep "^daemon""
192.168.32.77 | CHANGED | rc=0 >>
daemon 9597 0.0 0.1 121180 3332 ? S 20:42 0:00 nginx: worker process
daemon 9598 0.0 0.1 121180 3332 ? S 20:42 0:00 nginx: worker process
daemon 9599 0.0 0.1 121180 3576 ? S 20:42 0:00 nginx: worker process
daemon 9600 0.0 0.1 121180 3576 ? S 20:42 0:00 nginx: worker process
centos7中有2颗CPU则默认会开启2个进程,这里的daemon就是之前我们定义的username变量的daemon,默认叫nginx
之后在进程变量中加上了数值运算+2 及这里线程一共为4个。centos8同理可得。centos8中+4
192.168.32.8 | CHANGED | rc=0 >>
daemon 34722 0.0 0.4 148700 7744 ? S 20:42 0:00 nginx: worker process
daemon 34723 0.0 0.4 148700 7724 ? S 20:42 0:00 nginx: worker process
daemon 34724 0.0 0.4 148700 7728 ? S 20:42 0:00 nginx: worker process
daemon 34725 0.0 0.4 148700 7744 ? S 20:42 0:00 nginx: worker process
daemon 34726 0.0 0.4 148700 7748 ? S 20:42 0:00 nginx: worker process
daemon 34727 0.0 0.4 148700 7748 ? S 20:42 0:00 nginx: worker process
访问网页是否能正常访问:
案例3 :实现memcached角色简单总结
mkdir -pv /data/ansible/roles/memcached/{tasks,templates}
cd /data/ansible/roles/memcached
vim tasks/main.yml
- include: install.yml
- include: config.yml
- include: service.yml
vim tasks/install.yml
- name: install
yum: name=memcached
vim tasks/config.yml
- name: config file
template: src=memcached.j2 dest=/etc/sysconfig/memcached
vim tasks/service.yml
- name: service
service: name=memcached state=started enabled=yes
vim templates/memcached.j2
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="{{ansible_memtotal_mb//4}}" #总内存的四分之一 //表示取整数
OPTIONS=""
tree /data/ansible/roles/memcached/
/data/ansible/roles/memcached/
├── tasks
│ ├── config.yml
│ ├── install.yml
│ ├── main.yml
│ └── service.yml
└── templates
└── memcached.j2
2 directories, 5 files
vim /data/ansible/role_memcached.yml
---
- hosts: appsrvs
roles:
- role: memcached
ansible-play /data/ansible/role_memcached.yml
案例5 :实现多角色的选择
vim /data/ansible/role_httpd_nginx.yml
---
- hosts: appsrvs
roles:
- {role: httpd,tags: [httpd,web], when: ansible_distribution_major_version=="7" }
- {role: nginx,tags: [nginx,web], when: ansible_distribution_major_version=="8" }
ansible-playbook -t nginx /data/ansible/role_httpd_nginx.yml
表示安装nginx标签所指的角色,同时也要满足后面的版本centos8.
案例6:实现mysql二进制安装角色
[root@ansible1 ~]#vim /etc/ansible/hosts
[root@ansible1 ~]#cat /etc/ansible/hosts |grep -Ev "^#|^$"
[appsrvs]
192.168.32.8
[root@ansible1 ~]#ansible all -m ping
192.168.32.8 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
[root@ansible1 ~]#
[root@ansible1 ~]#cd /data/ansible/
[root@ansible1 ansible]#
[root@ansible1 ansible]#mkdir roles/mysql/{tasks,handlers,files,templates,vars} -pv
mkdir: created directory ‘roles/mysql’
mkdir: created directory ‘roles/mysql/tasks’
mkdir: created directory ‘roles/mysql/handlers’
mkdir: created directory ‘roles/mysql/files’
mkdir: created directory ‘roles/mysql/templates’
mkdir: created directory ‘roles/mysql/vars’
[root@ansible1 ansible]#cd roles/mysql/
[root@ansible1 mysql]#
# 准备配置文件
[root@ansible1 ansible]#cd roles/mysql/
[root@ansible1 mysql]#vim files/my.cnf #数据库配置文件
[root@ansible1 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@ansible1 mysql]#vim files/secure_mysql.sh #数据库安全脚本
[root@ansible1 mysql]#cat files/secure_mysql.sh
#!/bin/bash
/usr/local/mysql/bin/mysql_secure_installation <<EOF
#回车
y
magedu
magedu
y
y
y
y
EOF
[root@ansible1 mysql]#ls files/
my.cnf secure_mysql.sh
[root@ansible1 mysql]#chmod +x files/secure_mysql.sh #给安全加固脚本加上执行权限
[root@ansible1 mysql]#cd files/
[root@ansible1 files]#rz -E #上传数据库二进制文件mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz
rz waiting to receive.
[root@ansible1 files]#cd ../
[root@ansible1 mysql]#ls files/
my.cnf mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz secure_mysql.sh
[root@ansible1 mysql]#vim tasks/main.yml #剧本任务顺序定义,步骤定义
[root@ansible1 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@ansible1 mysql]#
[root@ansible1 mysql]#vim tasks/install.yml #安装
[root@ansible1 mysql]#cat tasks/install.yml
- name: install packages
yum: name=libaio,perl-Data-Dumper,perl-Getopt-Long
[root@ansible1 mysql]#vim tasks/group.yml #创建组
[root@ansible1 mysql]#cat tasks/group.yml
- name: create mysql group
group: name=mysql gid=306
[root@ansible1 mysql]#vim tasks/user.yml #创建用户
[root@ansible1 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@ansible1 mysql]#vim tasks/unarchive.yml #解压缩
[root@ansible1 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@ansible1 mysql]#vim tasks/link.yml #创建软连接
[root@ansible1 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@ansible1 mysql]#vim tasks/data.yml #创建数据库
[root@ansible1 mysql]#cat tasks/data.yml
- name: data dir
shell: chdir=/usr/local/mysql/ ./scripts/mysql_install_db --datadir=/data/mysql --user=mysql
[root@ansible1 mysql]#vim tasks/config.yml #配置文件
[root@ansible1 mysql]#cat tasks/config.yml
- name: config my.cnf
copy: src=my.cnf dest=/etc/my.cnf
[root@ansible1 mysql]#vim tasks/service.yml #启动服务
[root@ansible1 mysql]#cat tasks/service.yml
- name: service script
shell: /bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld;/etc/init.d/mysqld
start;chkconfig --add mysqld;chkconfig mysqld on
[root@ansible1 mysql]#vim tasks/path.yml #PATH路径修改
[root@ansible1 mysql]#cat tasks/path.yml
- name: PATH variable
copy: content='PATH=/usr/local/mysql/bin:$PATH' dest=/etc/profile.d/mysql.sh
[root@ansible1 mysql]#vim tasks/secure.yml #安全加固脚本
[root@ansible1 mysql]#cat tasks/secure.yml
- name: secure script
script: secure_mysql.sh
[root@ansible1 mysql]#tree
.
├── files
│ ├── my.cnf
│ ├── mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz
│ └── secure_mysql.sh
├── handlers
├── tasks
│ ├── config.yml
│ ├── data.yml
│ ├── group.yml
│ ├── install.yml
│ ├── link.yml
│ ├── main.yml
│ ├── path.yml
│ ├── secure.yml
│ ├── service.yml
│ ├── unarchive.yml
│ └── user.yml
├── templates
└── vars
5 directories, 14 files
[root@ansible1 mysql]#cd ../../
[root@ansible1 ansible]#vim mysql_roles.yml
[root@ansible1 ansible]#cat mysql_roles.yml
- hosts: appsrvs
remote_user: root
roles:
- {role: mysql,tags: ["mysql","db"]}
[root@ansible1 ansible]#
测试和运行:
[root@ansible1 ansible]#ansible-playbook -t mysql -C mysql_roles.yml
PLAY [appsrvs] ****************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************
ok: [192.168.32.8]
TASK [mysql : install packages] ***********************************************************************************
changed: [192.168.32.8]
TASK [mysql : create mysql group] *********************************************************************************
changed: [192.168.32.8]
TASK [mysql : create mysql user] **********************************************************************************
changed: [192.168.32.8]
TASK [mysql : copy tar to remote host and file mode] **************************************************************
skipping: [192.168.32.8]
TASK [mysql : mkdir /usr/local/mysql] *****************************************************************************
fatal: [192.168.32.8]: FAILED! => {"changed": false, "msg": "src file does not exist, use \"force=yes\"
if you really want to create the link: /usr/local/mysql-5.6.46-linux-glibc2.12-x86_64", "path": "/usr/local/mysql",
"src": "/usr/local/mysql-5.6.46-linux-glibc2.12-x86_64"}
PLAY RECAP ********************************************************************************************************
192.168.32.8 : ok=4 changed=3 unreachable=0 failed=1 skipped=1 rescued=0 ignored=0
上面报错是因为暂时还没有创建出文件,属于正常报错。测试语法是否错误,但实际上还没有执行。
[root@ansible1 ansible]#ansible-playbook -t mysql mysql_roles.yml
PLAY [appsrvs] ****************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************
ok: [192.168.32.8]
TASK [mysql : install packages] ***********************************************************************************
ok: [192.168.32.8]
TASK [mysql : create mysql group] *********************************************************************************
changed: [192.168.32.8]
TASK [mysql : create mysql user] **********************************************************************************
changed: [192.168.32.8]
TASK [mysql : copy tar to remote host and file mode] **************************************************************
changed: [192.168.32.8]
TASK [mysql : mkdir /usr/local/mysql] *****************************************************************************
changed: [192.168.32.8]
TASK [mysql : data dir] *******************************************************************************************
changed: [192.168.32.8]
TASK [mysql : config my.cnf] **************************************************************************************
changed: [192.168.32.8]
TASK [mysql : service script] *************************************************************************************
changed: [192.168.32.8]
TASK [mysql : PATH variable] **************************************************************************************
changed: [192.168.32.8]
TASK [mysql : secure script] **************************************************************************************
changed: [192.168.32.8]
PLAY RECAP ********************************************************************************************************
192.168.32.8 : ok=10 changed=8 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
查看数据库端口3306是否启动
[root@ansible1 ansible]#ansible all -a 'ss -ntl'
192.168.32.8 | CHANGED | rc=0 >>
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 32 192.168.122.1:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::1]:6010 [::]:*
LISTEN 0 128 [::1]:6011 [::]:*
LISTEN 0 128 [::]:49821 [::]:*
LISTEN 0 80 *:3306 *:*