Ansible远程执行脚本

Stella981
• 阅读 524

1.05 Ansible远程执行脚本

 首先创建一个shell脚本
 vim  /tmp/test.sh  //加入内容
 #!/bin/bash
 echo `date` > /tmp/ansible_test.txt
 然后把该脚本分发到各个机器上
 ansible testhost -m copy -a "src=/tmp/test.sh dest=/tmp/test.sh mode=0755"
 最后是批量执行该shell脚本
 ansible testhost -m shell -a "/tmp/test.sh"
 shell模块,还支持远程执行命令并且带管道
 ansible testhost -m shell -a "cat /etc/passwd|wc -l "


[root@Dasoncheng ~]# ansible testhost -m copy -a 'src=/tmp/test.sh dest=/tmp/test.sh mode=755'
127.0.0.1 | SUCCESS => {
    "changed": true, 
    "checksum": "1a6e4af02dba1bda6fc8e23031d4447efeba0ade", 
    "dest": "/tmp/test.sh", 
    "gid": 0, 
    "group": "root", 
    "mode": "0755", 
    "owner": "root", 
    "path": "/tmp/test.sh", 
    "size": 48, 
    "state": "file", 
    "uid": 0
}
192.168.60.12 | SUCCESS => {
    "changed": true, 
    "checksum": "1a6e4af02dba1bda6fc8e23031d4447efeba0ade", 
    "dest": "/tmp/test.sh", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "edfaa4371316af8c5ba354e708fe8a97", 
    "mode": "0755", 
    "owner": "root", 
    "size": 48, 
    "src": "/root/.ansible/tmp/ansible-tmp-1524218004.15-84846665116766/source", 
    "state": "file", 
    "uid": 0
}
cdn002 | SUCCESS => {
    "changed": false, 
    "checksum": "1a6e4af02dba1bda6fc8e23031d4447efeba0ade", 
    "dest": "/tmp/test.sh", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "edfaa4371316af8c5ba354e708fe8a97", 
    "mode": "0755", 
    "owner": "root", 
    "size": 48, 
    "src": "/root/.ansible/tmp/ansible-tmp-1524218004.2-83967531119849/source", 
    "state": "file", 
    "uid": 0
}
[root@Dasoncheng ~]# ansible 127.0.0.1,cdn002 -m shell -a '/tmp/test.sh'
cdn002 | SUCCESS | rc=0 >>  
127.0.0.1 | SUCCESS | rc=0 >>
[root@Dasoncheng ~]# ansible cdn002 -m shell -a 'cat /tmp/ansible_test.txt'
cdn002 | SUCCESS | rc=0 >>
Fri Apr 20 17:54:57 CST 2018
[root@Dasoncheng ~]# ansible cdn002 -m shell -a 'cat /etc/passwd | wc -l'
cdn002 | SUCCESS | rc=0 >>
23

1.06 ansible管理任务计划

 ansible testhost -m cron -a "name='test cron' job='/bin/touch /tmp/1212.txt'  weekday=6"

 若要删除该cron 只需要加一个字段 state=absent 
 ansible testhost -m cron -a "name='test cron' state=absent"

 其他的时间表示:分钟 minute 小时 hour 日期 day 月份 month


[root@Dasoncheng ~]# ansible testhost -m cron -a "name='test_cron' job='/bin/touch /tmp/1212.txt' weekday=6"
127.0.0.1 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "test_cron"
    ]
}
192.168.60.12 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "test_cron"
    ]
}
cdn002 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "test_cron"
    ]
}
[root@Dasoncheng ~]# ansible cdn002 -m command -a 'crontab -l'
cdn002 | SUCCESS | rc=0 >>
#Ansible: test_cron
* * * * 6 /bin/touch /tmp/1212.txt

[root@Dasoncheng ~]# ansible cdn002 -m cron -a "name='test_cron' state=absent"
cdn002 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": []
}
[root@Dasoncheng ~]# ansible cdn002 -m command -a 'crontab -l'
cdn002 | SUCCESS | rc=0 >>

1.07 Ansible安装rpm包/管理服务

 ansible testhost -m yum -a "name=httpd" 
 在name后面还可以加上state=installed/removed
 ansible testhost -m service -a "name=httpd state=started enabled=yes" 
 这里的name是centos系统里的服务名,可以通过chkconfig --list查到。
 Ansible文档的使用
 ansible-doc -l   列出所有的模块
 ansible-doc cron  查看指定模块的文档


[root@Dasoncheng ~]# ansible cdn002 -m yum -a "name=httpd state=installed"
cdn002 | SUCCESS => {
    "changed": true, 
    "msg": "", 
    "rc": 0, 
    "results": 
……
……
[root@Dasoncheng ~]# ansible cdn002 -m service -a "name=httpd state=started enabled=yes"
cdn002 | SUCCESS => {
    "changed": true, 
    "enabled": true, 
    "name": "httpd", 
    "state": "started", 
    "status": {
……
……
[root@Dasoncheng ~]# ansible cdn002 -m command -a 'systemctl status httpd'
cdn002 | SUCCESS | rc=0 >>
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2018-04-20 18:37:55 CST; 8s ago
……
……

1.08 Ansible文档的使用

[root@Dasoncheng ~]# ansible-doc -l
a10_server                                           Manage A10 Networks AX/SoftAX/Thunder/vThund...
a10_server                                           Manage A10 Networks AX/SoftAX/Thunder/vThund...
a10_server_axapi3                                    Manage A10 Networks AX/SoftAX/Thunder/vThund...
[root@Dasoncheng ~]# ansible-doc cron
> CRON    (/usr/lib/python2.7/site-packages/ansible/modules/system/cron.py)

        Use this module to manage crontab and environment variables entries. This
        module allows you to create environment variables and named crontab
        entries, update, or delete them. When crontab jobs are managed: the
        module includes one line with the description of the crontab entry
        `"#Ansible: <name>"' corresponding to the "name" passed to the module,
        which is used by future ansible/module calls to find/check the state. The
        "name" parameter should be unique, and changing the "name" value will
        result in a new cron task being created (or a different one being
        removed). When environment variables are managed: no comment line is
        added, but, when the module needs to find/check the state, it uses the
        "name" parameter to find the environment variable definition line. When
        using symbols such as %, they must be properly escaped.

OPTIONS (= is mandatory):

- backup
        If set, create a backup of the crontab before it is modified. The
        location of the backup is returned in the `backup_file' variable by this
        module.
        [Default: no]
        type: bool

- cron_file
        If specified, uses this file instead of an individual user's crontab. If
        this is a relative path, it is interpreted with respect to /etc/cron.d.
        (If it is absolute, it will typically be /etc/crontab). Many linux
        distros expect (and some require) the filename portion to consist solely
        of upper- and lower-case letters, digits, underscores, and hyphens. To
        use the `cron_file' parameter you must specify the `user' as well.
        [Default: (null)]
……
……
点赞
收藏
评论区
推荐文章
blmius blmius
2年前
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
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Stella981 Stella981
2年前
List的Select 和Select().tolist()
List<PersondelpnewList<Person{newPerson{Id1,Name"小明1",Age11,Sign0},newPerson{Id2,Name"小明2",Age12,
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
Java日期时间API系列36
  十二时辰,古代劳动人民把一昼夜划分成十二个时段,每一个时段叫一个时辰。二十四小时和十二时辰对照表:时辰时间24时制子时深夜11:00凌晨01:0023:0001:00丑时上午01:00上午03:0001:0003:00寅时上午03:00上午0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这