ansible同步/etc/crontab里面的脚本

简介:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import  ansible.runner
from  ansible.color  import  stringc
import  sys
import  socket
import  re
import  subprocess
 
host_list  =  'qa_servers.txt'
private_key_file  =  '/root/.ssh/id_rsa'
pattern  =  '*'
forks  =  10
timeout  =  30
module_name  =  'script'
module_args  =  'test.sh'
fp  =  open ( '/home/ip.txt' "w+" )
# construct the ansible runner and execute on all hosts
results  =  ansible.runner.Runner(
     host_list = host_list,
     private_key_file = private_key_file,
     pattern = pattern,
     forks = forks,
     timeout = timeout,
     module_name = module_name,
     module_args = module_args
).run()
# print results
if  results  is  None :
     print  "No hosts found"
     sys.exit( 1 )
#print results
for  (hostname, result)  in  results[ 'contacted' ].items():
     if  not  'failed'  in  result:
         ip  =  result[ 'stdout' ].strip()
         if  ip ! =  "":
             output  =  open ( '/home/ip.txt' 'a' )
             output.write(ip)
             output.write( '\n' )
fp.close()
1
2
3
4
5
6
7
cat test.sh
#!/bin/bash
#if [ `ps -ef|grep tomcat|grep /opt|wc -l` -gt 0 ];then
if  [ `cat  / etc / crontab|grep .sh|grep  - v SHELL|awk  '{print $8}' |uniq|wc  - l`  - gt  0  ];then
echo `ifconfig|grep  'inet ' |grep  - '127.0' |xargs|awk  - '[ :]'  '{print $3}' `
echo `cat  / etc / crontab|grep .sh|grep  - v SHELL|awk  '{print $8}' |uniq`
fi
1
2
3
cat qa_servers.txt
ip1
ip2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# !/usr/bin/env python
# coding:utf-8
import  os,sys,re
sfile = "/home/ip.txt"
with  open (sfile, 'r' ) as f:
     rlist = f.readlines()
     # print rlist
     llen = len (rlist)
     for  in  range (llen  -  1 ):
         if  i % 2 = = 0 :
             ip  =  rlist[i].strip()
             print  "ansible -i /etc/ansible/hosts %s -m shell -a 'mkdir -p /home/run/%s'"  % (ip,ip)
             command  =  rlist[i  +  1 ].strip()
             comlist  =  command.split( ' ' )
             for  in  comlist:
                 #print j
                 print  "ansible -i /etc/ansible/hosts %s -m synchronize -a 'mode=pull src=%s dest=/home/run/%s/'" \
                 % (ip,j,ip)

自动生成

1
2
ansible  - / etc / ansible / hosts ip1  - m shell  - 'mkdir -p /home/run/ip1'
ansible  - / etc / ansible / hosts ip1  - m synchronize  - 'mode=pull src=/home/jenkins.sh dest=/home/run/ip1/'

需要两个python脚本,生成最后的脚本,执行就可以同步各个客户端中/etc/crontab的脚本。


一个简单而笨的思路。




本文转自 liqius 51CTO博客,原文链接:http://blog.51cto.com/szgb17/1925140,如需转载请自行联系原作者

相关文章
|
Linux 开发工具
centos7脚本离线化安装CDH6.3版本(ansible闪亮登场~~~)(二)
centos7脚本离线化安装CDH6.3版本(ansible闪亮登场~~~)
202 0
centos7脚本离线化安装CDH6.3版本(ansible闪亮登场~~~)(二)
|
5月前
|
Shell API Docker
ansible api执行远程脚本结果中文乱码报错
ansible api执行远程脚本结果中文乱码报错
|
5月前
|
Linux Shell
suse 12 sp3 利用shell脚本离线编译安装ansible
suse 12 sp3 利用shell脚本离线编译安装ansible
49 0
|
11月前
|
NoSQL Shell Redis
ansible--一键部署redis5.0.5集群及全套脚本
ansible--一键部署redis5.0.5集群及全套脚本
|
关系型数据库 MySQL 大数据
centos7脚本离线化安装CDH6.3版本(ansible闪亮登场~~~)(一)
centos7脚本离线化安装CDH6.3版本(ansible闪亮登场~~~)
586 0
centos7脚本离线化安装CDH6.3版本(ansible闪亮登场~~~)(一)
|
Shell Linux
Ansible复制文件copy和执行脚本shell(学习笔记二)
用途 系统 IP 服务端 Linux7.2 52.244 客户端 Linux7.2 52.245 客户端 Linux7.2 52.246 客户端 Linux7.
1761 0
|
Shell 网络安全 数据安全/隐私保护
Ansible 安装脚本
#!/bin/bash #su - chmod 777 /usr/local/src -r; yum -y install gcc gcc++; yum install -y zlib zlib-devel; cd /usr/local/src wget https://www.
1057 0
|
Python
Ansible安装脚本
#!/bin/bash yum -y installgcc zlib zlib-devel openssl sshpass libselinux libselinux-python wget; cd/usr/local/src wget -c https://www.
1180 0