ansible安装与核心组件详解:http://www.cnblogs.com/Carr/p/7446917.html
ansible初始化配置:
1、修改/etc/ansible/hosts配置文件,在尾部添加内容
vim /etc/ansible/hosts
[203] #自定义组名
192.168.0.203 #客户端IP地址
hostname #主机名(前提有DNS域名解析或者修改/etc/hosts文件添加内部解析)
注意:如果不配置/etc/ansible/hosts添加客户端IP,在执行ansible命令时,会报错
通过ansible远程批量安装服务
1、ansible安装(nginx、tomcat、mysql)
在一个IP上,安装一个NGINX,一个TOMCAT,一个MYSQL
Nginx 的 80 口反向代理到 tomcat 的 8080口
Mysql里面有db : students , table : profile, 两个字段: name , age
Insert 一行 到mysql ,”Teddy”, “12”
2、构建目录结构
mkdir -p /home/cudo/ansible_server/roles/{nginx,mysql,tomcat,db}/{defaults,files,handlers,meta,tasks,templates,vars}
●defaults 默认寻找路径
●tasks 存放playbooks路径
●files 存放文件和脚本,copy模块文件搜索路径
●templates 模版存放路径
●handlers notify调用部分playbook存放路径
●vars roles内变量存放路径
3、文件结构
tree /home/cudo/ansible_server/roles/
/home/cudo/ansible_server/roles/
├── db
│ ├── defaults
│ ├── files
│ │ └── stu.sql #要导入的sql
│ ├── handlers
│ ├── meta
│ ├── tasks
│ │ └── main.yml #创建数据库和导入sql
│ ├── templates
│ └── vars
├── mysql
│ ├── defaults
│ ├── files
│ │ ├── mysql-5.6.27.tar.gz
│ │ └── mysql_install.sh #mysql源码安装脚本
│ ├── handlers
│ ├── meta
│ ├── tasks
│ │ └── main.yml #安装mysql
│ ├── templates
│ └── vars
├── nginx
│ ├── defaults
│ ├── files
│ │ ├── index.html #nginx测试主页面
│ │ ├── install_nginx.sh #nginx安装脚本
│ │ ├── nginx-1.8.0.tar.gz
│ │ ├── nginx.conf #nginx主配置文件
│ │ └── test.conf #nginx测试虚拟主机配置文件
│ ├── handlers
│ ├── meta
│ ├── tasks
│ │ └── main.yml #安装nginx
│ ├── templates
│ └── vars
└── tomcat
├── defaults
├── files
│ ├── apache-tomcat-7.0.29.tar.gz
│ ├── install_java.sh #java安装脚本
│ ├── install_tomcat.sh #tomcat安装脚本
│ ├── jdk1.7.0_79.tar.gz
│ ├── server.xml #tomcat配置文件
│ └── start.sh #tomcat服务启动脚本
├── handlers
├── meta
├── tasks
│ └── main.yml #安装java、tomcat
├── templates
└── vars
├── web.yml #总调用的文件
4、playbooks & shell
/ansible/web.yml
- hosts: bgo
remote_user: root
roles:- nginx
- mysql
- tomcat
- db
注意:在roles: # - nginx (#)为注释,不安装nginx
/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: copy nginx.conf to client
copy: src=nginx.conf dest=/tmp/nginx.conf - name: copy test.conf to client
copy: src=test.conf dest=/tmp/test.conf - name: copy index.html to client
copy: src=index.html dest=/tmp/index.html - 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=catalina.sh dest=/etc/init.d/tomcat mode=0755 - name: Start Tomcat
service: name=tomcat state=started enabled=yes
/ansible/roles/nginx/files/install_nginx.sh
#!/bin/bash
yum -y install zlib zlib-devel openssl openssl-devel pcre-devel gcc pcre pcre-devel automake GeoIP GeoIP-devel GeoIP-data
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/local/nginx \
#--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 \
--with-stream \
--with-http_stub_status_module \
--with-http_v2_module \
--with-http_gzip_static_module \
--with-ipv6 \
--with-http_sub_module \
--with-http_flv_module \
--with-http_geoip_module \
--with-pcre
make && make install
#sed "/^\sindex / i proxy_pass http://localhost:8080;" /etc/nginx/nginx.conf
/bin/rm -f /usr/local/nginx/conf/nginx.conf
/bin/cp /tmp/nginx.conf /usr/local/nginx/conf/nginx.conf
/bin/mkdir -p /usr/local/nginx/conf/extra
/bin/cp /tmp/test.conf /usr/local/nginx/conf/extra/
/usr/local/nginx/sbin/nginx &
/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
5、执行安装 & 检查
cd /home/cudo/ansible_server
ansible-playbook web.yml --syntax-check #检查语法
ansible-playbook web.yml #执行
二进制免编译安装mysql
/home/cudo/ansible_server/roles/mysql2/
├── defaults
├── files
│ ├── 3306.tar.gz
│ ├── mysql-5.6.16.tar.gz
│ └── mysql_install.sh
├── handlers
├── meta
├── tasks
│ └── main.yml
├── templates
└── vars
/home/cudo/ansible_server/roles/mysql2/tasks/main.yml
- name: copy mysql_tar_gz to client
copy: src=mysql-5.6.16.tar.gz dest=/tmp/mysql-5.6.16.tar.gz
- name: copy 3306_tar_gz to client
copy: src=3306.tar.gz dest=/tmp/3306.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
/home/cudo/ansible_server/roles/mysql2/files/mysql_install.sh
#!/bin/bash
####定义mysql数据库路径,和mysql登录密码
DBDIR='/data'
PASSWD='ever2016'
####判断数据目录是否存在如果不存在递归创建目录
[ -d $DBDIR ] || mkdir $DBDIR -p
####安装mysql组件
yum install cmake make gcc-c++ bison-devel ncurses-devel libaio 'perl(Data::Dumper)' -y
id mysql &> /dev/null
####如果执行id mysql输出为0 那么就是执行正确创建mysql用户
if [ $? -ne 0 ];then
useradd mysql -s /sbin/nologin -M
fi
####切换到tmp目录,解压mysql到/usr/local
cd /tmp/
tar -zxf mysql-5.6.16.tar.gz -C /usr/local/
tar -zxf 3306.tar.gz -C /data/
####赋予数据目录权限
chown -R mysql.mysql $DBDIR
chown -R mysql.mysql /usr/local/mysql-5.6.16
####初始化mysql
/usr/local/mysql-5.6.16/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/data/mysqldata/3306/data --user=mysql
####如果mysql初始化失败提示install mysql is failed! 退出安装mysql
if [ $? -ne 0 ];then
echo "install mysql is failed!" && /bin/false
exit 1
fi
####执行权
chmod 700 /data/mysqldata/3306/mysqld
####启动mysql
/data/mysqldata/3306/mysqld start
sleep 5
####更新数据库用户密码
/usr/local/mysql-5.6.16/bin/mysql -S /data/mysqldata/3306/mysql.sock -e "update mysql.user set password=password('$PASSWD') where host='localhost' and user='root';"
/usr/local/mysql-5.6.16/bin/mysql -S /data/mysqldata/3306/mysql.sock -e "update mysql.user set password=password('$PASSWD') where host='127.0.0.1' and user='root';"
/usr/local/mysql-5.6.16/bin/mysql -S /data/mysqldata/3306/mysql.sock -e "delete from mysql.user where password='';"
/usr/local/mysql-5.6.16/bin/mysql -S /data/mysqldata/3306/mysql.sock -e "flush privileges;"
if [ $? -eq 0 ];then
echo "ins_done"
fi
/home/cudo/ansible_server/mysql2.yml
- hosts: bgo
remote_user: root
roles:
- mysql2
cd /home/cudo/ansible_server
ansible-playbook mysql2.yml --syntax-check #检查语法
ansible-playbook mysql2.yml #执行