ansible安装应用软件

本文介绍了一种使用Ansible进行自动化部署的方法,包括创建角色、配置文件结构、解决常见问题等,并通过具体示例展示了如何安装配置nginx、mysql、tomcat及zabbix等组件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

转载于:https://www.cnblogs.com/tian880820/p/6672696.html

<think>好的,我现在需要帮助用户了解如何使用Ansible安装软件。根据用户的问题,他们可能对Ansible的基础不太熟悉,所以需要从基本步骤开始解释。用户之前提到过prod.ini文件,这可能意味着他们已经有一些清单配置的基础,但需要具体的模块使用方法。 首先,我应该回顾Ansible中用于软件包管理的模块,比如yum、apt、pip等。用户可能是在不同的Linux发行版上操作,所以需要区分不同的包管理工具。需要提到常用模块以及它们的参数,比如name、state等,并给出示例。 然后,用户可能需要一个完整的示例,包括主机清单和Playbook的编写。根据之前回答中的prod.ini示例,可以引用那个结构来展示如何组织主机和组,并应用对应的变量。此外,结合用户提供的引用,特别是引用[4]中提到批量安装PostgreSQL的例子,可以展示一个实际应用的场景。 接下来,需要强调最佳实践,比如使用变量、处理不同环境、幂等性等。用户可能关心如何高效且安全地管理多台主机的软件安装,所以这些实践建议会很实用。同时,用户的问题可能涉及到如何验证安装是否成功,所以需要包含验证步骤,比如使用ansible命令执行模块检查。 另外,用户引用的资料中提到了ansible-doc的使用,可以建议用户使用这个命令查看模块文档,以便更深入地了解模块参数和用法。这有助于用户自主解决问题,提升他们的Ansible技能。 最后,要确保回答结构清晰,分步骤说明,并给出相关问题的建议,帮助用户扩展知识面。可能的相关问题包括如何处理依赖、特定软件的最佳模块选择、多环境配置等,这些都需要在最后部分列出,引导用户进一步学习。</think>### 使用 Ansible 安装软件的完整教程 Ansible 通过**软件包管理模块**(如 `yum`、`apt`、`pip` 等)实现跨平台软件安装。以下是详细步骤与示例: --- #### 一、核心模块与语法 1. **通用参数** - `name`: 指定软件包名称 - `state`: 控制安装状态 - `present`/`latest`: 安装或更新 - `absent`: 卸载 2. **常用模块** - **`yum`**(RHEL/CentOS) ```yaml - name: Install Nginx via yum yum: name: nginx state: latest ``` - **`apt`**(Debian/Ubuntu) ```yaml - name: Install Nginx via apt apt: name: nginx state: present update_cache: yes # 等效于 apt-get update ``` - **`pip`**(Python 包) ```yaml - name: Install requests via pip pip: name: requests state: latest ``` - **`dnf`**(Fedora/RHEL8+) ```yaml - name: Install Git via dnf dnf: name: git state: present ``` --- #### 二、完整安装示例(以 PostgreSQL 为例[^4]) 1. **准备主机清单 `prod.ini`** ```ini [database] db01 ansible_host=192.168.1.20 db02 ansible_host=192.168.1.21 [database:vars] ansible_user=admin postgresql_version=14 # 定义变量 ``` 2. **编写 Playbook `install_postgresql.yml`** ```yaml --- - hosts: database become: yes # 提权为 root tasks: - name: Install EPEL Repository (CentOS) yum: name: epel-release state: present when: ansible_os_family == "RedHat" - name: Install PostgreSQL package: # 通用模块,自动适配 apt/yum name: "postgresql-{{ postgresql_version }}" state: present - name: Start and Enable PostgreSQL Service service: name: postgresql enabled: yes state: started ``` 3. **执行安装** ```bash ansible-playbook -i prod.ini install_postgresql.yml ``` --- #### 三、验证安装结果 1. **检查软件包状态** ```bash ansible database -i prod.ini -m shell -a "rpm -q postgresql-14 || dpkg -l | grep postgresql" ``` 2. **验证服务运行状态** ```bash ansible database -i prod.ini -m systemd -a "name=postgresql state=started" ``` --- #### 四、最佳实践 1. **变量分层管理** - 将软件版本等参数定义在 `group_vars/database.yml` 中,与 Playbook 解耦: ```yaml postgresql_version: 14 ``` 2. **幂等性设计** - 使用 `state: present` 而非直接执行 `yum install`,避免重复安装冲突[^1]。 3. **多环境适配** - 通过 `when` 条件判断操作系统类型(如 `ansible_os_family`),实现跨平台兼容: ```yaml - name: Install Python3 for Debian apt: name: python3 state: present when: ansible_os_family == "Debian" ``` --- #### 五、高级技巧 1. **批量安装指定版本软件** ```yaml - name: Install specific version of Docker yum: name: "docker-ce-{{ docker_version }}" state: present vars: docker_version: "20.10.7-3.el7" ``` 2. **处理依赖关系** 使用 `meta` 模块触发依赖解析: ```yaml - name: Resolve package dependencies meta: flush_handlers ``` --- ### 相关问题 1. 如何通过 Ansible 安装指定版本的 Python 包? 2. Ansible 的 `package` 模块与 `yum`/`apt` 模块有何区别?[^1] 3. 如何在 Playbook 中处理不同 Linux 发行版的软件包差异?[^4] 4. 如何通过 Ansible 卸载已安装软件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值