1.Ansible playbook实现apache批量部署,并对不同主机提供以各自IP地址为内容的index.html
yum install ansible -y
mkdir /ansible
cd /ansible
vim /etc/ansible/hosts
#添加分组
[web]
192.168.88.17
192.168.88.7
cd /ansible
#编写playblook
vim httpd_install.yml
- hosts: web
tasks:
- name: install httpd
yum: name=httpd
- name: start index page
copy: content={{ansible_all_ipv4_addresses}} dest=/var/www/html/index.html
- name: start httpd
service: name=httpd state=started
#测试
ansible-playbook -C httpd_install.yml
#没问题,执行
ansible-playbook httpd_install.yml
#验证
.....
2.ansible-playbook实现MySQL的二进制部署
#准备my.cnf
mkdir files
cat my.cnf
[mysqld]
user=mysql
basedir=/usr/local/mysql
datadir=/data/3306
server_id=6
port=3306
socket=/tmp/mysql.sock
[mysql]
socket=/tmp/mysql.sock
#编写playbook
vim mysql_install.yml
- hosts: web
gather_facts: no
vars:
version: 5.7.26
file_name: mysql-{{version}}-linux-x86_64.tar.gz
tasks:
- name: create mysql group
group: name=mysql system=yes
- name: create mysql user
user: name=mysql group=mysql shell=/sbin/nologin system=yes create_home=no
- name: create mysql data dir
file: path=/data/3306 state=directory owner=mysql
- name: download mysql file
get_url: url=https://downloads.mysql.com/archives/get/p/23/file/mysql-{{version}}-linux-glibc2.12-x86_64.tar.gz dest=/root/ timeout=60
- name: unarchive
unarchive: copy=no src=/root/mysql-{{version}}-linux-glibc2.12-x86_64.tar.gz dest=/usr/local/
- name: set link
file: src=/usr/local/mysql-{{version}}-linux-glibc2.12-x86_64 dest=/usr/local/mysql state=link
- name: copy my.conf
copy: src=my.cnf dest=/etc/my.cnf
- name: init data dir
shell: /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/data/3306
- name: PATH variable
copy: content='PATH=/usr/local/mysql/bin:$PATH' dest=/etc/profile.d/mysql.sh
- name: copy service file
shell: /bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld;chkconfig --add mysqld
- name: start service
service: name=mysqld state=started enabled=yes
PS: get_url 把超时时间改长,不然可能下载失败,默认10
#测试
ansible-playbook -C mysql_install.yml
#没问题,执行
ansible-playbook mysql_install.yml
#验证
.....