Ansible远程执行脚本

1.05 Ansible远程执行脚本

1 首先创建一个shell脚本 2 vim  /tmp/test.sh  //加入内容 3 #!/bin/bash 4 echo `date` > /tmp/ansible_test.txt 5 然后把该脚本分发到各个机器上 6 ansible testhost -m copy -a "src=/tmp/test.sh dest=/tmp/test.sh mode=0755" 7 最后是批量执行该shell脚本 8 ansible testhost -m shell -a "/tmp/test.sh" 9 shell模块,还支持远程执行命令并且带管道 10 ansible testhost -m shell -"cat /etc/passwd|wc -l " 11 12 13[root@Dasoncheng ~]# ansible testhost -m copy -a 'src=/tmp/test.sh dest=/tmp/test.sh mode=755' 14127.0.0.1 | SUCCESS => { 15 "changed": true, 16 "checksum": "1a6e4af02dba1bda6fc8e23031d4447efeba0ade", 17 "dest": "/tmp/test.sh", 18 "gid": 0, 19 "group": "root", 20 "mode": "0755", 21 "owner": "root", 22 "path": "/tmp/test.sh", 23 "size": 48, 24 "state": "file", 25 "uid": 0 26} 27192.168.60.12 | SUCCESS => { 28 "changed": true, 29 "checksum": "1a6e4af02dba1bda6fc8e23031d4447efeba0ade", 30 "dest": "/tmp/test.sh", 31 "gid": 0, 32 "group": "root", 33 "md5sum": "edfaa4371316af8c5ba354e708fe8a97", 34 "mode": "0755", 35 "owner": "root", 36 "size": 48, 37 "src": "/root/.ansible/tmp/ansible-tmp-1524218004.15-84846665116766/source", 38 "state": "file", 39 "uid": 0 40} 41cdn002 | SUCCESS => { 42 "changed": false, 43 "checksum": "1a6e4af02dba1bda6fc8e23031d4447efeba0ade", 44 "dest": "/tmp/test.sh", 45 "gid": 0, 46 "group": "root", 47 "md5sum": "edfaa4371316af8c5ba354e708fe8a97", 48 "mode": "0755", 49 "owner": "root", 50 "size": 48, 51 "src": "/root/.ansible/tmp/ansible-tmp-1524218004.2-83967531119849/source", 52 "state": "file", 53 "uid": 0 54} 55[root@Dasoncheng ~]# ansible 127.0.0.1,cdn002 -m shell -a '/tmp/test.sh' 56cdn002 | SUCCESS | rc=0 >> 57127.0.0.1 | SUCCESS | rc=0 >> 58[root@Dasoncheng ~]# ansible cdn002 -m shell -a 'cat /tmp/ansible_test.txt' 59cdn002 | SUCCESS | rc=0 >> 60Fri Apr 20 17:54:57 CST 2018 61[root@Dasoncheng ~]# ansible cdn002 -m shell -a 'cat /etc/passwd | wc -l' 62cdn002 | SUCCESS | rc=0 >> 6323

1.06 ansible管理任务计划

1 ansible testhost -m cron -a "name='test cron' job='/bin/touch /tmp/1212.txt'  weekday=6" 2 3 若要删除该cron 只需要加一个字段 state=absent  4 ansible testhost -m cron -a "name='test cron' state=absent" 5 6 其他的时间表示:分钟 minute 小时 hour 日期 day 月份 month 7 8 9[root@Dasoncheng ~]# ansible testhost -m cron -a "name='test_cron' job='/bin/touch /tmp/1212.txt' weekday=6" 10127.0.0.1 | SUCCESS => { 11 "changed": true, 12 "envs": [], 13 "jobs": [ 14 "test_cron" 15 ] 16} 17192.168.60.12 | SUCCESS => { 18 "changed": true, 19 "envs": [], 20 "jobs": [ 21 "test_cron" 22 ] 23} 24cdn002 | SUCCESS => { 25 "changed": true, 26 "envs": [], 27 "jobs": [ 28 "test_cron" 29 ] 30} 31[root@Dasoncheng ~]# ansible cdn002 -m command -a 'crontab -l' 32cdn002 | SUCCESS | rc=0 >> 33#Ansible: test_cron 34* * * * 6 /bin/touch /tmp/1212.txt 35 36[root@Dasoncheng ~]# ansible cdn002 -m cron -a "name='test_cron' state=absent" 37cdn002 | SUCCESS => { 38 "changed": true, 39 "envs": [], 40 "jobs": [] 41} 42[root@Dasoncheng ~]# ansible cdn002 -m command -a 'crontab -l' 43cdn002 | SUCCESS | rc=0 >>

1.07 Ansible安装rpm包/管理服务

1 ansible testhost -m yum -a "name=httpd" 2 在name后面还可以加上state=installed/removed 3 ansible testhost -m service -a "name=httpd state=started enabled=yes" 4 这里的name是centos系统里的服务名,可以通过chkconfig --list查到。 5 Ansible文档的使用 6 ansible-doc -l   列出所有的模块 7 ansible-doc cron  查看指定模块的文档 8 9 10[root@Dasoncheng ~]# ansible cdn002 -m yum -a "name=httpd state=installed" 11cdn002 | SUCCESS => { 12 "changed": true, 13 "msg": "", 14 "rc": 0, 15 "results": 16…… 17…… 18[root@Dasoncheng ~]# ansible cdn002 -m service -a "name=httpd state=started enabled=yes" 19cdn002 | SUCCESS => { 20 "changed": true, 21 "enabled": true, 22 "name": "httpd", 23 "state": "started", 24 "status": { 25…… 26…… 27[root@Dasoncheng ~]# ansible cdn002 -m command -a 'systemctl status httpd' 28cdn002 | SUCCESS | rc=0 >> 29● httpd.service - The Apache HTTP Server 30 Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled) 31 Active: active (running) since Fri 2018-04-20 18:37:55 CST; 8s ago 32…… 33……

1.08 Ansible文档的使用

1[root@Dasoncheng ~]# ansible-doc -l 2a10_server Manage A10 Networks AX/SoftAX/Thunder/vThund... 3a10_server Manage A10 Networks AX/SoftAX/Thunder/vThund... 4a10_server_axapi3 Manage A10 Networks AX/SoftAX/Thunder/vThund... 5[root@Dasoncheng ~]# ansible-doc cron 6> CRON (/usr/lib/python2.7/site-packages/ansible/modules/system/cron.py) 7 8 Use this module to manage crontab and environment variables entries. This 9 module allows you to create environment variables and named crontab 10 entries, update, or delete them. When crontab jobs are managed: the 11 module includes one line with the description of the crontab entry 12 `"#Ansible: <name>"' corresponding to the "name" passed to the module, 13 which is used by future ansible/module calls to find/check the state. The 14 "name" parameter should be unique, and changing the "name" value will 15 result in a new cron task being created (or a different one being 16 removed). When environment variables are managed: no comment line is 17 added, but, when the module needs to find/check the state, it uses the 18 "name" parameter to find the environment variable definition line. When 19 using symbols such as %, they must be properly escaped. 20 21OPTIONS (= is mandatory): 22 23- backup 24 If set, create a backup of the crontab before it is modified. The 25 location of the backup is returned in the `backup_file' variable by this 26 module. 27 [Default: no] 28 type: bool 29 30- cron_file 31 If specified, uses this file instead of an individual user's crontab. If 32 this is a relative path, it is interpreted with respect to /etc/cron.d. 33 (If it is absolute, it will typically be /etc/crontab). Many linux 34 distros expect (and some require) the filename portion to consist solely 35 of upper- and lower-case letters, digits, underscores, and hyphens. To 36 use the `cron_file' parameter you must specify the `user' as well. 37 [Default: (null)] 38…… 39……
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )