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
    评论
Ansible 是一种自动化运维工具,可以用于管理和配置多个计算机系统。它使用 Python 编写,并且提供了一个简单易用的 DSL(Domain Specific Language)来描述系统配置和部署任务。使用 Ansible,可以通过编写 Playbooks 来定义系统配置和任务,然后通过 SSH 协议远程执行这些任务。 在 Python 中使用 Ansible,可以使用 AnsiblePython API 来编写自动化脚本。这个 API 提供了一系列的模块和方法,可以用于远程执行命令、复制文件、安装软件等操作。你可以通过在 Python 脚本中导入 Ansible 的相关模块,然后调用相应的方法来完成自动化运维任务。 以下是一个简单的示例代码,演示了如何使用 AnsiblePython API 来执行远程命令: ```python from ansible import context from ansible.playbook import Playbook from ansible.executor.playbook_executor import PlaybookExecutor # 设置 Ansible 的上下文 context.CLIARGS = { 'listtags': False, 'listtasks': False, 'listhosts': False, 'syntax': False, 'connection': 'ssh', 'module_path': None, 'forks': 100, 'remote_user': 'root', 'private_key_file': None, 'ssh_common_args': None, 'ssh_extra_args': None, 'sftp_extra_args': None, 'scp_extra_args': None, 'become': None, 'become_method': None, 'become_user': None, 'verbosity': None, 'check': False, 'start_at_task': None, } # 定义 Playbook 的路径和主机列表 playbook_path = '/path/to/playbook.yaml' host_list = '/path/to/hosts' # 创建 Playbook 对象和执行器 playbook = Playbook.load(playbook_path) executor = PlaybookExecutor( playbooks=[playbook], inventory=host_list, variable_manager=playbook._variable_manager, loader=playbook._loader, ) # 执行 Playbook executor.run() ``` 上面的代码中,我们首先设置了 Ansible 的上下文,然后定义了 Playbook 的路径和主机列表。接着创建了 Playbook 对象和执行器,并最终执行了 Playbook。 需要注意的是,上述示例代码中的相关路径和参数需要根据实际情况进行修改。另外,为了运行该示例代码,你需要安装 ansible-python 包。可以使用 pip 命令进行安装: ``` pip install ansible ``` 希望这个示例能帮助到你开始使用 Ansible 进行自动化运维的 Python 编程。如果有更多问题,请随时提问!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值