text
stringlengths
0
828
'ANSIBLE_FORKS': '10',
'ANSIBLE_HOST_KEY_CHECKING': 'no',
'ANSIBLE_PRIVATE_KEY_FILE': cluster.user_key_private,
'ANSIBLE_ROLES_PATH': ':'.join(reversed(ansible_roles_dirs)),
'ANSIBLE_SSH_PIPELINING': 'yes',
'ANSIBLE_TIMEOUT': '120',
}
# ...override them with key/values set in the config file(s)
for k, v in self.extra_conf.items():
if k.startswith('ansible_'):
ansible_env[k.upper()] = str(v)
# ...finally allow the environment have the final word
ansible_env.update(os.environ)
if __debug__:
elasticluster.log.debug(
""Calling `ansible-playbook` with the following environment:"")
for var, value in sorted(ansible_env.items()):
elasticluster.log.debug(""- %s=%r"", var, value)
elasticluster.log.debug(""Using playbook file %s."", self._playbook_path)
# build `ansible-playbook` command-line
cmd = shlex.split(self.extra_conf.get('ansible_command', 'ansible-playbook'))
cmd += [
os.path.realpath(self._playbook_path),
('--inventory=' + inventory_path),
] + list(extra_args)
if self._sudo:
cmd.extend([
# force all plays to use `sudo` (even if not marked as such)
'--become',
# desired sudo-to user
('--become-user=' + self._sudo_user),
])
# determine Ansible verbosity as a function of ElastiCluster's
# log level (we cannot read `ElastiCluster().params.verbose`
# here, still we can access the log configuration since it's
# global).
verbosity = (logging.WARNING - elasticluster.log.getEffectiveLevel()) / 10
if verbosity > 0:
cmd.append('-' + ('v' * verbosity)) # e.g., `-vv`
# append any additional arguments provided by users
ansible_extra_args = self.extra_conf.get('ansible_extra_args', None)
if ansible_extra_args:
cmd += shlex.split(ansible_extra_args)
cmdline = ' '.join(cmd)
elasticluster.log.debug(""Running Ansible command `%s` ..."", cmdline)
rc = call(cmd, env=ansible_env, bufsize=1, close_fds=True)
if rc == 0:
elasticluster.log.info(""Cluster correctly configured."")
return True
else:
elasticluster.log.error(
""Command `%s` failed with exit code %d."", cmdline, rc)
elasticluster.log.error(
""Check the output lines above for additional information on this error."")
elasticluster.log.error(
""The cluster has likely *not* been configured correctly.""
"" You may need to re-run `elasticluster setup` or fix the playbooks."")
return False"
4612,"def _build_inventory(self, cluster):
""""""
Builds the inventory for the given cluster and returns its path
:param cluster: cluster to build inventory for
:type cluster: :py:class:`elasticluster.cluster.Cluster`
""""""
inventory_data = defaultdict(list)
for node in cluster.get_all_nodes():
if node.kind not in self.groups:
# FIXME: should this raise a `ConfigurationError` instead?
warn(""Node kind `{0}` not defined in cluster!"".format(node.kind))
continue
extra_vars = ['ansible_user=%s' % node.image_user]
# check for nonstandard port, either IPv4 or IPv6
if node.preferred_ip and ':' in node.preferred_ip:
match = IPV6_RE.match(node.preferred_ip)
if match:
host_port = match.groups()[1]
else:
_, _, host_port = node.preferred_ip.partition(':')
if host_port:
extra_vars.append('ansible_port=%s' % host_port)
if node.kind in self.environment:
extra_vars.extend('%s=%s' % (k, v) for k, v in
self.environment[node.kind].items())
for group in self.groups[node.kind]:
connection_ip = node.preferred_ip
if connection_ip:
inventory_data[group].append(
(node.name, connection_ip, str.join(' ', extra_vars)))
if not inventory_data:
elasticluster.log.info(""No inventory file was created."")