Пример #1
0
def TestAdHocRunner():
    """
         以yml的形式 执行多个命令
        :return:
        """

    host_data = [
        {
            "hostname": "localhost",
            "ip": "192.168.213.129",
            "port": 22,
            "username": "******",
            "password": "******",
        },
    ]
    res = [{
        'hostsname': '127.0.0.1',
        'ip': '127.0.0.1',
        'username': '******',
        'port': '22',
        'password': '******'
    }]
    #inventory = BaseInventory(host_data)
    runner = AdHocRunner(res)

    #tasks = [
    #    {"action": {"module": "shell", "args": "whoami"}, "name": "run_whoami"},
    #]
    #ret = runner.run('all', 'shell', 'whoami')
    ret = runner.run('all', 'shell', 'w')
    print(ret.results_summary)
    print(ret.results_raw)
Пример #2
0
def ansible_copy(assets, src, dest):
    args = "src=%s dest=%s owner=root group=root" % (src, dest)
    task_tuple = (('copy', args), )
    hoc = AdHocRunner(hosts=assets, forks=10)
    hoc.results_callback = CommandResultCallback()
    ret = hoc.run(task_tuple)
    return ret
Пример #3
0
def TestAdHocRunner():
    """
     以yml的形式 执行多个命令
    :return:
    """

    host_data = [
        {
            "hostname": "server",
            "ip": "192.168.5.224",
            "port": 22,
            "username": "******",
            "password": "",
            "private_key": '/root/.ssh/id_rsa',
            "become": {
                "method": "sudo",
                "user": "******",
                "pass": None,
            }
        },
    ]
    inventory = BaseInventory(host_data)
    runner = AdHocRunner(inventory)

    tasks = [
        {"action": {"module": "shell", "args": "hostname"}, "name": "run_whoami"},
    ]
    ret = runner.run(tasks, "all")
    print(ret.results_summary)
    print(ret.results_raw)
Пример #4
0
 def setUp(self):
     host_data = [
         {
             "hostname": "testserver1",
             "ip": "192.168.33.101",
             "port": 22,
             "username": "******",
             "password": "******",
         },
     ]
     inventory = BaseInventory(host_data)
     self.runner = AdHocRunner(inventory)
Пример #5
0
class TestAdHocRunner(unittest.TestCase):
    def setUp(self):
        host_data = [
            {
                "hostname": "testserver1",
                "ip": "192.168.33.101",
                "port": 22,
                "username": "******",
                "password": "******",
            },
        ]
        inventory = BaseInventory(host_data)
        self.runner = AdHocRunner(inventory)

    def test_run(self):
        tasks = [
            {
                "action": {
                    "module": "shell",
                    "args": "ls"
                },
                "name": "run_cmd"
            },
            {
                "action": {
                    "module": "shell",
                    "args": "whoami"
                },
                "name": "run_whoami"
            },
        ]
        ret = self.runner.run(tasks, "all")
        print(ret.results_summary)
        print(ret.results_raw)
Пример #6
0
def TestAdHocRunner():
    """
         以yml的形式 执行多个命令
        :return:
        """

    host_data = [
        {
            "hostname": "testserver",
            "ip": "192.168.10.93",
            "port": 22,
            "username": "******",
            "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)
Пример #7
0
def TestAdHocRunner():
    """
         以yml的形式 执行多个命令
        :return:
        """

    host_data = [
        {
            "hostname": "server",
            "ip": "192.168.5.224",
            "port": 22,
            "username": "******",
            "password": "",
            "private_key": '/root/.ssh/id_rsa',
            "become": {
                "method": "sudo",
                "user": "******",
                "pass": None,
            }
        },
    ]
    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": "hostname"
            },
            "name": "run_whoami"
        },
    ]
    ret = runner.run(tasks, "all")
    print(ret.results_summary)
    print(ret.results_raw)
Пример #8
0
def TestAdHocRunner():
    """
     以yml的形式 执行多个命令
    :return:
    """
    host_data = [
        {
            "hostname": "10.211.55.13",
            "ip": "10.211.55.13",
            "username": "******",
        },
    ]
    inventory = Inventory(host_data)
    runner = AdHocRunner(inventory)
    # dest = "/opt/mysql/world.sh"

    tasks = [
        {"action": {"module": "shell", "args": "whoami"}, "name": "run_whoami"},
        {"action": {"module": "replace", "args": 'path=/tmp/a.txt regexp="^(192.168.1.1.*)" replace="#\\1"'},
         "name": "down nginx"}
        # {"action": {"module": "shell", "args": "free -m | awk 'NR\=\=2{printf \"%.2f\", $3*100/$2 }'"}, "name": "get_mem_usage"},
        # {"action": {"module": "shell", "args": "df -h | awk '$NF\=\=\"/\"{printf \"%s\", $5}'"}, "name": "get_disk_usage"},
        # {"action": {"module": "copy", "args": "src=/home/python/Desktop/3358.cnf dest=/opt/mysql/my3358.cnf mode=0777"}, "name": "send_file"},
        # {"action": {"module": "copy", "args": "src=/home/python/Desktop/deploy.sh dest=/opt/mysql/deploy.sh mode=0777"}, "name": "send_file"},
        # {"action": {"module": "command", "args": "sh /opt/mysql/hello.sh"}, "name": "execute_file"},
        # {"action": {"module": "shell", "args": "sudo sh /opt/mysql/deploy.sh"}, "name": "execute_file"},
        # {"action": {"module": "lineinfile", "args": "dest=/opt/mysql/hello.sh line=hello1 regexp=echo state=present"}, "name": "modify_file"},
        # {"action": {"module": "lineinfile", "args": "dest=/opt/mysql/world.sh line="" regexp=echo state=present"}, "name": "modify_file"},
        # {"action": {"module": "lineinfile", "args": "dest=%s line=sun regexp=echo state=present" % dest}, "name": "modify_file"},
        # {"action": {"module": "shell", "args": "lineinfile dest=/opt/mysql/hello.sh regexp=hello insertafter=#echo line=hello world"}, "name": "modify_file"},

        # {"action": {"module": "shell", "args": "grep 'cpu ' /proc/stat | awk '{usage\=($2+$4)*100/($2+$4+$5)} END {print usage}'"}, "name": "get_cpu_usage"},
    ]
    ret = runner.run(tasks)
    print(ret.results_summary)
    print(ret.results_raw)
Пример #9
0
from inventory import BaseInventory
from runner import AdHocRunner


host_data = [
    {
        "hostname": "testserver",
        "ip": "10.20.15.105",
        "port": 22,
        "username": "******",
        "password": "******",
    }
]
inventory = BaseInventory(host_data)
runner = AdHocRunner(inventory)
tasks = [
    {"action": {"module": "shell", "args": "ls"}, "name": "run_cmd"},
    {"action": {"module": "shell", "args": "whoami"}, "name": "run_whoami"},
]
ret = runner.run(tasks, "all")
Пример #10
0
def ansible_script(assets, args):
    task_tuple = (('script', args), )
    hoc = AdHocRunner(hosts=assets, forks=10)
    hoc.results_callback = CommandResultCallback()
    ret = hoc.run(task_tuple)
    return ret