ansible安裝應用軟件

1.建立相應的目錄:

mkdir -p /ansible/roles/{nginx,mysql,tomcat,db,zabbix}/{defaults,files,handlers,meta,tasks,templates,vars}java

2 文件結構

[root@MSJTVL-MJSP-A35 etc]# tree ansible/
ansible/
├── ansible.cfg
├── hosts #配置主機相關信息
├── roles
│   ├── db
│   │   ├── defaults
│   │   ├── files
│   │   │   └── stu.sql #要導入的sql
│   │   ├── handlers
│   │   ├── meta
│   │   ├── tasks
│   │   │   └── main.yml #建立數據庫和導入SQL
│   │   ├── templates
│   │   └── vars
│   ├── mysql
│   │   ├── defaults
│   │   ├── files
│   │   │   └── mysql_install.sh #mysql源碼和安裝腳本
│   │   ├── handlers
│   │   ├── meta
│   │   ├── tasks
│   │   │   └── main.yml  #安裝mysql
│   │   ├── templates
│   │   └── vars
│   ├── nginx
│   │   ├── default
│   │   ├── defaults
│   │   ├── files
│   │   │   ├── install_nginx.sh  #nginx安裝腳本
│   │   │   ├── nginx-1.10.0.tar.gz #nginx安裝程序包
│   │   │   └── ngx_cache_purge-2.3.tar.gz
│   │   ├── handlers
│   │   ├── meta
│   │   ├── tasks
│   │   │   └── main.yml #安裝nginx
│   │   ├── templates
│   │   │   └── nginx.conf #nginx配置文件
│   │   └── vars
│   ├── tomcat
│   │   ├── defaults
│   │   ├── files
│   │   ├── handlers
│   │   │   └── main.yml #安裝後處理
│   │   ├── meta
│   │   ├── tasks
│   │   │   └── main.yml #安裝tomcat
│   │   ├── templates
│   │   └── vars
│   └── zabbix
│   ├── defaults
│   ├── files
│   │   ├── install_zabbix.sh  #安裝zabbix客戶端腳本
│   │   ├── zabbix-3.0.7.tar.gz #zabbix安裝包
│   │   └── zabbix_agentd.conf #zabbix客戶端配置文件
│   ├── handlers
│   ├── meta
│   ├── tasks
│   │   └── main.yml  #安裝zabbix
│   ├── templates
│   └── vars
├── web.retry
├── webservice.yml
└── web.yml #總的調用文件python

各目錄功能說明mysql

 

3.解決「Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!」問題:

更新python庫:linux

yum -y install libselinux-pythonnginx

4.playbooks&shell

/etc/ansible/web.ymlc++

- hosts: lzy
  remote_user: root
  roles:
    - nginx
    - zabbix
    - mysql

/etc/ansible/roles/db/tasks/main.ymlweb

---
- 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

/etc/ansible/roles/db/files/stu.sqlsql

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

/etc/ansible/roles/nginx/tasks/main.ymlshell

- name: copy nginx_tar_gz to client
  copy: src=/etc/ansible/roles/nginx/files/nginx-1.10.0.tar.gz dest=/tmp/nginx-1.10.0.tar.gz
- name: copy install_shell to client
  copy: src=/etc/ansible/roles/nginx/files/install_nginx.sh dest=/tmp/install_nginx.sh
- name: copy ngx_cache_purge-2.3.tar.gz to client
  copy: src=/etc/ansible/roles/nginx/files/ngx_cache_purge-2.3.tar.gz dest=/tmp/ngx_cache_purge-2.3.tar.gz
- name: install nginx
  shell: /bin/bash /tmp/install_nginx.sh

/etc/ansible/roles/nginx/files/install_nginx.sh數據庫

#!/bin/bash 

#yum安裝一些依賴的模塊
#yum -y install libselinux-python
yum -y install gcc zlib zlib-devel openssl openssl-devel pcre pcre-devel
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.10.0.tar.gz
tar xf ngx_cache_purge-2.3.tar.gz
cd nginx-1.10.0
mkdir -p /opt/nginx/server/sbin
mkdir -p /opt/nginx/server/lib
mkdir -p /opt/nginx/server/log
mkdir -p /opt/nginx/server/run
mkdir -p /opt/nginx/server/cache
mkdir -p /opt/nginx/server/conf
mkdir -p /opt/nginx/server/lib
mkdir -p /opt/nginx/cache
./configure \
--prefix=/opt/nginx/server \
--sbin-path=/opt/nginx/server/sbin/nginx \
--modules-path=/opt/nginx/server/lib/modules \
--conf-path=/opt/nginx/server/conf/nginx.conf \
--error-log-path=/opt/nginx/server/log/error.log \
--http-log-path=/opt/nginx/server/log/access.log \
--pid-path=/opt/nginx/server/run/nginx.pid \
--lock-path=/opt/nginx/server/run/nginx.lock \
--http-client-body-temp-path=/opt/nginx/server/cache/client_temp \
--http-proxy-temp-path=/opt/nginx/server/cache/proxy_temp \
--http-fastcgi-temp-path=/opt/nginx/server/cache/fastcgi_temp \
--http-uwsgi-temp-path=/opt/nginx/server/cache/uwsgi_temp \
--http-scgi-temp-path=/opt/nginx/server/cache/scgi_temp \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-threads \
--with-stream \
--with-stream_ssl_module \
--with-http_slice_module \
--with-mail \
--with-mail_ssl_module \
--with-file-aio \
--with-http_v2_module \
--with-ipv6 \
--add-module=/tmp/ngx_cache_purge-2.3

make && make install
chown -R nginx:nginx /opt/nginx
#sed  "/^\s*index / i proxy_pass http://localhost:8080;" /etc/nginx/nginx.conf
/opt/nginx/server/sbin/nginx
#sed

/etc/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

etc/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

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

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

etc/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執行安裝&檢查

 

[root@MSJTVL-MJSP-A35 ansible]# ansible-playbook web.yml 

PLAY [lzy] *********************************************************************

TASK [setup] *******************************************************************
ok: [10.0.110.91]
ok: [10.0.110.47]

TASK [zabbix : copy zabbix_tar_gz to client] ***********************************
changed: [10.0.110.47]
changed: [10.0.110.91]

TASK [zabbix : copy install_shell to client] ***********************************
changed: [10.0.110.91]
changed: [10.0.110.47]

TASK [zabbix : copy zabbix_agentd.conf to client] ******************************
changed: [10.0.110.91]
changed: [10.0.110.47]

TASK [zabbix : install zabbix] *************************************************
changed: [10.0.110.47]
changed: [10.0.110.91]

PLAY RECAP *********************************************************************
10.0.110.47                : ok=5    changed=4    unreachable=0    failed=0   
10.0.110.91                : ok=5    changed=4    unreachable=0    failed=0

 

六、常見錯誤

一、出現Error: ansible requires a json module, none found!
SSH password:
192.168.24.15 | FAILED >> {
   "failed": true,
   "msg": "Error: ansible requires a json module, nonefound!",
   "parsed": false
}
解決:python版本太低,要不升級python要不就安裝python-simplejson
二、安裝完成後鏈接客戶端服務器報錯:
FAILED => Using a SSH password insteadof a key is not possible because Host Key checking is enabled and sshpass doesnot support this.  Please add this host'sfingerprint to your known_hosts file to manage this host.
解決:在ansible 服務器上使用ssh 登錄下/etc/ansible/hosts 裏面配置的服務器。而後再次使用ansible 去管理就不會報上面的錯誤了!但這樣大批量登錄就麻煩來。由於默認ansible是使用key驗證的,若是使用密碼登錄的服務器,使用ansible的話,要不修改ansible.cfg配置文件的ask_pass = True給取消註釋,要不就在運行命令時候加上-k,這個意思是-k, --ask-pass ask for SSH password。再修改:host_key_checking= False便可
三、若是客戶端不在know_hosts裏將會報錯
paramiko: The authenticity of host '192.168.24.15'can't be established.
The ssh-rsa key fingerprint is397c139fd4b0d763fcffaee346a4bf6b.
Are you sure you want to continueconnecting (yes/no)?
解決:須要修改ansible.cfg的#host_key_checking= False取消註釋
四、出現FAILED => FAILED: not a valid DSA private key file
解決:須要你在最後添加參數-k
五、openssh升級後沒法登陸報錯
PAM unable todlopen(/lib64/security/pam_stack.so): /lib64/security/pam_stack.so: cannot openshared object
file: No such file or directory
解決:sshrpm 升級後會修改/etc/pam.d/sshd 文件。須要升級前備份此文件最後還原便可登陸。
六、pip安裝完成後,運行ansible報錯:
File "/usr/lib64/python2.6/subprocess.py",line 642, in __init__ errread, errwrite)
解決:安裝:yum installopenssh-clients
七、第一次系統初始化運行生成本機ansible用戶key時報錯
failed: [127.0.0.1] =>{"checksum": "f5f2f20fc0774be961fffb951a50023e31abe920","failed": true}
msg: Aborting, target uses selinux but pythonbindings (libselinux-python) aren't installed!
FATAL: all hosts have already failed –aborting
解決:# yuminstall libselinux-python -y

 7.補充

1.使用ssh-copy-id命令來複制Ansible公鑰到節點中。

    ssh-copy-id -i sm01@10.0.110.47

相關文章
相關標籤/搜索