PageRenderTime 47ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/ansible/utils/__init__.py

https://gitlab.com/18runt88/ansible
Python | 1360 lines | 1220 code | 65 blank | 75 comment | 92 complexity | c400d8ae01d37216d1f087f22eaf3a71 MD5 | raw file
  1. # (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
  2. #
  3. # This file is part of Ansible
  4. #
  5. # Ansible is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # Ansible is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
  17. import errno
  18. import sys
  19. import re
  20. import os
  21. import shlex
  22. import yaml
  23. import copy
  24. import optparse
  25. import operator
  26. from ansible import errors
  27. from ansible import __version__
  28. from ansible.utils.display_functions import *
  29. from ansible.utils.plugins import *
  30. from ansible.utils.su_prompts import *
  31. from ansible.utils.hashing import secure_hash, secure_hash_s, checksum, checksum_s, md5, md5s
  32. from ansible.callbacks import display
  33. from ansible.module_utils.splitter import split_args, unquote
  34. from ansible.module_utils.basic import heuristic_log_sanitize
  35. from ansible.utils.unicode import to_bytes, to_unicode
  36. import ansible.constants as C
  37. import ast
  38. import time
  39. import StringIO
  40. import stat
  41. import termios
  42. import tty
  43. import pipes
  44. import random
  45. import difflib
  46. import warnings
  47. import traceback
  48. import getpass
  49. import sys
  50. import subprocess
  51. import contextlib
  52. from vault import VaultLib
  53. VERBOSITY=0
  54. MAX_FILE_SIZE_FOR_DIFF=1*1024*1024
  55. # caching the compilation of the regex used
  56. # to check for lookup calls within data
  57. LOOKUP_REGEX = re.compile(r'lookup\s*\(')
  58. PRINT_CODE_REGEX = re.compile(r'(?:{[{%]|[%}]})')
  59. CODE_REGEX = re.compile(r'(?:{%|%})')
  60. try:
  61. # simplejson can be much faster if it's available
  62. import simplejson as json
  63. except ImportError:
  64. import json
  65. try:
  66. from yaml import CSafeLoader as Loader
  67. except ImportError:
  68. from yaml import SafeLoader as Loader
  69. PASSLIB_AVAILABLE = False
  70. try:
  71. import passlib.hash
  72. PASSLIB_AVAILABLE = True
  73. except:
  74. pass
  75. try:
  76. import builtin
  77. except ImportError:
  78. import __builtin__ as builtin
  79. KEYCZAR_AVAILABLE=False
  80. try:
  81. try:
  82. # some versions of pycrypto may not have this?
  83. from Crypto.pct_warnings import PowmInsecureWarning
  84. except ImportError:
  85. PowmInsecureWarning = RuntimeWarning
  86. with warnings.catch_warnings(record=True) as warning_handler:
  87. warnings.simplefilter("error", PowmInsecureWarning)
  88. try:
  89. import keyczar.errors as key_errors
  90. from keyczar.keys import AesKey
  91. except PowmInsecureWarning:
  92. system_warning(
  93. "The version of gmp you have installed has a known issue regarding " + \
  94. "timing vulnerabilities when used with pycrypto. " + \
  95. "If possible, you should update it (i.e. yum update gmp)."
  96. )
  97. warnings.resetwarnings()
  98. warnings.simplefilter("ignore")
  99. import keyczar.errors as key_errors
  100. from keyczar.keys import AesKey
  101. KEYCZAR_AVAILABLE=True
  102. except ImportError:
  103. pass
  104. ###############################################################
  105. # Abstractions around keyczar
  106. ###############################################################
  107. def key_for_hostname(hostname):
  108. # fireball mode is an implementation of ansible firing up zeromq via SSH
  109. # to use no persistent daemons or key management
  110. if not KEYCZAR_AVAILABLE:
  111. raise errors.AnsibleError("python-keyczar must be installed on the control machine to use accelerated modes")
  112. key_path = os.path.expanduser(C.ACCELERATE_KEYS_DIR)
  113. if not os.path.exists(key_path):
  114. os.makedirs(key_path, mode=0700)
  115. os.chmod(key_path, int(C.ACCELERATE_KEYS_DIR_PERMS, 8))
  116. elif not os.path.isdir(key_path):
  117. raise errors.AnsibleError('ACCELERATE_KEYS_DIR is not a directory.')
  118. if stat.S_IMODE(os.stat(key_path).st_mode) != int(C.ACCELERATE_KEYS_DIR_PERMS, 8):
  119. raise errors.AnsibleError('Incorrect permissions on the private key directory. Use `chmod 0%o %s` to correct this issue, and make sure any of the keys files contained within that directory are set to 0%o' % (int(C.ACCELERATE_KEYS_DIR_PERMS, 8), C.ACCELERATE_KEYS_DIR, int(C.ACCELERATE_KEYS_FILE_PERMS, 8)))
  120. key_path = os.path.join(key_path, hostname)
  121. # use new AES keys every 2 hours, which means fireball must not allow running for longer either
  122. if not os.path.exists(key_path) or (time.time() - os.path.getmtime(key_path) > 60*60*2):
  123. key = AesKey.Generate()
  124. fd = os.open(key_path, os.O_WRONLY | os.O_CREAT, int(C.ACCELERATE_KEYS_FILE_PERMS, 8))
  125. fh = os.fdopen(fd, 'w')
  126. fh.write(str(key))
  127. fh.close()
  128. return key
  129. else:
  130. if stat.S_IMODE(os.stat(key_path).st_mode) != int(C.ACCELERATE_KEYS_FILE_PERMS, 8):
  131. raise errors.AnsibleError('Incorrect permissions on the key file for this host. Use `chmod 0%o %s` to correct this issue.' % (int(C.ACCELERATE_KEYS_FILE_PERMS, 8), key_path))
  132. fh = open(key_path)
  133. key = AesKey.Read(fh.read())
  134. fh.close()
  135. return key
  136. def encrypt(key, msg):
  137. return key.Encrypt(msg)
  138. def decrypt(key, msg):
  139. try:
  140. return key.Decrypt(msg)
  141. except key_errors.InvalidSignatureError:
  142. raise errors.AnsibleError("decryption failed")
  143. ###############################################################
  144. # UTILITY FUNCTIONS FOR COMMAND LINE TOOLS
  145. ###############################################################
  146. def read_vault_file(vault_password_file):
  147. """Read a vault password from a file or if executable, execute the script and
  148. retrieve password from STDOUT
  149. """
  150. if vault_password_file:
  151. this_path = os.path.realpath(os.path.expanduser(vault_password_file))
  152. if is_executable(this_path):
  153. try:
  154. # STDERR not captured to make it easier for users to prompt for input in their scripts
  155. p = subprocess.Popen(this_path, stdout=subprocess.PIPE)
  156. except OSError, e:
  157. raise errors.AnsibleError("problem running %s (%s)" % (' '.join(this_path), e))
  158. stdout, stderr = p.communicate()
  159. vault_pass = stdout.strip('\r\n')
  160. else:
  161. try:
  162. f = open(this_path, "rb")
  163. vault_pass=f.read().strip()
  164. f.close()
  165. except (OSError, IOError), e:
  166. raise errors.AnsibleError("Could not read %s: %s" % (this_path, e))
  167. return vault_pass
  168. else:
  169. return None
  170. def err(msg):
  171. ''' print an error message to stderr '''
  172. print >> sys.stderr, msg
  173. def exit(msg, rc=1):
  174. ''' quit with an error to stdout and a failure code '''
  175. err(msg)
  176. sys.exit(rc)
  177. def jsonify(result, format=False):
  178. ''' format JSON output (uncompressed or uncompressed) '''
  179. if result is None:
  180. return "{}"
  181. result2 = result.copy()
  182. for key, value in result2.items():
  183. if type(value) is str:
  184. result2[key] = value.decode('utf-8', 'ignore')
  185. indent = None
  186. if format:
  187. indent = 4
  188. try:
  189. return json.dumps(result2, sort_keys=True, indent=indent, ensure_ascii=False)
  190. except UnicodeDecodeError:
  191. return json.dumps(result2, sort_keys=True, indent=indent)
  192. def write_tree_file(tree, hostname, buf):
  193. ''' write something into treedir/hostname '''
  194. # TODO: might be nice to append playbook runs per host in a similar way
  195. # in which case, we'd want append mode.
  196. path = os.path.join(tree, hostname)
  197. fd = open(path, "w+")
  198. fd.write(buf)
  199. fd.close()
  200. def is_failed(result):
  201. ''' is a given JSON result a failed result? '''
  202. return ((result.get('rc', 0) != 0) or (result.get('failed', False) in [ True, 'True', 'true']))
  203. def is_changed(result):
  204. ''' is a given JSON result a changed result? '''
  205. return (result.get('changed', False) in [ True, 'True', 'true'])
  206. def check_conditional(conditional, basedir, inject, fail_on_undefined=False):
  207. from ansible.utils import template
  208. if conditional is None or conditional == '':
  209. return True
  210. if isinstance(conditional, list):
  211. for x in conditional:
  212. if not check_conditional(x, basedir, inject, fail_on_undefined=fail_on_undefined):
  213. return False
  214. return True
  215. if not isinstance(conditional, basestring):
  216. return conditional
  217. conditional = conditional.replace("jinja2_compare ","")
  218. # allow variable names
  219. if conditional in inject and '-' not in to_unicode(inject[conditional], nonstring='simplerepr'):
  220. conditional = to_unicode(inject[conditional], nonstring='simplerepr')
  221. conditional = template.template(basedir, conditional, inject, fail_on_undefined=fail_on_undefined)
  222. original = to_unicode(conditional, nonstring='simplerepr').replace("jinja2_compare ","")
  223. # a Jinja2 evaluation that results in something Python can eval!
  224. presented = "{%% if %s %%} True {%% else %%} False {%% endif %%}" % conditional
  225. conditional = template.template(basedir, presented, inject)
  226. val = conditional.strip()
  227. if val == presented:
  228. # the templating failed, meaning most likely a
  229. # variable was undefined. If we happened to be
  230. # looking for an undefined variable, return True,
  231. # otherwise fail
  232. if "is undefined" in conditional:
  233. return True
  234. elif "is defined" in conditional:
  235. return False
  236. else:
  237. raise errors.AnsibleError("error while evaluating conditional: %s" % original)
  238. elif val == "True":
  239. return True
  240. elif val == "False":
  241. return False
  242. else:
  243. raise errors.AnsibleError("unable to evaluate conditional: %s" % original)
  244. def is_executable(path):
  245. '''is the given path executable?'''
  246. return (stat.S_IXUSR & os.stat(path)[stat.ST_MODE]
  247. or stat.S_IXGRP & os.stat(path)[stat.ST_MODE]
  248. or stat.S_IXOTH & os.stat(path)[stat.ST_MODE])
  249. def unfrackpath(path):
  250. '''
  251. returns a path that is free of symlinks, environment
  252. variables, relative path traversals and symbols (~)
  253. example:
  254. '$HOME/../../var/mail' becomes '/var/spool/mail'
  255. '''
  256. return os.path.normpath(os.path.realpath(os.path.expandvars(os.path.expanduser(path))))
  257. def prepare_writeable_dir(tree,mode=0777):
  258. ''' make sure a directory exists and is writeable '''
  259. # modify the mode to ensure the owner at least
  260. # has read/write access to this directory
  261. mode |= 0700
  262. # make sure the tree path is always expanded
  263. # and normalized and free of symlinks
  264. tree = unfrackpath(tree)
  265. if not os.path.exists(tree):
  266. try:
  267. os.makedirs(tree, mode)
  268. except (IOError, OSError), e:
  269. raise errors.AnsibleError("Could not make dir %s: %s" % (tree, e))
  270. if not os.access(tree, os.W_OK):
  271. raise errors.AnsibleError("Cannot write to path %s" % tree)
  272. return tree
  273. def path_dwim(basedir, given):
  274. '''
  275. make relative paths work like folks expect.
  276. '''
  277. if given.startswith("'"):
  278. given = given[1:-1]
  279. if given.startswith("/"):
  280. return os.path.abspath(given)
  281. elif given.startswith("~"):
  282. return os.path.abspath(os.path.expanduser(given))
  283. else:
  284. if basedir is None:
  285. basedir = "."
  286. return os.path.abspath(os.path.join(basedir, given))
  287. def path_dwim_relative(original, dirname, source, playbook_base, check=True):
  288. ''' find one file in a directory one level up in a dir named dirname relative to current '''
  289. # (used by roles code)
  290. from ansible.utils import template
  291. basedir = os.path.dirname(original)
  292. if os.path.islink(basedir):
  293. basedir = unfrackpath(basedir)
  294. template2 = os.path.join(basedir, dirname, source)
  295. else:
  296. template2 = os.path.join(basedir, '..', dirname, source)
  297. source2 = path_dwim(basedir, template2)
  298. if os.path.exists(source2):
  299. return source2
  300. obvious_local_path = path_dwim(playbook_base, source)
  301. if os.path.exists(obvious_local_path):
  302. return obvious_local_path
  303. if check:
  304. raise errors.AnsibleError("input file not found at %s or %s" % (source2, obvious_local_path))
  305. return source2 # which does not exist
  306. def repo_url_to_role_name(repo_url):
  307. # gets the role name out of a repo like
  308. # http://git.example.com/repos/repo.git" => "repo"
  309. if '://' not in repo_url and '@' not in repo_url:
  310. return repo_url
  311. trailing_path = repo_url.split('/')[-1]
  312. if trailing_path.endswith('.git'):
  313. trailing_path = trailing_path[:-4]
  314. if trailing_path.endswith('.tar.gz'):
  315. trailing_path = trailing_path[:-7]
  316. if ',' in trailing_path:
  317. trailing_path = trailing_path.split(',')[0]
  318. return trailing_path
  319. def role_spec_parse(role_spec):
  320. # takes a repo and a version like
  321. # git+http://git.example.com/repos/repo.git,v1.0
  322. # and returns a list of properties such as:
  323. # {
  324. # 'scm': 'git',
  325. # 'src': 'http://git.example.com/repos/repo.git',
  326. # 'version': 'v1.0',
  327. # 'name': 'repo'
  328. # }
  329. role_spec = role_spec.strip()
  330. role_version = ''
  331. default_role_versions = dict(git='master', hg='tip')
  332. if role_spec == "" or role_spec.startswith("#"):
  333. return (None, None, None, None)
  334. tokens = [s.strip() for s in role_spec.split(',')]
  335. # assume https://github.com URLs are git+https:// URLs and not
  336. # tarballs unless they end in '.zip'
  337. if 'github.com/' in tokens[0] and not tokens[0].startswith("git+") and not tokens[0].endswith('.tar.gz'):
  338. tokens[0] = 'git+' + tokens[0]
  339. if '+' in tokens[0]:
  340. (scm, role_url) = tokens[0].split('+')
  341. else:
  342. scm = None
  343. role_url = tokens[0]
  344. if len(tokens) >= 2:
  345. role_version = tokens[1]
  346. if len(tokens) == 3:
  347. role_name = tokens[2]
  348. else:
  349. role_name = repo_url_to_role_name(tokens[0])
  350. if scm and not role_version:
  351. role_version = default_role_versions.get(scm, '')
  352. return dict(scm=scm, src=role_url, version=role_version, name=role_name)
  353. def role_yaml_parse(role):
  354. if 'role' in role:
  355. # Old style: {role: "galaxy.role,version,name", other_vars: "here" }
  356. role_info = role_spec_parse(role['role'])
  357. if isinstance(role_info, dict):
  358. # Warning: Slight change in behaviour here. name may be being
  359. # overloaded. Previously, name was only a parameter to the role.
  360. # Now it is both a parameter to the role and the name that
  361. # ansible-galaxy will install under on the local system.
  362. if 'name' in role and 'name' in role_info:
  363. del role_info['name']
  364. role.update(role_info)
  365. else:
  366. # New style: { src: 'galaxy.role,version,name', other_vars: "here" }
  367. if 'github.com' in role["src"] and 'http' in role["src"] and '+' not in role["src"] and not role["src"].endswith('.tar.gz'):
  368. role["src"] = "git+" + role["src"]
  369. if '+' in role["src"]:
  370. (scm, src) = role["src"].split('+')
  371. role["scm"] = scm
  372. role["src"] = src
  373. if 'name' not in role:
  374. role["name"] = repo_url_to_role_name(role["src"])
  375. if 'version' not in role:
  376. role['version'] = ''
  377. if 'scm' not in role:
  378. role['scm'] = None
  379. return role
  380. def json_loads(data):
  381. ''' parse a JSON string and return a data structure '''
  382. try:
  383. loaded = json.loads(data)
  384. except ValueError,e:
  385. raise errors.AnsibleError("Unable to read provided data as JSON: %s" % str(e))
  386. return loaded
  387. def _clean_data(orig_data, from_remote=False, from_inventory=False):
  388. ''' remove jinja2 template tags from a string '''
  389. if not isinstance(orig_data, basestring):
  390. return orig_data
  391. # when the data is marked as having come from a remote, we always
  392. # replace any print blocks (ie. {{var}}), however when marked as coming
  393. # from inventory we only replace print blocks that contain a call to
  394. # a lookup plugin (ie. {{lookup('foo','bar'))}})
  395. replace_prints = from_remote or (from_inventory and '{{' in orig_data and LOOKUP_REGEX.search(orig_data) is not None)
  396. regex = PRINT_CODE_REGEX if replace_prints else CODE_REGEX
  397. with contextlib.closing(StringIO.StringIO(orig_data)) as data:
  398. # these variables keep track of opening block locations, as we only
  399. # want to replace matched pairs of print/block tags
  400. print_openings = []
  401. block_openings = []
  402. for mo in regex.finditer(orig_data):
  403. token = mo.group(0)
  404. token_start = mo.start(0)
  405. if token[0] == '{':
  406. if token == '{%':
  407. block_openings.append(token_start)
  408. elif token == '{{':
  409. print_openings.append(token_start)
  410. elif token[1] == '}':
  411. prev_idx = None
  412. if token == '%}' and block_openings:
  413. prev_idx = block_openings.pop()
  414. elif token == '}}' and print_openings:
  415. prev_idx = print_openings.pop()
  416. if prev_idx is not None:
  417. # replace the opening
  418. data.seek(prev_idx, os.SEEK_SET)
  419. data.write('{#')
  420. # replace the closing
  421. data.seek(token_start, os.SEEK_SET)
  422. data.write('#}')
  423. else:
  424. assert False, 'Unhandled regex match'
  425. return data.getvalue()
  426. def _clean_data_struct(orig_data, from_remote=False, from_inventory=False):
  427. '''
  428. walk a complex data structure, and use _clean_data() to
  429. remove any template tags that may exist
  430. '''
  431. if not from_remote and not from_inventory:
  432. raise errors.AnsibleErrors("when cleaning data, you must specify either from_remote or from_inventory")
  433. if isinstance(orig_data, dict):
  434. data = orig_data.copy()
  435. for key in data:
  436. new_key = _clean_data_struct(key, from_remote, from_inventory)
  437. new_val = _clean_data_struct(data[key], from_remote, from_inventory)
  438. if key != new_key:
  439. del data[key]
  440. data[new_key] = new_val
  441. elif isinstance(orig_data, list):
  442. data = orig_data[:]
  443. for i in range(0, len(data)):
  444. data[i] = _clean_data_struct(data[i], from_remote, from_inventory)
  445. elif isinstance(orig_data, basestring):
  446. data = _clean_data(orig_data, from_remote, from_inventory)
  447. else:
  448. data = orig_data
  449. return data
  450. def parse_json(raw_data, from_remote=False, from_inventory=False, no_exceptions=False):
  451. ''' this version for module return data only '''
  452. orig_data = raw_data
  453. # ignore stuff like tcgetattr spewage or other warnings
  454. data = filter_leading_non_json_lines(raw_data)
  455. try:
  456. results = json.loads(data)
  457. except:
  458. if no_exceptions:
  459. return dict(failed=True, parsed=False, msg=raw_data)
  460. else:
  461. raise
  462. if from_remote:
  463. results = _clean_data_struct(results, from_remote, from_inventory)
  464. return results
  465. def serialize_args(args):
  466. '''
  467. Flattens a dictionary args to a k=v string
  468. '''
  469. module_args = ""
  470. for (k,v) in args.iteritems():
  471. if isinstance(v, basestring):
  472. module_args = "%s=%s %s" % (k, pipes.quote(v), module_args)
  473. elif isinstance(v, bool):
  474. module_args = "%s=%s %s" % (k, str(v), module_args)
  475. return module_args.strip()
  476. def merge_module_args(current_args, new_args):
  477. '''
  478. merges either a dictionary or string of k=v pairs with another string of k=v pairs,
  479. and returns a new k=v string without duplicates.
  480. '''
  481. if not isinstance(current_args, basestring):
  482. raise errors.AnsibleError("expected current_args to be a basestring")
  483. # we use parse_kv to split up the current args into a dictionary
  484. final_args = parse_kv(current_args)
  485. if isinstance(new_args, dict):
  486. final_args.update(new_args)
  487. elif isinstance(new_args, basestring):
  488. new_args_kv = parse_kv(new_args)
  489. final_args.update(new_args_kv)
  490. return serialize_args(final_args)
  491. def parse_yaml(data, path_hint=None):
  492. ''' convert a yaml string to a data structure. Also supports JSON, ssssssh!!!'''
  493. stripped_data = data.lstrip()
  494. loaded = None
  495. if stripped_data.startswith("{") or stripped_data.startswith("["):
  496. # since the line starts with { or [ we can infer this is a JSON document.
  497. try:
  498. loaded = json.loads(data)
  499. except ValueError, ve:
  500. if path_hint:
  501. raise errors.AnsibleError(path_hint + ": " + str(ve))
  502. else:
  503. raise errors.AnsibleError(str(ve))
  504. else:
  505. # else this is pretty sure to be a YAML document
  506. loaded = yaml.load(data, Loader=Loader)
  507. return loaded
  508. def process_common_errors(msg, probline, column):
  509. replaced = probline.replace(" ","")
  510. if ":{{" in replaced and "}}" in replaced:
  511. msg = msg + """
  512. This one looks easy to fix. YAML thought it was looking for the start of a
  513. hash/dictionary and was confused to see a second "{". Most likely this was
  514. meant to be an ansible template evaluation instead, so we have to give the
  515. parser a small hint that we wanted a string instead. The solution here is to
  516. just quote the entire value.
  517. For instance, if the original line was:
  518. app_path: {{ base_path }}/foo
  519. It should be written as:
  520. app_path: "{{ base_path }}/foo"
  521. """
  522. return msg
  523. elif len(probline) and len(probline) > 1 and len(probline) > column and probline[column] == ":" and probline.count(':') > 1:
  524. msg = msg + """
  525. This one looks easy to fix. There seems to be an extra unquoted colon in the line
  526. and this is confusing the parser. It was only expecting to find one free
  527. colon. The solution is just add some quotes around the colon, or quote the
  528. entire line after the first colon.
  529. For instance, if the original line was:
  530. copy: src=file.txt dest=/path/filename:with_colon.txt
  531. It can be written as:
  532. copy: src=file.txt dest='/path/filename:with_colon.txt'
  533. Or:
  534. copy: 'src=file.txt dest=/path/filename:with_colon.txt'
  535. """
  536. return msg
  537. else:
  538. parts = probline.split(":")
  539. if len(parts) > 1:
  540. middle = parts[1].strip()
  541. match = False
  542. unbalanced = False
  543. if middle.startswith("'") and not middle.endswith("'"):
  544. match = True
  545. elif middle.startswith('"') and not middle.endswith('"'):
  546. match = True
  547. if len(middle) > 0 and middle[0] in [ '"', "'" ] and middle[-1] in [ '"', "'" ] and probline.count("'") > 2 or probline.count('"') > 2:
  548. unbalanced = True
  549. if match:
  550. msg = msg + """
  551. This one looks easy to fix. It seems that there is a value started
  552. with a quote, and the YAML parser is expecting to see the line ended
  553. with the same kind of quote. For instance:
  554. when: "ok" in result.stdout
  555. Could be written as:
  556. when: '"ok" in result.stdout'
  557. or equivalently:
  558. when: "'ok' in result.stdout"
  559. """
  560. return msg
  561. if unbalanced:
  562. msg = msg + """
  563. We could be wrong, but this one looks like it might be an issue with
  564. unbalanced quotes. If starting a value with a quote, make sure the
  565. line ends with the same set of quotes. For instance this arbitrary
  566. example:
  567. foo: "bad" "wolf"
  568. Could be written as:
  569. foo: '"bad" "wolf"'
  570. """
  571. return msg
  572. return msg
  573. def process_yaml_error(exc, data, path=None, show_content=True):
  574. if hasattr(exc, 'problem_mark'):
  575. mark = exc.problem_mark
  576. if show_content:
  577. if mark.line -1 >= 0:
  578. before_probline = data.split("\n")[mark.line-1]
  579. else:
  580. before_probline = ''
  581. probline = data.split("\n")[mark.line]
  582. arrow = " " * mark.column + "^"
  583. msg = """Syntax Error while loading YAML script, %s
  584. Note: The error may actually appear before this position: line %s, column %s
  585. %s
  586. %s
  587. %s""" % (path, mark.line + 1, mark.column + 1, before_probline, probline, arrow)
  588. unquoted_var = None
  589. if '{{' in probline and '}}' in probline:
  590. if '"{{' not in probline or "'{{" not in probline:
  591. unquoted_var = True
  592. if not unquoted_var:
  593. msg = process_common_errors(msg, probline, mark.column)
  594. else:
  595. msg = msg + """
  596. We could be wrong, but this one looks like it might be an issue with
  597. missing quotes. Always quote template expression brackets when they
  598. start a value. For instance:
  599. with_items:
  600. - {{ foo }}
  601. Should be written as:
  602. with_items:
  603. - "{{ foo }}"
  604. """
  605. else:
  606. # most likely displaying a file with sensitive content,
  607. # so don't show any of the actual lines of yaml just the
  608. # line number itself
  609. msg = """Syntax error while loading YAML script, %s
  610. The error appears to have been on line %s, column %s, but may actually
  611. be before there depending on the exact syntax problem.
  612. """ % (path, mark.line + 1, mark.column + 1)
  613. else:
  614. # No problem markers means we have to throw a generic
  615. # "stuff messed up" type message. Sry bud.
  616. if path:
  617. msg = "Could not parse YAML. Check over %s again." % path
  618. else:
  619. msg = "Could not parse YAML."
  620. raise errors.AnsibleYAMLValidationFailed(msg)
  621. def parse_yaml_from_file(path, vault_password=None):
  622. ''' convert a yaml file to a data structure '''
  623. data = None
  624. show_content = True
  625. try:
  626. data = open(path).read()
  627. except IOError:
  628. raise errors.AnsibleError("file could not read: %s" % path)
  629. vault = VaultLib(password=vault_password)
  630. if vault.is_encrypted(data):
  631. # if the file is encrypted and no password was specified,
  632. # the decrypt call would throw an error, but we check first
  633. # since the decrypt function doesn't know the file name
  634. if vault_password is None:
  635. raise errors.AnsibleError("A vault password must be specified to decrypt %s" % path)
  636. data = vault.decrypt(data)
  637. show_content = False
  638. try:
  639. return parse_yaml(data, path_hint=path)
  640. except yaml.YAMLError, exc:
  641. process_yaml_error(exc, data, path, show_content)
  642. def parse_kv(args):
  643. ''' convert a string of key/value items to a dict '''
  644. options = {}
  645. if args is not None:
  646. try:
  647. vargs = split_args(args)
  648. except ValueError, ve:
  649. if 'no closing quotation' in str(ve).lower():
  650. raise errors.AnsibleError("error parsing argument string, try quoting the entire line.")
  651. else:
  652. raise
  653. for x in vargs:
  654. if "=" in x:
  655. k, v = x.split("=",1)
  656. options[k.strip()] = unquote(v.strip())
  657. return options
  658. def _validate_both_dicts(a, b):
  659. if not (isinstance(a, dict) and isinstance(b, dict)):
  660. raise errors.AnsibleError(
  661. "failed to combine variables, expected dicts but got a '%s' and a '%s'" % (type(a).__name__, type(b).__name__)
  662. )
  663. def merge_hash(a, b):
  664. ''' recursively merges hash b into a
  665. keys from b take precedence over keys from a '''
  666. result = {}
  667. # we check here as well as in combine_vars() since this
  668. # function can work recursively with nested dicts
  669. _validate_both_dicts(a, b)
  670. for dicts in a, b:
  671. # next, iterate over b keys and values
  672. for k, v in dicts.iteritems():
  673. # if there's already such key in a
  674. # and that key contains dict
  675. if k in result and isinstance(result[k], dict):
  676. # merge those dicts recursively
  677. result[k] = merge_hash(a[k], v)
  678. else:
  679. # otherwise, just copy a value from b to a
  680. result[k] = v
  681. return result
  682. def default(value, function):
  683. ''' syntactic sugar around lazy evaluation of defaults '''
  684. if value is None:
  685. return function()
  686. return value
  687. def _git_repo_info(repo_path):
  688. ''' returns a string containing git branch, commit id and commit date '''
  689. result = None
  690. if os.path.exists(repo_path):
  691. # Check if the .git is a file. If it is a file, it means that we are in a submodule structure.
  692. if os.path.isfile(repo_path):
  693. try:
  694. gitdir = yaml.safe_load(open(repo_path)).get('gitdir')
  695. # There is a possibility the .git file to have an absolute path.
  696. if os.path.isabs(gitdir):
  697. repo_path = gitdir
  698. else:
  699. repo_path = os.path.join(repo_path[:-4], gitdir)
  700. except (IOError, AttributeError):
  701. return ''
  702. f = open(os.path.join(repo_path, "HEAD"))
  703. branch = f.readline().split('/')[-1].rstrip("\n")
  704. f.close()
  705. branch_path = os.path.join(repo_path, "refs", "heads", branch)
  706. if os.path.exists(branch_path):
  707. f = open(branch_path)
  708. commit = f.readline()[:10]
  709. f.close()
  710. else:
  711. # detached HEAD
  712. commit = branch[:10]
  713. branch = 'detached HEAD'
  714. branch_path = os.path.join(repo_path, "HEAD")
  715. date = time.localtime(os.stat(branch_path).st_mtime)
  716. if time.daylight == 0:
  717. offset = time.timezone
  718. else:
  719. offset = time.altzone
  720. result = "({0} {1}) last updated {2} (GMT {3:+04d})".format(branch, commit,
  721. time.strftime("%Y/%m/%d %H:%M:%S", date), offset / -36)
  722. else:
  723. result = ''
  724. return result
  725. def _gitinfo():
  726. basedir = os.path.join(os.path.dirname(__file__), '..', '..', '..')
  727. repo_path = os.path.join(basedir, '.git')
  728. result = _git_repo_info(repo_path)
  729. submodules = os.path.join(basedir, '.gitmodules')
  730. if not os.path.exists(submodules):
  731. return result
  732. f = open(submodules)
  733. for line in f:
  734. tokens = line.strip().split(' ')
  735. if tokens[0] == 'path':
  736. submodule_path = tokens[2]
  737. submodule_info =_git_repo_info(os.path.join(basedir, submodule_path, '.git'))
  738. if not submodule_info:
  739. submodule_info = ' not found - use git submodule update --init ' + submodule_path
  740. result += "\n {0}: {1}".format(submodule_path, submodule_info)
  741. f.close()
  742. return result
  743. def version(prog):
  744. result = "{0} {1}".format(prog, __version__)
  745. gitinfo = _gitinfo()
  746. if gitinfo:
  747. result = result + " {0}".format(gitinfo)
  748. result = result + "\n configured module search path = %s" % C.DEFAULT_MODULE_PATH
  749. return result
  750. def version_info(gitinfo=False):
  751. if gitinfo:
  752. # expensive call, user with care
  753. ansible_version_string = version('')
  754. else:
  755. ansible_version_string = __version__
  756. ansible_version = ansible_version_string.split()[0]
  757. ansible_versions = ansible_version.split('.')
  758. for counter in range(len(ansible_versions)):
  759. if ansible_versions[counter] == "":
  760. ansible_versions[counter] = 0
  761. try:
  762. ansible_versions[counter] = int(ansible_versions[counter])
  763. except:
  764. pass
  765. if len(ansible_versions) < 3:
  766. for counter in range(len(ansible_versions), 3):
  767. ansible_versions.append(0)
  768. return {'string': ansible_version_string.strip(),
  769. 'full': ansible_version,
  770. 'major': ansible_versions[0],
  771. 'minor': ansible_versions[1],
  772. 'revision': ansible_versions[2]}
  773. def getch():
  774. ''' read in a single character '''
  775. fd = sys.stdin.fileno()
  776. old_settings = termios.tcgetattr(fd)
  777. try:
  778. tty.setraw(sys.stdin.fileno())
  779. ch = sys.stdin.read(1)
  780. finally:
  781. termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
  782. return ch
  783. def sanitize_output(arg_string):
  784. ''' strips private info out of a string '''
  785. private_keys = ('password', 'login_password')
  786. output = []
  787. for part in arg_string.split():
  788. try:
  789. (k, v) = part.split('=', 1)
  790. except ValueError:
  791. v = heuristic_log_sanitize(part)
  792. output.append(v)
  793. continue
  794. if k in private_keys:
  795. v = 'VALUE_HIDDEN'
  796. else:
  797. v = heuristic_log_sanitize(v)
  798. output.append('%s=%s' % (k, v))
  799. output = ' '.join(output)
  800. return output
  801. ####################################################################
  802. # option handling code for /usr/bin/ansible and ansible-playbook
  803. # below this line
  804. class SortedOptParser(optparse.OptionParser):
  805. '''Optparser which sorts the options by opt before outputting --help'''
  806. def format_help(self, formatter=None):
  807. self.option_list.sort(key=operator.methodcaller('get_opt_string'))
  808. return optparse.OptionParser.format_help(self, formatter=None)
  809. def increment_debug(option, opt, value, parser):
  810. global VERBOSITY
  811. VERBOSITY += 1
  812. def base_parser(constants=C, usage="", output_opts=False, runas_opts=False,
  813. async_opts=False, connect_opts=False, subset_opts=False, check_opts=False, diff_opts=False):
  814. ''' create an options parser for any ansible script '''
  815. parser = SortedOptParser(usage, version=version("%prog"))
  816. parser.add_option('-v','--verbose', default=False, action="callback",
  817. callback=increment_debug, help="verbose mode (-vvv for more, -vvvv to enable connection debugging)")
  818. parser.add_option('-f','--forks', dest='forks', default=constants.DEFAULT_FORKS, type='int',
  819. help="specify number of parallel processes to use (default=%s)" % constants.DEFAULT_FORKS)
  820. parser.add_option('-i', '--inventory-file', dest='inventory',
  821. help="specify inventory host file (default=%s)" % constants.DEFAULT_HOST_LIST,
  822. default=constants.DEFAULT_HOST_LIST)
  823. parser.add_option('-e', '--extra-vars', dest="extra_vars", action="append",
  824. help="set additional variables as key=value or YAML/JSON", default=[])
  825. parser.add_option('-u', '--user', default=constants.DEFAULT_REMOTE_USER, dest='remote_user',
  826. help='connect as this user (default=%s)' % constants.DEFAULT_REMOTE_USER)
  827. parser.add_option('-k', '--ask-pass', default=False, dest='ask_pass', action='store_true',
  828. help='ask for SSH password')
  829. parser.add_option('--private-key', default=constants.DEFAULT_PRIVATE_KEY_FILE, dest='private_key_file',
  830. help='use this file to authenticate the connection')
  831. parser.add_option('--ask-vault-pass', default=False, dest='ask_vault_pass', action='store_true',
  832. help='ask for vault password')
  833. parser.add_option('--vault-password-file', default=constants.DEFAULT_VAULT_PASSWORD_FILE,
  834. dest='vault_password_file', help="vault password file")
  835. parser.add_option('--list-hosts', dest='listhosts', action='store_true',
  836. help='outputs a list of matching hosts; does not execute anything else')
  837. parser.add_option('-M', '--module-path', dest='module_path',
  838. help="specify path(s) to module library (default=%s)" % constants.DEFAULT_MODULE_PATH,
  839. default=None)
  840. if subset_opts:
  841. parser.add_option('-l', '--limit', default=constants.DEFAULT_SUBSET, dest='subset',
  842. help='further limit selected hosts to an additional pattern')
  843. parser.add_option('-T', '--timeout', default=constants.DEFAULT_TIMEOUT, type='int',
  844. dest='timeout',
  845. help="override the SSH timeout in seconds (default=%s)" % constants.DEFAULT_TIMEOUT)
  846. if output_opts:
  847. parser.add_option('-o', '--one-line', dest='one_line', action='store_true',
  848. help='condense output')
  849. parser.add_option('-t', '--tree', dest='tree', default=None,
  850. help='log output to this directory')
  851. if runas_opts:
  852. # priv user defaults to root later on to enable detecting when this option was given here
  853. parser.add_option('-K', '--ask-sudo-pass', default=constants.DEFAULT_ASK_SUDO_PASS, dest='ask_sudo_pass', action='store_true',
  854. help='ask for sudo password (deprecated, use become)')
  855. parser.add_option('--ask-su-pass', default=constants.DEFAULT_ASK_SU_PASS, dest='ask_su_pass', action='store_true',
  856. help='ask for su password (deprecated, use become)')
  857. parser.add_option("-s", "--sudo", default=constants.DEFAULT_SUDO, action="store_true", dest='sudo',
  858. help="run operations with sudo (nopasswd) (deprecated, use become)")
  859. parser.add_option('-U', '--sudo-user', dest='sudo_user', default=None,
  860. help='desired sudo user (default=root) (deprecated, use become)')
  861. parser.add_option('-S', '--su', default=constants.DEFAULT_SU, action='store_true',
  862. help='run operations with su (deprecated, use become)')
  863. parser.add_option('-R', '--su-user', default=None,
  864. help='run operations with su as this user (default=%s) (deprecated, use become)' % constants.DEFAULT_SU_USER)
  865. # consolidated privilege escalation (become)
  866. parser.add_option("-b", "--become", default=constants.DEFAULT_BECOME, action="store_true", dest='become',
  867. help="run operations with become (nopasswd implied)")
  868. parser.add_option('--become-method', dest='become_method', default=constants.DEFAULT_BECOME_METHOD, type='string',
  869. help="privilege escalation method to use (default=%s), valid choices: [ %s ]" % (constants.DEFAULT_BECOME_METHOD, ' | '.join(constants.BECOME_METHODS)))
  870. parser.add_option('--become-user', default=None, dest='become_user', type='string',
  871. help='run operations as this user (default=%s)' % constants.DEFAULT_BECOME_USER)
  872. parser.add_option('--ask-become-pass', default=False, dest='become_ask_pass', action='store_true',
  873. help='ask for privilege escalation password')
  874. if connect_opts:
  875. parser.add_option('-c', '--connection', dest='connection',
  876. default=constants.DEFAULT_TRANSPORT,
  877. help="connection type to use (default=%s)" % constants.DEFAULT_TRANSPORT)
  878. if async_opts:
  879. parser.add_option('-P', '--poll', default=constants.DEFAULT_POLL_INTERVAL, type='int',
  880. dest='poll_interval',
  881. help="set the poll interval if using -B (default=%s)" % constants.DEFAULT_POLL_INTERVAL)
  882. parser.add_option('-B', '--background', dest='seconds', type='int', default=0,
  883. help='run asynchronously, failing after X seconds (default=N/A)')
  884. if check_opts:
  885. parser.add_option("-C", "--check", default=False, dest='check', action='store_true',
  886. help="don't make any changes; instead, try to predict some of the changes that may occur"
  887. )
  888. if diff_opts:
  889. parser.add_option("-D", "--diff", default=False, dest='diff', action='store_true',
  890. help="when changing (small) files and templates, show the differences in those files; works great with --check"
  891. )
  892. return parser
  893. def parse_extra_vars(extra_vars_opts, vault_pass):
  894. extra_vars = {}
  895. for extra_vars_opt in extra_vars_opts:
  896. extra_vars_opt = to_unicode(extra_vars_opt)
  897. if extra_vars_opt.startswith(u"@"):
  898. # Argument is a YAML file (JSON is a subset of YAML)
  899. extra_vars = combine_vars(extra_vars, parse_yaml_from_file(extra_vars_opt[1:], vault_password=vault_pass))
  900. elif extra_vars_opt and extra_vars_opt[0] in u'[{':
  901. # Arguments as YAML
  902. extra_vars = combine_vars(extra_vars, parse_yaml(extra_vars_opt))
  903. else:
  904. # Arguments as Key-value
  905. extra_vars = combine_vars(extra_vars, parse_kv(extra_vars_opt))
  906. return extra_vars
  907. def ask_vault_passwords(ask_vault_pass=False, ask_new_vault_pass=False, confirm_vault=False, confirm_new=False):
  908. vault_pass = None
  909. new_vault_pass = None
  910. if ask_vault_pass:
  911. vault_pass = getpass.getpass(prompt="Vault password: ")
  912. if ask_vault_pass and confirm_vault:
  913. vault_pass2 = getpass.getpass(prompt="Confirm Vault password: ")
  914. if vault_pass != vault_pass2:
  915. raise errors.AnsibleError("Passwords do not match")
  916. if ask_new_vault_pass:
  917. new_vault_pass = getpass.getpass(prompt="New Vault password: ")
  918. if ask_new_vault_pass and confirm_new:
  919. new_vault_pass2 = getpass.getpass(prompt="Confirm New Vault password: ")
  920. if new_vault_pass != new_vault_pass2:
  921. raise errors.AnsibleError("Passwords do not match")
  922. # enforce no newline chars at the end of passwords
  923. if vault_pass:
  924. vault_pass = to_bytes(vault_pass, errors='strict', nonstring='simplerepr').strip()
  925. if new_vault_pass:
  926. new_vault_pass = to_bytes(new_vault_pass, errors='strict', nonstring='simplerepr').strip()
  927. return vault_pass, new_vault_pass
  928. def ask_passwords(ask_pass=False, become_ask_pass=False, ask_vault_pass=False, become_method=C.DEFAULT_BECOME_METHOD):
  929. sshpass = None
  930. becomepass = None
  931. vaultpass = None
  932. become_prompt = ''
  933. if ask_pass:
  934. sshpass = getpass.getpass(prompt="SSH password: ")
  935. become_prompt = "%s password[defaults to SSH password]: " % become_method.upper()
  936. if sshpass:
  937. sshpass = to_bytes(sshpass, errors='strict', nonstring='simplerepr')
  938. else:
  939. become_prompt = "%s password: " % become_method.upper()
  940. if become_ask_pass:
  941. becomepass = getpass.getpass(prompt=become_prompt)
  942. if ask_pass and becomepass == '':
  943. becomepass = sshpass
  944. if becomepass:
  945. becomepass = to_bytes(becomepass)
  946. if ask_vault_pass:
  947. vaultpass = getpass.getpass(prompt="Vault password: ")
  948. if vaultpass:
  949. vaultpass = to_bytes(vaultpass, errors='strict', nonstring='simplerepr').strip()
  950. return (sshpass, becomepass, vaultpass)
  951. def choose_pass_prompt(options):
  952. if options.ask_su_pass:
  953. return 'su'
  954. elif options.ask_sudo_pass:
  955. return 'sudo'
  956. return options.become_method
  957. def normalize_become_options(options):
  958. options.become_ask_pass = options.become_ask_pass or options.ask_sudo_pass or options.ask_su_pass or C.DEFAULT_BECOME_ASK_PASS
  959. options.become_user = options.become_user or options.sudo_user or options.su_user or C.DEFAULT_BECOME_USER
  960. if options.become:
  961. pass
  962. elif options.sudo:
  963. options.become = True
  964. options.become_method = 'sudo'
  965. elif options.su:
  966. options.become = True
  967. options.become_method = 'su'
  968. def do_encrypt(result, encrypt, salt_size=None, salt=None):
  969. if PASSLIB_AVAILABLE:
  970. try:
  971. crypt = getattr(passlib.hash, encrypt)
  972. except:
  973. raise errors.AnsibleError("passlib does not support '%s' algorithm" % encrypt)
  974. if salt_size:
  975. result = crypt.encrypt(result, salt_size=salt_size)
  976. elif salt:
  977. result = crypt.encrypt(result, salt=salt)
  978. else:
  979. result = crypt.encrypt(result)
  980. else:
  981. raise errors.AnsibleError("passlib must be installed to encrypt vars_prompt values")
  982. return result
  983. def last_non_blank_line(buf):
  984. all_lines = buf.splitlines()
  985. all_lines.reverse()
  986. for line in all_lines:
  987. if (len(line) > 0):
  988. return line
  989. # shouldn't occur unless there's no output
  990. return ""
  991. def filter_leading_non_json_lines(buf):
  992. '''
  993. used to avoid random output from SSH at the top of JSON output, like messages from
  994. tcagetattr, or where dropbear spews MOTD on every single command (which is nuts).
  995. need to filter anything which starts not with '{', '[', ', '=' or is an empty line.
  996. filter only leading lines since multiline JSON is valid.
  997. '''
  998. filtered_lines = StringIO.StringIO()
  999. stop_filtering = False
  1000. for line in buf.splitlines():
  1001. if stop_filtering or line.startswith('{') or line.startswith('['):
  1002. stop_filtering = True
  1003. filtered_lines.write(line + '\n')
  1004. return filtered_lines.getvalue()
  1005. def boolean(value):
  1006. val = str(value)
  1007. if val.lower() in [ "true", "t", "y", "1", "yes" ]:
  1008. return True
  1009. else:
  1010. return False
  1011. def make_become_cmd(cmd, user, shell, method, flags=None, exe=None):
  1012. """
  1013. helper function for connection plugins to create privilege escalation commands
  1014. """
  1015. randbits = ''.join(chr(random.randint(ord('a'), ord('z'))) for x in xrange(32))
  1016. success_key = 'BECOME-SUCCESS-%s' % randbits
  1017. prompt = None
  1018. becomecmd = None
  1019. shell = shell or '$SHELL'
  1020. if method == 'sudo':
  1021. # Rather than detect if sudo wants a password this time, -k makes sudo always ask for
  1022. # a password if one is required. Passing a quoted compound command to sudo (or sudo -s)
  1023. # directly doesn't work, so we shellquote it with pipes.quote() and pass the quoted
  1024. # string to the user's shell. We loop reading output until we see the randomly-generated
  1025. # sudo prompt set with the -p option.
  1026. prompt = '[sudo via ansible, key=%s] password: ' % randbits
  1027. exe = exe or C.DEFAULT_SUDO_EXE
  1028. becomecmd = '%s -k && %s %s -S -p "%s" -u %s %s -c %s' % \
  1029. (exe, exe, flags or C.DEFAULT_SUDO_FLAGS, prompt, user, shell, pipes.quote('echo %s; %s' % (success_key, cmd)))
  1030. elif method == 'su':
  1031. exe = exe or C.DEFAULT_SU_EXE
  1032. flags = flags or C.DEFAULT_SU_FLAGS
  1033. becomecmd = '%s %s %s -c "%s -c %s"' % (exe, flags, user, shell, pipes.quote('echo %s; %s' % (success_key, cmd)))
  1034. elif method == 'pbrun':
  1035. prompt = 'assword:'
  1036. exe = exe or 'pbrun'
  1037. flags = flags or ''
  1038. becomecmd = '%s -b -l %s -u %s "%s"' % (exe, flags, user, pipes.quote('echo %s; %s' % (success_key,cmd)))
  1039. elif method == 'pfexec':
  1040. exe = exe or 'pfexec'
  1041. flags = flags or ''
  1042. # No user as it uses it's own exec_attr to figure it out
  1043. becomecmd = '%s %s "%s"' % (exe, flags, pipes.quote('echo %s; %s' % (success_key,cmd)))
  1044. if becomecmd is None:
  1045. raise errors.AnsibleError("Privilege escalation method not found: %s" % method)
  1046. return (('%s -c ' % shell) + pipes.quote(becomecmd), prompt, success_key)
  1047. def make_sudo_cmd(sudo_exe, sudo_user, executable, cmd):
  1048. """
  1049. helper function for connection plugins to create sudo commands
  1050. """
  1051. return make_become_cmd(cmd, sudo_user, executable, 'sudo', C.DEFAULT_SUDO_FLAGS, sudo_exe)
  1052. def make_su_cmd(su_user, executable, cmd):
  1053. """
  1054. Helper function for connection plugins to create direct su commands
  1055. """
  1056. return make_become_cmd(cmd, su_user, executable, 'su', C.DEFAULT_SU_FLAGS, C.DEFAULT_SU_EXE)
  1057. def get_diff(diff):
  1058. # called by --diff usage in playbook and runner via callbacks
  1059. # include names in diffs 'before' and 'after' and do diff -U 10
  1060. try:
  1061. with warnings.catch_warnings():
  1062. warnings.simplefilter('ignore')
  1063. ret = []
  1064. if 'dst_binary' in diff:
  1065. ret.append("diff skipped: destination file appears to be binary\n")
  1066. if 'src_binary' in diff:
  1067. ret.append("diff skipped: source file appears to be binary\n")
  1068. if 'dst_larger' in diff:
  1069. ret.append("diff skipped: destination file size is greater than %d\n" % diff['dst_larger'])
  1070. if 'src_larger' in diff:
  1071. ret.append("diff skipped: source file size is greater than %d\n" % diff['src_larger'])
  1072. if 'before' in diff and 'after' in diff:
  1073. if 'before_header' in diff:
  1074. before_header = "before: %s" % diff['before_header']
  1075. else:
  1076. before_header = 'before'
  1077. if 'after_header' in diff:
  1078. after_header = "after: %s" % diff['after_header']
  1079. else:
  1080. after_header = 'after'
  1081. differ = difflib.unified_diff(to_unicode(diff['before']).splitlines(True), to_unicode(diff['after']).splitlines(True), before_header, after_header, '', '', 10)
  1082. for line in list(differ):
  1083. ret.append(line)
  1084. return u"".join(ret)
  1085. except UnicodeDecodeError:
  1086. return ">> the files are different, but the diff library cannot compare unicode strings"
  1087. def is_list_of_strings(items):
  1088. for x in items:
  1089. if not isinstance(x, basestring):
  1090. return False
  1091. return True
  1092. def list_union(a, b):
  1093. result = []
  1094. for x in a:
  1095. if x not in result:
  1096. result.append(x)
  1097. for x in b:
  1098. if x not in result:
  1099. result.append(x)
  1100. return result
  1101. def list_intersection(a, b):
  1102. result = []
  1103. for x in a:
  1104. if x in b and x not in result:
  1105. result.append(x)
  1106. return result
  1107. def list_difference(a, b):
  1108. result = []
  1109. for x in a:
  1110. if x not in b and x not in result:
  1111. result.append(x)
  1112. for x in b:
  1113. if x not in a and x not in result:
  1114. result.append(x)
  1115. return result
  1116. def contains_vars(data):
  1117. '''
  1118. returns True if the data contains a variable pattern
  1119. '''
  1120. return "$" in data or "{{" in data
  1121. def safe_eval(expr, locals=None, include_exceptions=False):
  1122. '''
  1123. This is intended for allowing things like:
  1124. with_items: a_list_variable
  1125. Where Jinja2 would return a string but we do not want to allow it to
  1126. call functions (outside