为了账号安全,请及时绑定邮箱和手机立即绑定

ansible (2.4.2.0) API python调用重写

标签:
Python

转自 jumpserver 5.0 测试版本  下载地址

https://github.com/hequan2017/zabbix-models/tree/master/ansible_run

只是简单改了一下 能够单独使用。

目录结构

ansible_run/├── callback.py├── exceptions.py├── __init__.py├── inventory.py├── runner.py├── test_inventory.py└── test_runner.py

下面两个是演示文件

先  pip3 install  ansible==2.4.2.0   安装

例子


# -*- coding: utf-8 -*-

from  runner import AdHocRunner, CommandRunner

from  inventory import BaseInventory

def  TestAdHocRunner():

        """

         以yml的形式 执行多个命令

        :return:

        """

        host_data = [

            {

                "hostname": "testserver",

                "ip": "192.168.10.93",

                "port": 22,

                "username": "root",

                "password": "password",

            },

        ]

        inventory = BaseInventory(host_data)

        runner = AdHocRunner(inventory)

        tasks = [

            {"action": {"module": "cron","args": "name=\"sync time\" minute=\"*/3\" job=\"/usr/sbin/ntpdate time.nist.gov &> /dev/null\"" }, "name": "run_cmd"},

            {"action": {"module": "shell", "args": "whoami"}, "name": "run_whoami"},

        ]

        ret = runner.run(tasks, "all")

        print(ret.results_summary)

        print(ret.results_raw)

def TestCommandRunner():

        """

        执行单个命令,返回结果

        :return:

        """

        host_data = [

            {

                "hostname": "testserver",

                "ip": "192.168.10.93",

                "port": 22,

                "username": "root",

                "password": "password",

            },

        ]

        inventory = BaseInventory(host_data)

        runner = CommandRunner(inventory)

        res = runner.execute('pwd', 'all')

        print(res.results_command)

        print(res.results_raw)

        print(res.results_command['testserver']['stdout'])

if __name__ == "__main__":

    TestAdHocRunner()

    TestCommandRunner()




from inventory import BaseInventory

def  Test():

        """

        返回主机信息,组信息,组内主机信息

        :return:

        """

        host_list = [{

            "hostname": "testserver1",

            "ip": "102.1.1.1",

            "port": 22,

            "username": "root",

            "password": "password",

            "private_key": "/tmp/private_key",

            "become": {

                "method": "sudo",

                "user": "root",

                "pass": None,

            },

            "groups": ["group1", "group2"],

            "vars": {"sexy": "yes"},

        }, {

            "hostname": "testserver2",

            "ip": "8.8.8.8",

            "port": 2222,

            "username": "root",

            "password": "password",

            "private_key": "/tmp/private_key",

            "become": {

                "method": "su",

                "user": "root",

                "pass": "123",

            },

            "groups": ["group3", "group4"],

            "vars": {"love": "yes"},

        }]

        inventory = BaseInventory(host_list=host_list)

        print("#"*10 + "Hosts" + "#"*10)

        for host in inventory.hosts:

            print(host)

        print("#" * 10 + "Groups" + "#" * 10)

        for group in inventory.groups:

            print(group)

        print("#" * 10 + "all group hosts" + "#" * 10)

        group = inventory.get_group('all')

        print(group.hosts)

if __name__ == '__main__':

     Test()

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消