【ansible】playbook api 2.0 运行项目
上篇 api 的文章 《ansible-playbook api 2.0 直接运行》 介绍的是直接将 tasks 直接写在 代码中的,本文介绍 api 运行整个项目
[root@10_1_162_39 host_vars]# cat test1.yaml --- - hosts: 10.1.167.36 gather_facts: false tasks: - shell: ifconfig register: output - debug: var=output.stdout
hosts 可以配置密码,暂时不知道怎么在代码 host_list 中配置不同的密码
root@10_1_162_39 host_vars]# cat hosts [web] 10.1.162.18:322 10.1.167.36:32200 ansible_ssh_port=32200 ansible_ssh_pass=test***
test_api_2.py 如下
#!/bin/env python # -*- coding:utf8 -*- from collections import namedtuple from ansible.parsing.dataloader import DataLoader from ansible.vars import VariableManager from ansible.inventory import Inventory from ansible.executor.playbook_executor import PlaybookExecutor loader = DataLoader() variable_manager = VariableManager() inventory = Inventory(loader=loader, variable_manager=variable_manager,host_list='hosts') variable_manager.set_inventory(inventory) passwords=None Options = namedtuple('Options', ['connection', 'remote_user', 'ask_sudo_pass', 'verbosity', 'ack_pass', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check', 'listhosts', 'listtasks', 'listtags', 'syntax', 'sudo_user', 'sudo']) options = Options(connection='smart', remote_user='root', ack_pass=None, sudo_user='root', forks=100, sudo='yes', ask_sudo_pass=False, verbosity=5, module_path=None, become=True, become_method='sudo', become_user='root', check=None, listhosts=None, listtasks=None, listtags=None, syntax=None) # 多个yaml文件则以列表形式 playbook = PlaybookExecutor(playbooks=['test1.yaml'],inventory=inventory, variable_manager=variable_manager, loader=loader,options=options,passwords=passwords) result = playbook.run() print result
playbook_executor 是不能自定义 callback的,所以输出是标准输出,result 是返回码,0表示全部运行成功,其他代码解释参考上篇文章。
输出
[root@10_1_162_39 host_vars]# python test_api_2.py PLAY [10.1.167.36] ************************************************************* TASK [command] ***************************************************************** changed: [10.1.167.36] TASK [debug] ******************************************************************* ok: [10.1.167.36] => { "output.stdout": "eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500\n inet 192.168.142.129 netmask 255.255.255.0 broadcast 192.168.142.255\n inet6 fe80::20c:29ff:fe9e:335f prefixlen 64 scopeid 0x20<link>\n ether 00:0c:29:9e:33:5f txqueuelen 1000 (Ethernet)\n RX packets 54015 bytes 6112394 (5.8 MiB)\n RX errors 0 dropped 0 overruns 0 frame 0\n TX packets 5675 bytes 898005 (876.9 KiB)\n TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0\n\nlo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536\n inet 127.0.0.1 netmask 255.0.0.0\n inet6 ::1 prefixlen 128 scopeid 0x10<host>\n loop txqueuelen 1 (Local Loopback)\n RX packets 4 bytes 340 (340.0 B)\n RX errors 0 dropped 0 overruns 0 frame 0\n TX packets 4 bytes 340 (340.0 B)\n TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0\n\nvirbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500\n inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255\n ether 52:54:00:2b:6f:35 txqueuelen 1000 (Ethernet)\n RX packets 0 bytes 0 (0.0 B)\n RX errors 0 dropped 0 overruns 0 frame 0\n TX packets 0 bytes 0 (0.0 B)\n TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0" } PLAY RECAP ********************************************************************* 10.1.167.36 : ok=2 changed=1 unreachable=0 failed=0 0 [root@10_1_162_39 host_vars]#