Ansible初探[安装nginx、mysql、tomcat]

作为入门,安装和基本配置练习是这样的:

  • 在一个IP上,安装一个NGINX,一个TOMCAT,一个MYSQL
  • Nginx 的 80 口反向代理到 tomcat 的 8080口
  • Mysql里面有db : students , table : profile, 两个字段: name , age
  • Insert 一行 到mysql ,”Teddy”, “12”

1、安装Ansible

#管理端
rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
#vim epel.repo    #替换https为http
yum install ansible
#节点[如果没有使用mysql_db模块,则无需安装]
yum install MySQL-python

2、节点设置

ssh-keygen #生成证书
112.74.209.102
#将IP添加到操作列表
vim /etc/ansible/hosts
[bgo]
112.74.209.102 ansible_ssh_port=200
#测试
ansible bgo -m ping

3、构建目录结构

mkdir -p /ansible/roles/{nginx,mysql,tomcat,db}/{defaults,files,handlers,meta,tasks,templates,vars}
  • defaults 默认寻找路径
  • tasks 存放playbooks路径
  • files 存放文件和脚本,copy模块文件搜索路径
  • templates 模版存放路径
  • handlers notify调用部分playbook存放路径
  • vars roles内变量存放路径

4、文件结构

tree /ansbile/
./
├── roles
│   ├── db
│   │   ├── files
│   │   │   └── stu.sql  #要导入的sql
│   │   ├── handlers
│   │   ├── meta
│   │   ├── tasks
│   │   │   └── main.yml  #创建数据库和导入sql
│   │   ├── templates
│   │   └── vars
│   ├── mysql
│   │   ├── default
│   │   ├── files
│   │   │   ├── mysql-5.6.27.tar.gz
│   │   │   └── mysql_install.sh #mysql源码安装脚本
│   │   ├── handlers
│   │   ├── meta
│   │   ├── tasks
│   │   │   └── main.yml #安装mysql
│   │   ├── templates
│   │   └── vars
│   ├── nginx
│   │   ├── default
│   │   ├── files
│   │   │   │─   nginx-1.8.0.tar.gz
│   │   │   └── install_nginx.sh  #nginx安装脚本
│   │   ├── handlers
│   │   ├── meta
│   │   ├── tasks
│   │   │   └── main.yml  #安装nginx
│   │   ├── templates
│   │   └── vars
│   └── tomcat
│       ├── default
│       ├── files
│             ├── apache-tomcat-7.0.65.tar.gz
│       │   ├── tomcat-initscript.sh  #tomcat的init管理脚本
│       │   └── tomcat-users.xml    #tomcat配置文件
│       ├── handlers
│       │   └── main.yml    #安装后处理
│       ├── meta
│       ├── tasks
│       │   └── main.yml  #安装tomcat
│       ├── templates
│       │   └── tomcat-users.xml   #tomcat配置文件模版
│       └── vars
└── web.yml    #总调用文件

5、playbooks & Shell

/ansible/web.yml

- hosts: bgo
  remote_user: root
  roles:
    - nginx
    - mysql
    - tomcat
    - db

/ansible/roles/db/tasks/main.yml

---
- name: create db
  mysql_db: name=student state=present login_password=bingoclo123 login_user=root login_unix_socket=/data/mysql/data/mysql.sock
- name: copy sql file
  copy: src=stu.sql dest=/tmp
- name: import sql
  mysql_db: name=student state=import target=/tmp/stu.sql login_password=bingoclo123 login_user=root login_unix_socket=/data/mysql/data/mysql.sock

/ansible/roles/db/files/stu.sql

create table profile(name varchar(20),age tinyint);
 insert into profile(name,age) values('teddy',12);

/ansible/roles/nginx/tasks/main.yml

- name: copy nginx_tar_gz to client
  copy: src=nginx-1.8.0.tar.gz dest=/tmp/nginx-1.8.0.tar.gz
- name: copy install_shell to client
  copy: src=install_nginx.sh dest=/tmp/install_nginx.sh
- name: install nginx
  shell: /bin/bash /tmp/install_nginx.sh

/ansible/roles/mysql/tasks/main.yml

- name: copy mysql_tar_gz to client
  copy: src=mysql-5.6.27.tar.gz dest=/tmp/mysql-5.6.27.tar.gz
- name: copy install_script to client
  copy: src=mysql_install.sh dest=/tmp/mysql_install.sh owner=root group=root mode=755
- name: install mysql
  shell: /bin/bash /tmp/mysql_install.sh

/ansible/roles/tomcat/tasks/main.yml

- name: install java
  yum: name=java-1.7.0-openjdk state=present
- name: group
  group: name=tomcat
- name: user
  user: name=tomcat group=tomcat home=/usr/tomcat
  sudo: True
- name: copy tomcat_tar_gz
  copy: src=apache-tomcat-7.0.65.tar.gz dest=/tmp/apache-tomcat-7.0.65.tar.gz
- name: Extract archive
  command: /bin/tar xf /tmp/apache-tomcat-7.0.65.tar.gz -C /opt/
- name: Symlink install directory
  file: src=/opt/apache-tomcat-7.0.65/ dest=/usr/share/tomcat state=link
- name: Change ownership of Tomcat installation
  file: path=/usr/share/tomcat/ owner=tomcat group=tomcat state=directory recurse=yes
- name: Configure Tomcat users
  template: src=tomcat-users.xml dest=/usr/share/tomcat/conf/
  notify: restart tomcat
- name: Install Tomcat init script
  copy: src=tomcat-initscript.sh dest=/etc/init.d/tomcat mode=0755
- name: Start Tomcat
  service: name=tomcat state=started enabled=yes

/ansible/roles/tomcat/handlers/main.yml

- name: restart tomcat 
  service: name=tomcat state=restarted

/ansible/roles/nginx/files/install_nginx.sh

#!/bin/bash
yum -y install zlib zlib-devel openssl openssl-devel pcre-devel
groupadd -r nginx
useradd -s /sbin/nologin -g nginx -r nginx
cd /tmp
tar xf nginx-1.8.0.tar.gz;cd nginx-1.8.0
mkdir /var/run/nginx/;chown nginx.nginx /var/run/nginx/
./configure \
--prefix=/usr \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--pid-path=/var/run/nginx/nginx.pid \
--user=nginx \
--group=nginx \
--with-http_ssl_module
make && make install
sed  "/^\s*index / i proxy_pass http://localhost:8080;" /etc/nginx/nginx.conf
/usr/sbin/nginx 
sed 

/ansible/roles/mysql/files/mysql_install.sh

#!/bin/bash
DBDIR='/data/mysql/data'
PASSWD='bingoclo123'
[ -d $DBDIR ] || mkdir $DBDIR -p
yum install cmake make gcc-c++ bison-devel ncurses-devel -y
id mysql &> /dev/null
if [ $? -ne 0 ];then
 useradd mysql -s /sbin/nologin -M
fi
chown -R mysql.mysql $DBDIR
cd /tmp/
tar xf mysql-5.6.27.tar.gz
cd mysql-5.6.27
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=$DBDIR \
-DMYSQL_UNIX_ADDR=$DBDIR/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DEXTRA_CHARSETS=all \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_READLINE=1 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EMBEDDED_SERVER=1
if [ $? != 0 ];then
 echo "cmake error!"
 exit 1
fi
make && make install
if [ $? -ne 0 ];then
 echo "install mysql is failed!" && /bin/false
fi
sleep 2
ln -s /usr/local/mysql/bin/* /usr/bin/
cp -f /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
cp -f /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod 700 /etc/init.d/mysqld
/usr/local/mysql/scripts/mysql_install_db  --basedir=/usr/local/mysql --datadir=$DBDIR --user=mysql
if [ $? -ne 0 ];then
 echo "install mysql is failed!" && /bin/false
fi
/etc/init.d/mysqld start
if [ $? -ne 0 ];then
 echo "install mysql is failed!" && /bin/false
fi
chkconfig --add mysqld
chkconfig mysqld on
/usr/local/mysql/bin/mysql -e "update mysql.user set password=password('$PASSWD') where host='localhost' and user='root';"
/usr/local/mysql/bin/mysql -e "update mysql.user set password=password('$PASSWD') where host='127.0.0.1' and user='root';"
/usr/local/mysql/bin/mysql -e "delete from mysql.user where password='';"
/usr/local/mysql/bin/mysql -e "flush privileges;"
if [ $? -eq 0 ];then
 echo "ins_done"
fi

6、执行安装 & 检查

cd /ansible
ansible-playbook web.yml --syntax-check  #检查语法
ansible-playbook web.yml  #执行
Ansible初探[安装nginx、mysql、tomcat]_第1张图片
屏幕快照 2015-10-21 下午1.32.54.png
Ansible初探[安装nginx、mysql、tomcat]_第2张图片
Paste_Image.png

结束语:
第一次玩Ansible,感觉比Puppet简单多了。虽然在很多做法选择上并不那么酷(很挫的另一种说法 T_T),Shell成分也相对较多,不过,作为Ansible的“初探”,也算是把常用功能模块都体验了一遍,熟悉了基本玩法,后续再认真全面地学习。
另:Ansible 大法好!

你可能感兴趣的:(Ansible初探[安装nginx、mysql、tomcat])