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 #执行playbook from 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'}
##################
Runner参数:
host_list=C.DEFAULT_HOST_LIST, # ex: /etc/ansible/hosts, legacy usage #执行host清单,如果不指定文件,会读取ansible.cfg文件的inventory = xx字段。
module_path=None, # ex: /usr/share/ansible#模块路径,比如:/usr/share/ansible ex==example。
module_name=C.DEFAULT_MODULE_NAME, # ex: copy#模块的名字。
module_args=C.DEFAULT_MODULE_ARGS, # ex: "src=/tmp/a dest=/tmp/b"#模块的参数。
forks=C.DEFAULT_FORKS, # parallelism level并发进程数。
timeout=C.DEFAULT_TIMEOUT, # SSH timeout#ssh 连接超时。
pattern=C.DEFAULT_PATTERN, # which hosts? ex: 'all', 'acme.example.org'#host清单里,匹配的组 host清单。
remote_user=C.DEFAULT_REMOTE_USER, # ex: 'username'#远程登录用户和执行用户。
remote_pass=C.DEFAULT_REMOTE_PASS, # ex: 'password123' or None if using key#远程登录用户密码。
remote_port=None, # if SSH on different ports#ssh连接端口。
private_key_file=C.DEFAULT_PRIVATE_KEY_FILE, # if not using keys/passwords#私钥位置,如果不是ssh 连接话。
background=0, # async poll every X seconds, else 0 for non-async#异步参数。
basedir=None, # directory of playbook, if applicable#playbook 路径。
setup_cache=None, # used to share fact data w/ other tasks#搜集远程节点的信息。
vars_cache=None, # used to store variables about hosts#host清单变量。
transport=C.DEFAULT_TRANSPORT, # 'ssh', 'paramiko', 'local'#连接远程主机的方式,是ssh还是paramiko....
conditional='True', # run only if this fact expression evals to true #未知
callbacks=None, # used for output#用于结果输出。
module_vars=None, # a playbooks internals thing##模块变量。
play_vars=None, #
play_file_vars=None, #
role_vars=None, #
role_params=None, #
default_vars=None, #
extra_vars=None, # extra vars specified with he playbook(s)#其他额外一些参数。
is_playbook=False, # running from playbook or not?
inventory=None, # reference to Inventory object
subset=None, # subset pattern
check=False, # don't make any changes, just try to probe for potential changes#测试play,看看对hostlist产生哪些变化。
diff=False, # whether to show diffs for template files that change
environment=None, # environment variables (as dict) to use inside the command
complex_args=None, # structured data in addition to module_args, must be a dict
error_on_undefined_vars=C.DEFAULT_UNDEFINED_VAR_BEHAVIOR, # ex. False
accelerate=False, # use accelerated connection
accelerate_ipv6=False, # accelerated connection w/ IPv6
accelerate_port=None, # port to use with accelerated connection
vault_pass=None,
run_hosts=None, # an optional list of pre-calculated hosts to run on
no_log=False, # option to enable/disable logging for a given task
run_once=False, # option to enable/disable host bypass loop for a given task
become=False, # whether to run privelege escalation or not
become_method=C.DEFAULT_BECOME_METHOD,
become_user=C.DEFAULT_BECOME_USER, # ex: 'root'
become_pass=C.DEFAULT_BECOME_PASS, # ex: 'password123' or None
become_exe=C.DEFAULT_BECOME_EXE, # ex: /usr/local/bin/sudo
):
# used to lock multiprocess inputs and outputs at various levels
self.output_lockfile = OUTPUT_LOCKFILE
self.process_lockfile = PROCESS_LOCKFILE