ansible api常用模块与参数

###ansibleAPI 常用模块用于读取yaml,json格式的文件from ansible.parsing.dataloader import DataLoader#用于管理变量的类,包括主机,组,扩展等变量from ansible.vars.manager import VariableManager#用于创建和管理inventory,倒入inventory文件from ansible.inventory.manager import InventoryManager#ad-hoc 存储着角色列表,任务,处理代码块from ansible.playbook.play import Play#ad-hoc ansible底层用到的任务队列from ansible.executor.task_queue_manager import TaskQueueManager#回调基类,用来定义回调事件,比如返回失败成功等信息from ansible.plugins.callback import CallbackBase#执行playbookfrom ansible.executor.playbook_executor import PlaybookExecutor#操作单个主机from ansible.inventory import host#操作单个主机组from ansible.inventory import group

###InventoryManager#实例化需要两个参数"参数一为读取yml文件的信息,需要实例化 DataLoader""参数二为读取从那个位置读取资产配置文件,多个可逗号分割"intertory = InventoryManager(loader=‘‘,sources=‘‘)#以字典的方式打印出主机和主机组相关信息intertory.get_group_dict()#获取所有的主机inventory.get_hosts()#添加主机到指定主机组"参数一指定主机地址""参数二指定主机端口""参数三指定主机群组,必须存在的群组"inventory.add_host(host=1.1.1.1,port=2222,group=web_server)#获取指定的主机对象inventory.get_host(hostname=1.1.1.1)

###VariableManager#实例化需要两个参数"参数一为读取yml文件的信息,需要实例化 DataLoader""参数二为资产管理配置变量"variable = VariableManager(loader=loader,inventory=inventory)#获取变量variable.get_vars()# 查看指定主机的详细变量信息"传入的host是inventory.get_host获得的主机对象"host = inventory.get_host(hostname=1.1.1.1)host_vars = variable.get_vars(host=host)#设置主机变量方法 "传入的host是inventory.get_host获得的主机对象"host = inventory.get_host(hostname=1.1.1.1)variable.set_host_variable(host=host,varname=ansible_ssh_pass,value=12345)#添加扩展变量"参数是一个字典多个逗号分割"variable.extra_vars={devops:best}

 

相关文章