1.创建相应的目录:

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

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 #总的调用文件

各目录功能说明

3.解决“Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!”问题:

更新python库:

yum -y install libselinux-python

4.playbooks&shell

/etc/ansible/web.yml

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

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

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

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

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

- 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

6、常见错误

1、出现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
2、安装完成后连接客户端服务器报错:
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即可
3、如果客户端不在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取消注释
4、出现FAILED => FAILED: not a valid DSA private key file
解决:需要你在最后添加参数-k
5、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 文件。需要升级前备份此文件最后还原即可登录。
6、pip安装完成后,运行ansible报错:
File "/usr/lib64/python2.6/subprocess.py",line 642, in __init__ errread, errwrite)
解决:安装:yum installopenssh-clients
7、第一次系统初始化运行生成本机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

 

ansible安装应用软件的更多相关文章

  1. 初探ansible安装

    一.ansible介绍常用的自动化运维工具 Puppet —基于 Ruby 开发,采用 C/S 架构,扩展性强,基于 SSL,远程命令执行相对较弱SaltStack —基于 Python 开发,采用 ...

  2. Ansible安装配置Nginx

    一.思路 现在一台机器上编译安装好nginx.打包,然后在用ansible去下发 cd /etc/ansible 进入ansible配置文件目录 mkdir roles/{common,install ...

  3. Ansible安装配置及使用

    一.Ansible特点 1.不需要安装客户端,通过sshd通信 2.基于模块工作,模块可以由任何序言开发 3.不仅支持命令行使用模块,也支持编写yaml格式的playbook 4.支持sudo 5.有 ...

  4. Ansible安装配置

    Ansible工具的安装与配置 Ansible基于SSH,不需要在远程端安装任何软件,只需要在管理端安装ansible及其组件即可. Ansible使用前提是已配置ssh密钥免登陆. 一.安装组件: ...

  5. ansible安装二进制kubernetes-1.14.1

    主机信息: 主机IP 主机名 角色 10.10.3.181 k8s-m1  kube-apiserver,kube-controller-manager,kube-scheduler,etcd 10. ...

  6. Ansible安装部署以及常用模块详解

    一.  Ansible 介绍Ansible是一个配置管理系统configuration management system, python 语言是运维人员必须会的语言, ansible 是一个基于py ...

  7. 内网环境使用ansible安装software 需要外网时,如何绑定代理呢

    内网环境使用ansible安装software 需要外网时,如何绑定代理呢? 方法一: 在ansible 的脚本里,yum install 的地方,添加语句: environment: https_p ...

  8. Ansible 安装与配置(一)

    公司大概有200多云主机需要进行管理,但是如果通过手工管理费时还累最终结果也容易出错,所以考虑通过自动化的方式来管理云主机,目前开源的自动化工具,大家用的比较多的有Ansible和Saltstack这 ...

  9. Ansible安装及OS规划

    Ansible安装  1.以管理用户mtnsadmin连接服务器后下载安装包(-O表示将下载的文件存放到指定的文件夹下,同时重命名下载的文件)     sudo wget -O /etc/yum.re ...

随机推荐

  1. hexo-theme-next

    Hexo Next github主页:https://github.com/iissnan/hexo-theme-next 官网地址:http://theme-next.iissnan.com/ 一篇 ...

  2. ELINK离线编程器常见问题

    Q1 编程器是否可以接JTAG JTAG接口已经包含SWD接口引脚,按以下引脚对应接线即可: SWDIO->目标板JTAG 的JTMS SWCLK->目标板JTAG 的JTCK Q2 PC ...

  3. WPF 控件 深度克隆

    原文:WPF 控件 深度克隆 http://social.msdn.microsoft.com/Forums/zh-SG/wpfzhchs/thread/e5c87129-966a-4d51-a934 ...

  4. SQL Server Update:使用 TOP 限制更新的数据

    原文 使用 TOP 限制更新的数据 可以使用 TOP 子句来限制 UPDATE 语句中修改的行数.当 TOP (n) 子句与 UPDATE 一起使用时,将针对随机选择的 n 行执行删除操作.例如,假设 ...

  5. Android零基础入门第65节:RecyclerView分割线开发技巧

    在上一期通过简单学习,已经领略到了RecyclerView的灵活性,当然都是一些最基础的用法,那么本期一起来学习RecyclerView的分割线使用. 相信有的比较细心的同学已经发现了,使用Recyc ...

  6. Tensorflow中循环神经网络及其Wrappers

    tf.nn.rnn_cell.LSTMCell 又名:tf.nn.rnn_cell.BasicLSTMCell.tf.contrib.rnn.LSTMCell 参见: tf.nn.rnn_cell.L ...

  7. delphi控件安装(安装ODAC、TeeChart、TServerSocket、TWSocketServer、TComm)

    一.oracle插件安装delphi7如何安装oracle access控件 假设ODAC主目录在 D:\dzj\odac Delphi7主目录在 D:\Program Files\Borland\D ...

  8. Qt5下OpenGL程序的新写法

    在Qt5中,引入了QOpenGL*系列类,以取代Qt4时代的QGL*系列类. 下面是从Qt5例子中抠出的两种OpenGL程序模板,方便参考. 第一种写法: #ifndef TRIANGLEWINDOW ...

  9. linux oracle 启动全过程

    一:启动oracle [root@ccoracle ~]# su -l oracle [oracle@ccoracle ~]$ sqlplus /nolog SQL*Plus: Release 10. ...

  10. Spark之权威指南经典案例

    hadoop权威指南上有一个求历史最高温度的经典案例,源数据如下: -- sample.txt0067011990999991950051507004+68750+023550FM-12+038299 ...