1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
""" 通过ansible API动态生成ansible资产信息但不产生实际的hosts文件 主机信息都可以通过数据库获得,然后生成指定格式,最后调用这个类来 生成主机信息。 """
import sys
from ansible.executor.playbook_executor import PlaybookExecutor from ansible.parsing.dataloader import DataLoader
from ansible.vars.manager import VariableManager
from ansible.inventory.manager import InventoryManager
from ansible.inventory.host import Host
from ansible.inventory.group import Group
from ansible.plugins.callback import CallbackBase from collections import namedtuple
class PlaybookCallResultCollector(CallbackBase): """ playbook的callback改写,格式化输出playbook执行结果 """ CALLBACK_VERSION = 2.0
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.task_ok = {} self.task_unreachable = {} self.task_failed = {} self.task_skipped = {} self.task_status = {}
def v2_runner_on_unreachable(self, result): """ 重写 unreachable 状态 :param result: 这是父类里面一个对象,这个对象可以获取执行任务信息 """ self.task_unreachable[result._host.get_name()] = result
def v2_runner_on_ok(self, result, *args, **kwargs): """ 重写 ok 状态 :param result: """ self.task_ok[result._host.get_name()] = result
def v2_runner_on_failed(self, result, *args, **kwargs): """ 重写 failed 状态 :param result: """ self.task_failed[result._host.get_name()] = result
def v2_runner_on_skipped(self, result): self.task_skipped[result._host.get_name()] = result
class MyInventory: def __init__(self, hostsresource): """ 初始化函数 :param hostsresource: 主机资源可以有2种形式 列表形式: [{"ip": "172.16.48.171", "port": "22", "username": "root", "password": "123456"}] 字典形式: { "Group1": { "hosts": [{"ip": "192.168.200.10", "port": "1314", "username": "root", "password": None}], "vars": {"var1": "ansible"} }, "Group2": {} } """ self._hostsresource = hostsresource self._loader = DataLoader() self._hostsfilelist = ["temphosts"] """ sources这个我们知道这里是设置hosts文件的地方,它可以是一个列表里面包含多个文件路径且文件真实存在,在单纯的执行ad-hoc的时候这里的 文件里面必须具有有效的hosts配置,但是当通过动态生成的资产信息的时候这个文件必须存在但是它里面可以是空的,如果这里配置成None那么 它不影响资产信息动态生成但是会有一个警告,所以还是要配置一个真实文件。 """ self._inventory = InventoryManager(loader=self._loader, sources=self._hostsfilelist) self._variable_manager = VariableManager(loader=self._loader, inventory=self._inventory)
self._dynamic_inventory()
def _add_dynamic_group(self, hosts_list, groupname, groupvars=None): """ 动态添加主机到指定的主机组
完整的HOSTS文件格式 [test1] hostname ansible_ssh_host=192.168.1.111 ansible_ssh_user="root" ansible_ssh_pass="123456"
但通常我们都省略hostname,端口也省略因为默认是22,这个在ansible配置文件中有,除非有非22端口的才会配置 [test1] 192.168.100.10 ansible_ssh_user="root" ansible_ssh_pass="123456" ansible_python_interpreter="/PATH/python3/bin/python3"
:param hosts_list: 主机列表 [{"ip": "192.168.100.10", "port": "22", "username": "root", "password": None}, {}] :param groupname: 组名称 :param groupvars: 组变量,格式为字典 :return: """ my_group = Group(name=groupname) self._inventory.add_group(groupname)
if groupvars: for key, value in groupvars.items(): my_group.set_variable(key, value)
for host in hosts_list: hostname = host.get("hostname", None) hostip = host.get("ip", None) if hostip is None: print("IP地址为空,跳过该元素。") continue hostport = host.get("port", "22") username = host.get("username", "root") password = host.get("password", None) ssh_key = host.get("ssh_key", None) python_interpreter = host.get("python_interpreter", None)
try: if hostname is None: hostname = hostip my_host = Host(name=hostname, port=hostport) self._variable_manager.set_host_variable(host=my_host, varname="ansible_ssh_host", value=hostip) self._variable_manager.set_host_variable(host=my_host, varname="ansible_ssh_port", value=hostport) if password: self._variable_manager.set_host_variable(host=my_host, varname="ansible_ssh_pass", value=password) self._variable_manager.set_host_variable(host=my_host, varname="ansible_ssh_user", value=username) if ssh_key: self._variable_manager.set_host_variable(host=my_host, varname="ansible_ssh_private_key_file", value=ssh_key) if python_interpreter: self._variable_manager.set_host_variable(host=my_host, varname="ansible_python_interpreter", value=python_interpreter)
for key, value in host.items(): if key not in ["ip", "hostname", "port", "username", "password", "ssh_key", "python_interpreter"]: self._variable_manager.set_host_variable(host=my_host, varname=key, value=value)
self._inventory.add_host(host=hostname, group=groupname, port=hostport)
except Exception as err: print(err)
def _dynamic_inventory(self): """ 添加 hosts 到inventory :return: """ if isinstance(self._hostsresource, list): self._add_dynamic_group(self._hostsresource, "default_group") elif isinstance(self._hostsresource, dict): for groupname, hosts_and_vars in self._hostsresource.items(): self._add_dynamic_group(hosts_and_vars.get("hosts"), groupname, hosts_and_vars.get("vars"))
@property def INVENTORY(self): """ 返回资产实例 :return: """ return self._inventory
@property def VARIABLE_MANAGER(self): """ 返回变量管理器实例 :return: """ return self._variable_manager
class AnsibleRunner(object): def __init__(self, hostsresource): 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", "diff" ]) self._options = Options(connection='smart', remote_user=None, ack_pass=None, sudo_user=None, forks=5, sudo=None, ask_sudo_pass=False, verbosity=5, module_path=None, become=None, become_method=None, become_user=None, check=False, diff=False, listhosts=None, listtasks=None, listtags=None, syntax=None) self._passwords = dict(sshpass=None, becomepass=None) self._loader = DataLoader() myinven = MyInventory(hostsresource=hostsresource) self._inventory = myinven.INVENTORY self._variable_manager = myinven.VARIABLE_MANAGER
def run_playbook(self, playbook_path, extra_vars=None): """ 执行playbook :param playbook_path: playbook的yaml文件路径 :param extra_vars: 额外变量 :return: 无返回值 """ try: if extra_vars: self._variable_manager.extra_vars = extra_vars playbook = PlaybookExecutor(playbooks=[playbook_path], inventory=self._inventory, variable_manager=self._variable_manager, loader=self._loader, options=self._options, passwords=self._passwords) self._callback = PlaybookCallResultCollector() playbook._tqm._stdout_callback = self._callback playbook.run() except Exception as err: print(err)
def get_playbook_result(self): """ 获取playbook执行结果 :return: """ result_raw = {"ok": {}, "failed": {}, "unreachable": {}, "skipped": {}, "status": {}} for host, result in self._callback.task_ok.items(): result_raw["ok"][host] = result._result
for host, result in self._callback.task_failed.items(): result_raw["failed"][host] = result._result
for host, result in self._callback.task_unreachable.items(): result_raw["unreachable"][host] = result._result
for host, result in self._callback.task_skipped.items(): result_raw["skipped"][host] = result._result
for host, result in self._callback.task_status.items(): result_raw["status"][host] = result._result
return result_raw
def main(): temphosts_list = [{"ip": "172.16.48.250", "port": "22", "username": "root", "password": "12qwaszx!@QW"}]
temphosts_dict = { "Group1": { "hosts": [{"ip": "192.168.200.10", "port": "1314", "username": "root", "password": None}], "vars": {"var1": "ansible"} }, }
ar = AnsibleRunner(temphosts_list) ar.run_playbook("/Users/rex.chen/PycharmProjects/IDCMigration/AnsibleStudy/f1.yml") print(ar.get_playbook_result())
if __name__ == "__main__": try: main() finally: sys.exit()
|