1、安装部署
yum inatsll ansible -y
2、配置文件
Ansible常用参数详解
[defaults] #通用默认配置
inventory = /etc/ansible/hosts #被控制端IP或者DNS列表
library = /usr/share/my_modules/ ##默认搜寻模块的位置
remote_tmp = ~/.ansible/tmp #远程执行临时文件
local_tmp = ~/.ansible/tmp
plugin_filters_cfg = /etc/ansible/plugin_filters.yml
forks = 5 ##并行线程数
poll_interval = 15 ##回频率或轮询间隔时间
sudo_user = root ##sudo远程执行用户名
ask_sudo_pass = True ##使用sudo,是否需要输入密码
ask_pass = True ##是否需要输入密码
transport = smart ##通信机制
remote_port = 22 ##远程SSH端口
module_lang = C ##模块和系统之间通信的语言
module_set_locale = False
gathering = implicit ##控制默认facts收集(远程系统变量)
gather_subset = all
gather_timeout = 10
roles_path = /etc/ansible/roles ##使用playbook搜索Ansible roles
host_key_checking = False ##是否检查远程主机密钥
sudo_exe = sudo ##sudo远程执行命令
sudo_flags = -H -S -n ##传递sudo之外的参数
timeout = 10 ##SSH超时时间
remote_user = root ##远程登录用户名
log_path = /var/log/ansible.log ##日志文件存放路径
module_name = command ##Ansible命令默认执行的模块
executable = /bin/sh ##执行的shell环境,用户shell模块
hash_behaviour = replace ##特定的优先级覆盖变量
jinja2_extensions = jinja2.ext.do,jinja2.ext.i18 ##允许开启jinja2扩展模块
private_key_file = /path/to/file ##私钥文件存储位置
display_skipped_hosts = True ##显示跳过任何任务的状态
system_warnings = True ##禁用系统运行Ansible潜在问题警告
deprecation_warnings = True ##PlayBook输出禁用“不建议使用”警告
command_warnings = False ##command模块Ansible默认发出警告
nocolor = 1 ##输出带上颜色区别,0表示开启,1表示关闭
pipelining = False ##开启pipe SSH通道优化
[accelerate] ##accelerate缓存加速
accelerate_port = 5099 ##加速连接端口5099
accelerate_timeout = 30 ##命令执行超过时间,单位为s
accelerate_connect_timeout = 5.0 ##上一个活动连接的时间,单位为min
accelerate_daemon_timeout = 30 ##允许多个私钥被加载到daemon
accelerate_multi_key = yes ##任何客户端想要连接daemon都要开启这个选项
简单配置
配置ansible.cfg 文件
[root@localhost ansible]# cat ansible.cfg
[defaults]
inventory = /root/ansible/inventory
remote_user = root
roles_path = /root/ansible/roles
[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False
配置 inventory文件
[root@localhost ansible]# cat inventory
goods-service
[sy-test]
sy-test-service
配置hosts文件
[root@localhost ansible]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 walle.china95059.com
192.168.1.5 sy-test-service
配置执行文件sy-test.yml
---
- hosts: sy-test
tasks:
- name: uat-back
shell: mv /home/project/shangye/uat-prj-shangye-biz-app.jar /home/project/shangye/uat-prj-shangye-biz-app.jar-20210120.bak
- name: scp
copy:
src: /opt/shangye/uat-prj-shangye-biz-app.jar
dest: /home/project/shangye/uat-prj-shangye-biz-app.jar
- name: restart service
shell: source /etc/profile; cd /home/project/shangye; source ~/.bash_profile; sh 902.验收商业app.sh nohup
- name: ust the new website
shell: /opt/sy/test.sh
编写服务启动检查脚本
#!/bin/bash
i=0
host_ip=`ifconfig | grep "\<inet\>" | grep -v 127.0.0.1 | head -1 | awk '{print $2}'` #截取主机ip
while ((i <= 8))
do
curl http://$host_ip:8001/app/deployTest -D /opt/sh/curl.txt &> /dev/null #访问tomcat网站,并将头部信息存放在指定文件中
num=`head -1 /opt/sh/curl.txt| cut -d' ' -f2` #截取网站响应码
if [[ "$num" == "200" ]];then #判断响应码是否正常
exit 0 #正常返回0
fi
sleep 15
((i++))
done
exit 1
运行命令ansible-playbook sy-test.yml
注意:ansibl机器到目标机器一定要免密登录