ansible 在python中的使用,ansiblepython使用
step1
环境master:centos 7
rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum install gcc libffi-devel python-devel openssl-devel
yum -y install ansible
step2
# vim /etc/ansible/hosts
[remote-host]
192.168.1.241
...
step3
.py脚本文件
#!/usr/bin/python
import ansible.runner
import sys
# construct the ansible runner and execute on all hosts
def command(pattern,module,args,fork):
results = ansible.runner.Runner(
pattern=pattern, forks=fork,
module_name=module, module_args=args,
).run()
if results is None:
print "No hosts found"
sys.exit(1)
print "UP ***********"
for (hostname, result) in results['contacted'].items():
try:
if not 'failed' in result:
print "%s >>> %s" % (hostname, result['stdout'])
except Exception,e:
print e
sys.exit(1)
print "FAILED *******"
for (hostname, result) in results['contacted'].items():
try:
if 'failed' in result:
print "%s >>> %s" % (hostname, result['msg'])
except Exception,e:
print e
sys.exit(1)
print "DOWN *********"
for (hostname, result) in results['dark'].items():
try:
print "%s >>> %s" % (hostname, result)
except Exception,e:
print e
sys.exit(1)
if __name__ == "__main__":
#command('remote-host','command','/usr/bin/uptime',5)
command('remote-host','copy','src=/root/zabbix-2.4.6.tar.gz dest=/root/zabbix-2.4.6.tar.gz mode=0644',5)
ansible V2.0
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import json
from collections import namedtuple
from ansible.parsing.dataloader import DataLoader
from ansible.vars import VariableManager
from ansible.inventory import Inventory
from ansible.playbook.play import Play
from ansible.executor.task_queue_manager import TaskQueueManager
from ansible.plugins.callback import CallbackBase
# Create a callback object so we can capture the output
class ResultCallback(CallbackBase):
"""A sample callback plugin used for performing an action as results come in
If you want to collect all results into a single object for processing at
the end of the execution, look into utilizing the ``json`` callback plugin
or writing your own custom callback plugin
"""
def v2_runner_on_ok(self, result, **kwargs):
"""Print a json representation of the result
This method could store the result in an instance attribute for retrieval later
"""
host = result._host
print (json.dumps({host.name: result._result}, indent=4))
def ansibleRun(host_list,task_list):
Options = namedtuple('Options',
['connection','module_path','forks','remote_user','private_key_file',
'ssh_common_args','ssh_extra_args','sftp_extra_args','scp_extra_args',
'become','become_method','become_user','verbosity','check'])
# initialize needed objects
variable_manager = VariableManager()
loader = DataLoader()
options = Options( connection='smart',module_path=None,
forks=100,remote_user=None,private_key_file=None,ssh_common_args=None,ssh_extra_args=None,
sftp_extra_args=None,scp_extra_args=None,become=None,become_method=None,
become_user=None,verbosity=None,check=False
)
passwords = dict()
# Instantiate our ResultCallback for handling results as they come in
results_callback = ResultCallback()
# create inventory and pass to var manager
inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=host_list)
variable_manager.set_inventory(inventory)
# create play with tasks
play_source = dict(
name = "Ansible Play",
hosts = host_list,
gather_facts = 'no',
tasks = task_list
)
play = Play().load(play_source, variable_manager=variable_manager, loader=loader)
# actually run it
tqm = None
try:
tqm = TaskQueueManager(
inventory=inventory,variable_manager=variable_manager,
loader=loader,options=options,passwords=passwords,
stdout_callback=results_callback, # Use our custom callback instead of the ``default`` callback plugin
)
result = tqm.run(play)
finally:
if tqm is not None:
tqm.cleanup()
if __name__ == '__main__':
host_list = ['10.14.86.177', '10.14.86.178']
tasks_list = [
dict(action=dict(module='command', args='pwd')),
# dict(action=dict(module='shell', args='python sleep.py')),
# dict(action=dict(module='synchronize', args='src=/home/op/test dest=/home/op/ delete=yes')),
]
ansibleRun(host_list,tasks_list)