作為入門跋核,安裝和基本配置練習(xí)是這樣的:
- 在一個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
#節(jié)點[如果沒有使用mysql_db模塊蹋订,則無需安裝]
yum install MySQL-python
2、節(jié)點設(shè)置
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刻伊、構(gòu)建目錄結(jié)構(gòu)
mkdir -p /ansible/roles/{nginx,mysql,tomcat,db}/{defaults,files,handlers,meta,tasks,templates,vars}
- defaults 默認(rèn)尋找路徑
- tasks 存放playbooks路徑
- files 存放文件和腳本露戒,copy模塊文件搜索路徑
- templates 模版存放路徑
- handlers notify調(diào)用部分playbook存放路徑
- vars roles內(nèi)變量存放路徑
4、文件結(jié)構(gòu)
tree /ansbile/
./
├── roles
│ ├── db
│ │ ├── files
│ │ │ └── stu.sql #要導(dǎo)入的sql
│ │ ├── handlers
│ │ ├── meta
│ │ ├── tasks
│ │ │ └── main.yml #創(chuàng)建數(shù)據(jù)庫和導(dǎo)入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 4废洌總調(diào)用文件
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、執(zhí)行安裝 & 檢查
cd /ansible
ansible-playbook web.yml --syntax-check #檢查語法
ansible-playbook web.yml #執(zhí)行
結(jié)束語:
第一次玩Ansible丁屎,感覺比Puppet簡單多了荠锭。雖然在很多做法選擇上并不那么酷(很挫的另一種說法 T_T),Shell成分也相對較多悦屏,不過节沦,作為Ansible的“初探”,也算是把常用功能模塊都體驗了一遍础爬,熟悉了基本玩法,后續(xù)再認(rèn)真全面地學(xué)習(xí)吼鳞。
另:Ansible 大法好看蚜!