python自动化之Ansible常用模块及API(下)

一 get_url模块

1 功能

实现在远程主机下载指定URL到本地,支持sha256sum文件校验。

2 举例

[root@localhost home]# ansible webservers -m get_url -a "url=http://www.baidu.com dest=/tmp/index.html mode=0440 force=yes"
192.168.0.101 | SUCCESS => {
    "changed": true,
    "checksum_dest": null,
    "checksum_src": "f69cad5ec73016242d6743b8ddfc5db2a2298d94",
    "dest": "/tmp/index.html",
    "gid": 0,
    "group": "root",
    "md5sum": "47bb784e4cf23db89df84be789c7dca2",
    "mode": "0440",
    "msg": "OK (unknown bytes)",
    "owner": "root",
    "size": 153785,
    "src": "/tmp/tmplT4PPP",
    "state": "file",
    "status_code": 200,
    "uid": 0,
    "url": "http://www.baidu.com"
}
192.168.0.102 | SUCCESS => {
    "changed": true,
    "checksum_dest": null,
    "checksum_src": "687e74e222416e8afe8ff75c4d393a429b15ecfe",
    "dest": "/tmp/index.html",
    "gid": 0,
    "group": "root",
    "md5sum": "cf1ee21c76e230dd8453e9e67dd0a385",
    "mode": "0440",
    "msg": "OK (unknown bytes)",
    "owner": "root",
    "size": 153103,
    "src": "/tmp/tmpX8vBVT",
    "state": "file",
    "status_code": 200,
    "uid": 0,
    "url": "http://www.baidu.com"
}

二 yum模块

1 功能

Linux平台软件包管理操作,常见有yum、apt管理方式。

2 举例

[root@localhost home]# ansible webservers -m yum -a "name=curl state=latest"

三 cron模块

1 功能

远程主机crontab配置。

2 举例

ansible webservers -m cron -a "name='check dirs' hour='5,2' job='ls -alh > /dev/null'"

四 mount模块

1 功能

远程主机分区挂载。

2 举例

[root@localhost home]# ansible webservers -m mount -a "name=/mnt/data src=/dev/sd0 fstype=ext3 opts=ro state=present"
192.168.0.102 | SUCCESS => {
    "changed": true,
    "dump": "0",
    "fstab": "/etc/fstab",
    "fstype": "ext3",
    "name": "/mnt/data",
    "opts": "ro",
    "passno": "0",
    "src": "/dev/sd0"
}
192.168.0.101 | SUCCESS => {
    "changed": true,
    "dump": "0",
    "fstab": "/etc/fstab",
    "fstype": "ext3",
    "name": "/mnt/data",
    "opts": "ro",
    "passno": "0",
    "src": "/dev/sd0"
}

五 service模块

1 功能

远程主机系统服务管理。

2 举例

[root@localhost home]# ansible webservers -m service -a "name=nginx state=stopped"
192.168.0.102 | FAILED! => {
    "changed": false,
    "msg": "Could not find the requested service nginx: host"
}
192.168.0.101 | SUCCESS => {
    "changed": true,
    "name": "nginx",
    "state": "stopped",
    "status": {
        "ActiveEnterTimestamp": "Sat 2019-03-02 19:35:18 CST",
        "ActiveEnterTimestampMonotonic": "97415681",
        "ActiveExitTimestampMonotonic": "0",
        "ActiveState": "active",
......
[root@localhost home]# ansible webservers -m service -a "name=nginx state=restarted"
192.168.0.102 | FAILED! => {
    "changed": false,
    "msg": "Could not find the requested service nginx: host"
}
192.168.0.101 | SUCCESS => {
    "changed": true,
    "name": "nginx",
    "state": "started",
......
[root@localhost home]# ansible webservers -m service -a "name=nginx state=reloaded"
192.168.0.102 | FAILED! => {
    "changed": false,
    "msg": "Could not find the requested service nginx: host"
}
192.168.0.101 | SUCCESS => {
    "changed": true,
    "name": "nginx",
    "state": "started",
    "status": {
        "ActiveEnterTimestamp": "Sat 2019-03-02 21:39:44 CST",
        "ActiveEnterTimestampMonotonic": "7563490031",
        "ActiveExitTimestamp": "Sat 2019-03-02 21:38:20 CST",
        "ActiveExitTimestampMonotonic": "7480052579",
        "ActiveState": "active",
......

六 user服务模块

1 功能

远程主机系统用户管理。

2 举例

#添加用户johnd; 

#添加用户johnd; 
[root@localhost home]# ansible webservers -m user -a "name=johnd comment='John Doe'"
192.168.0.102 | SUCCESS => {
    "changed": true,
    "comment": "John Doe",
    "createhome": true,
    "group": 1011,
    "home": "/home/johnd",
    "name": "johnd",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 1006
}
192.168.0.101 | SUCCESS => {
    "changed": true,
    "comment": "John Doe",
    "createhome": true,
    "group": 1011,
    "home": "/home/johnd",
    "name": "johnd",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 1006
}
#删除用户johnd; 
ansible webservers -m user -a "name=johnd state=absent remove=yes"
[root@localhost home]# ansible webservers -m user -a "name=johnd state=absent remove=yes"
192.168.0.102 | SUCCESS => {
    "changed": true,
    "force": false,
    "name": "johnd",
    "remove": true,
    "state": "absent"
}
192.168.0.101 | SUCCESS => {
    "changed": true,
    "force": false,
    "name": "johnd",
    "remove": true,
    "state": "absent"
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值