一、引言

现阶段不少小伙伴都有自研CMDB平台,不过也有不少朋友询问如何定时获取系统硬件数据更新到数据库?对此,博主推荐使用自动化运维工具,如salt、ansible等,并不是自行写脚本就不可以。如做到标准化、规范化、机器规模达到一定量,还是比较推荐使用自动化运维工具。

二、简单看以下执行shell模块中ls命令

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
#!/usr/bin/env python
'''
引用模块
'''
import json
import shutil
from collections import namedtuple
from ansible.parsing.dataloader import DataLoader
from ansible.vars.manager import VariableManager
from ansible.inventory.manager import InventoryManager
from ansible.playbook.play import Play
from ansible.executor.task_queue_manager import TaskQueueManager
from ansible.plugins.callback import CallbackBase
import ansible.constants as C

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))

# since API is constructed for CLI it expects certain options to always be set, named tuple 'fakes' the args parsing options object
Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check', 'diff'])
options = Options(connection='local', module_path=['/to/mymodules'], forks=10, become=None, become_method=None, become_user=None, check=False, diff=False)

# initialize needed objects
loader = DataLoader() # Takes care of finding and reading yaml, json and ini files
passwords = dict(vault_pass='secret')

# Instantiate our ResultCallback for handling results as they come in. Ansible expects this to be one of its main display outlets
results_callback = ResultCallback()

# create inventory, use path to host config file as source or hosts in a comma separated string
inventory = InventoryManager(loader=loader, sources='localhost,')

# variable manager takes care of merging all the different sources to give you a unifed view of variables available in each context
variable_manager = VariableManager(loader=loader, inventory=inventory)

# create datastructure that represents our play, including tasks, this is basically what our YAML loader does internally.
play_source = dict(
name = "Ansible Play",
hosts = 'localhost',
gather_facts = 'no',
tasks = [
dict(action=dict(module='shell', args='ls'), register='shell_out'),
dict(action=dict(module='debug', args=dict(msg='{{shell_out.stdout}}')))
]
)

# Create play object, playbook objects use .load instead of init or new methods,
# this will also automatically create the task objects from the info provided in play_source
play = Play().load(play_source, variable_manager=variable_manager, loader=loader)

# Run it - instantiate task queue manager, which takes care of forking and setting up all objects to iterate over host list and tasks
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, which prints to stdout
)
result = tqm.run(play) # most interesting data for a play is actually sent to the callback's methods
finally:
# we always need to cleanup child procs and the structres we use to communicate with them
if tqm is not None:
tqm.cleanup()

# Remove ansible tmpdir
shutil.rmtree(C.DEFAULT_LOCAL_TMP, True)

运行结果如下:

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
[root@localhost python]# python ansible_api.py 
{
"localhost": {
"_ansible_parsed": true,
"stderr_lines": [],
"changed": true,
"end": "2018-05-29 03:44:27.292832",
"_ansible_no_log": false,
"stdout": "ansible_api.py\nsaomiao.py",
"cmd": "ls",
"start": "2018-05-29 03:44:27.262984",
"delta": "0:00:00.029848",
"stderr": "",
"rc": 0,
"invocation": {
"module_args": {
"creates": null,
"executable": null,
"_uses_shell": true,
"_raw_params": "ls",
"removes": null,
"warn": true,
"chdir": null,
"stdin": null
}
},
"stdout_lines": [
"ansible_api.py",
"saomiao.py"
]
}
}
{
"localhost": {
"msg": "ansible_api.py\nsaomiao.py",
"changed": false,
"_ansible_verbose_always": true,
"_ansible_no_log": false
}
}

通过以上可举例,当获取硬件如cpu、硬盘、内存等信息时候,可以使用setup模块获取,过滤即可入库保存。

三、Ansible模块中核心类

1
2
3
4
5
6
from ansible.parsing.dataloader import DataLoader
from ansible.vars.manager import VariableManager
from ansible.inventory.manager import InventoryManager
from ansible.playbook.play import Play
from ansible.executor.task_queue_manager import TaskQueueManager
from ansible.plugins.callback import CallbackBase
核心类 用途 路径
DataLoader 读取yam、json格式文件 ansible.parsing.dataloader
VariableManager 存储变量信息 ansible.inventory.manager
InventoryManager 用于导入Invetory文件 ansible.inventory.manager
Play 存储hosts角色信息 ansible.playbook.play
TaskQueueManager 任务队列 ansible.executor.task_queue_manager
CallbackBase 状态回调 ansible.plugins.callback
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
In [2]: from ansible.vars.manager import VariableManager

In [3]: from ansible.inventory.manager import InventoryManager

In [4]: from ansible.playbook.play import Play

In [5]: from ansible.executor.playbook_executor import PlaybookExecutor

In [6]: from ansible.executor.task_queue_manager import TaskQueueManager

In [7]: from ansible.plugins.callback import CallbackBase

In [8]: loader = DataLoader() ##实例对象

In [9]: inventory = InventoryManager(loader=loader, sources=['/etc/ansible/hosts']) ##调用InventoryManager返回实例对象
执行dir查看函数
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
In [10]: dir(inventory)
Out[10]:
['__class__',
'__delattr__',
'__dict__',
'__doc__',
'__format__',
'__getattribute__',
'__hash__',
'__init__',
'__module__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'__weakref__',
'_apply_subscript',
'_enumerate_matches',
'_evaluate_patterns',
'_hosts_patterns_cache',
'_inventory',
'_inventory_plugins',
'_loader',
'_match_list',
'_match_one_pattern',
'_pattern_cache',
'_restriction',
'_setup_inventory_plugins',
'_sources',
'_split_subscript',
'_subset',
'add_group',
'add_host',
'clear_caches',
'clear_pattern_cache',
'get_groups_dict',
'get_host',
'get_hosts',
'get_vars',
'groups',
'hosts',
'list_groups',
'list_hosts',
'localhost',
'parse_source',
'parse_sources',
'reconcile_inventory',
'refresh_inventory',
'remove_restriction',
'restrict_to_hosts',
'subset']
/etc/ansible/hosts
1
2
3
4
5
6
7
8
9
10
11
[root@localhost ~]# tail /etc/ansible/hosts 
# leading 0s:

## db-[99:101]-node.example.com

[VM_129]
192.168.72.129
192.168.72.130
[VM_129:vars]
ansible_ssh_port=22
nginx_version=1.13.1
获取主机group
1
2
In [30]: print inventory.get_groups_dict()
{'ungrouped': [], 'all': [u'192.168.72.129', u'192.168.72.130'], u'VM_129': [u'192.168.72.129', u'192.168.72.130']}
获取主机host
1
2
In [31]: inventory.hosts
Out[31]: {u'192.168.72.129': 192.168.72.129, u'192.168.72.130': 192.168.72.130}
variable_manager
1
2
3
4
5
6
7
8
9
In [32]: variable_manager = VariableManager(loader=loader, inventory=inventory)


###查看相关模块
In [33]: variable_manager.
variable_manager.clear_facts variable_manager.set_host_facts
variable_manager.extra_vars variable_manager.set_host_variable
variable_manager.get_vars variable_manager.set_inventory
variable_manager.options_vars variable_manager.set_nonpersistent_facts

重点说三个variable_manager模块

  • 1、查看变量variable_manager.get_vars
  • 2、扩展变量variable_manager.extra_vars
  • 3、设置主机变量variable_manager.set_host_variable
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
In [37]: host=inventory.get_host(hostname='192.168.72.129')

In [38]: variable_manager.get_vars(host=host)
Out[38]:
{'ansible_playbook_python': '/usr/bin/python2',
u'ansible_ssh_port': 22,
'group_names': [u'VM_129'],
'groups': {u'VM_129': [u'192.168.72.129', u'192.168.72.130'],
'all': [u'192.168.72.129', u'192.168.72.130'],
'ungrouped': []},
'inventory_dir': u'/etc/ansible',
'inventory_file': u'/etc/ansible/hosts',
'inventory_hostname': u'192.168.72.129',
'inventory_hostname_short': u'192',
u'nginx_version': u'1.13.1',
'omit': '__omit_place_holder__91ac78f3a75e01eaa7d20ff0db9463023de382b8',
'playbook_dir': '/root'}

四、如何执行剧本?

上面如何介绍了执行命令,下面顺便提供执行剧本的代码

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
# coding:utf-8
# python 2.7.5
# ansible 2.4.2.0

import os, sys
import json
import shutil
from collections import namedtuple
from ansible.parsing.dataloader import DataLoader
from ansible.vars.manager import VariableManager
from ansible.inventory.manager import InventoryManager
from ansible.playbook.play import Play
from ansible.executor.playbook_executor import PlaybookExecutor
from ansible.executor.task_queue_manager import TaskQueueManager
from ansible.plugins.callback import CallbackBase
from ansible.errors import AnsibleParserError
import ansible.constants as C



class Ad_hocResultsCollector(CallbackBase):

def __init__(self, *args, **kwargs):
super(Ad_hocResultsCollector, self).__init__(*args, **kwargs)
self.host_ok = {}
self.host_unreachable = {}
self.host_failed = {}

def v2_runner_on_unreachable(self, result):
self.host_unreachable[result._host.get_name()] = result

def v2_runner_on_ok(self, result, *args, **kwargs):
self.host_ok[result._host.get_name()] = result

def v2_runner_on_failed(self, result, *args, **kwargs):
self.host_failed[result._host.get_name()] = result


def getAd_hocResult(self):
# 获取结果的回调函数
results_info = {'success': {}, 'failed': {}, 'unreachable': {}}
for host, result in self.host_ok.items():
results_info['success'][host] = result._result
for host, result in self.host_failed.items():
results_info['failed'][host] = result._result
for host, result in self.host_unreachable.items():
results_info['unreachable'][host] = result._result

return results_info


class PlayBookResultsCollector(CallbackBase):
CALLBACK_VERSION = 2.0

def __init__(self, *args, **kwargs):
super(PlayBookResultsCollector, self).__init__(*args, **kwargs)
self.task_ok = {}
self.task_skipped = {}
self.task_failed = {}
self.task_status = {}
self.task_unreachable = {}
self.status_no_hosts = False

def v2_runner_on_ok(self, result, *args, **kwargs):
self.task_ok[result._host.get_name()] = result

def v2_runner_on_failed(self, result, *args, **kwargs):
self.task_failed[result._host.get_name()] = result

def v2_runner_on_unreachable(self, result):
self.task_unreachable[result._host.get_name()] = result

def v2_runner_on_skipped(self, result):
self.task_skipped[result._host.get_name()] = result

def v2_playbook_on_no_hosts_matched(self):
self.status_no_hosts = True

def v2_playbook_on_stats(self, stats):

hosts = sorted(stats.processed.keys())
for h in hosts:
t = stats.summarize(h)
self.task_status[h] = {
"success": t['ok'],
"changed": t['changed'],
"unreachable": t['unreachable'],
"skipped": t['skipped'],
"failed": t['failures']
}

def getPlaybookResult(self):
if self.status_no_hosts:
results = {'msg': "Could not match supplied host pattern", 'flag': False, 'executed': False}
return results
results_info = {'skipped': {}, 'failed': {}, 'success': {}, "status": {}, 'unreachable': {}, "changed": {}}
for host, result in self.task_ok.items():
results_info['success'][host] = result._result
for host, result in self.task_failed.items():
results_info['failed'][host] = result
for host, result in self.task_status.items():
results_info['status'][host] = result
for host, result in self.task_skipped.items():
results_info['skipped'][host] = result
for host, result in self.task_unreachable.items():
results_info['unreachable'][host] = result
return results_info

class AnsRunner(object):
def __init__(self):
self.resource = None
self.inventory = None
self.variable_manager = None
self.loader = None
self.options = None
self.passwords = None
self.__initializeData()

def __initializeData(self):

# 初始化需要的对象
Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'timeout', 'remote_user',
'ask_pass', 'private_key_file', 'ssh_common_args', 'ssh_extra_args',
'sftp_extra_args',
'scp_extra_args', 'become', 'become_method', 'become_user', 'ask_value_pass',
'verbosity',
'check', 'listhosts', 'listtasks', 'listtags', 'syntax', 'diff'])

self.options = Options(connection='ssh', module_path=None, forks=100, timeout=10,
remote_user='root', ask_pass=False, 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='root', ask_value_pass=False, verbosity=None, check=False, listhosts=False,
listtasks=False, listtags=False, syntax=False, diff=False)

self.loader = DataLoader() # Takes care of finding and reading yaml, json and ini files
self.passwords = dict(vault_pass='secret')
self.results_callback = Ad_hocResultsCollector()

# create inventory, use path to host config file as source or hosts in a comma separated string e.g. sources=['10.1.30.193',]
self.inventory = InventoryManager(loader=self.loader, sources=['/etc/ansible/hosts'])
# variable manager takes care of merging all the different sources to give you a unifed view of variables available in each context
self.variable_manager = VariableManager(loader=self.loader, inventory=self.inventory)

def run_ad_hoc(self, host_list=None, module_name=None, module_args=None):
self.results_callback = Ad_hocResultsCollector()
# create datastructure that represents our play, including tasks, this is basically what our YAML loader does internally.
play_source = dict(
name="Ansible Play",
hosts=host_list,
gather_facts='no',
tasks=[
dict(action=dict(module=module_name, args=module_args), register='shell_out'),
dict(action=dict(module='debug', args=dict(msg='{{shell_out.stdout}}')))
]
)

play = Play().load(play_source, variable_manager=self.variable_manager, loader=self.loader)
tqm = None
try:
tqm = TaskQueueManager(
inventory=self.inventory,
variable_manager=self.variable_manager,
loader=self.loader,
options=self.options,
passwords=self.passwords,
stdout_callback=self.results_callback,
# Use our custom callback instead of the ``default`` callback plugin, which prints to stdout
)
result = tqm.run(play) # most interesting data for a play is actually sent to the callback's methods
return self.results_callback.getAd_hocResult()
finally:
# we always need to cleanup child procs and the structres we use to communicate with them
if tqm is not None:
tqm.cleanup()

# Remove ansible tmpdir
shutil.rmtree(C.DEFAULT_LOCAL_TMP, True)

def get_cmdb_info(self, host_list):
self.results_callback = Ad_hocResultsCollector()
play_source = dict(
name="Ansible setup Play",
hosts=host_list,
gather_facts='no',
tasks=[
dict(action=dict(module='setup'), register='shell_out'),

]
)

play = Play().load(play_source, variable_manager=self.variable_manager, loader=self.loader)
tqm = None
try:
tqm = TaskQueueManager(
inventory=self.inventory,
variable_manager=self.variable_manager,
loader=self.loader,
options=self.options,
passwords=self.passwords,
stdout_callback=self.results_callback,
)
result = tqm.run(play)
return self.results_callback.getAd_hocResult()
finally:
if tqm is not None:
tqm.cleanup()
shutil.rmtree(C.DEFAULT_LOCAL_TMP, True)

def run_playbook(self, playbook_path, host_list=None, extra_vars=None):

for i in playbook_path:
if not os.path.exists(i):
print
'[INFO] The [%s] playbook does not exist' % i
sys.exit()

self.variable_manager.extra_vars = extra_vars
passwords = None
self.results_callback = PlayBookResultsCollector()
try:
playbook = PlaybookExecutor(playbooks=playbook_path, inventory=self.inventory,
variable_manager=self.variable_manager,
loader=self.loader, options=self.options, passwords=passwords)
playbook._tqm._stdout_callback = self.results_callback
# 执行playbook
result = playbook.run()
return self.results_callback.getPlaybookResult()
except AnsibleParserError:
code = 1001
results = {'playbook': playbook_path, 'msg': playbook_path + ' playbook have syntax error', 'flag': False}
return code, results
# except Exception as e:
# return False



if __name__ == '__main__':
ANS = AnsRunner()
ret=ANS.run_ad_hoc(host_list='*', module_name='shell', module_args='ls')
#ret=ANS.get_cmdb_info(host_list='*')
#print(ret)
print(ANS.run_playbook(playbook_path=['/data/Playbook/install_jdk18.yaml'],
extra_vars={"remote_server": "*"}))

Comments

2019-02-18