commit 7c144111028bec16c4a8b58495b107884f7d6495
Author: Nicolas Chauvet <kwizart(a)gmail.com>
Date: Tue Feb 6 12:20:11 2018 +0100
sync scripts
scripts/freezelist | 20 +++++++-------------
scripts/hosts_with_var_set | 22 +++++++---------------
scripts/list-vms-per-host | 12 ++++++++++--
scripts/vhost-info | 37 +++++++++++++++++--------------------
scripts/which_playbook | 15 +++++++--------
5 files changed, 48 insertions(+), 58 deletions(-)
---
diff --git a/scripts/freezelist b/scripts/freezelist
index bc381a4..2690a54 100755
--- a/scripts/freezelist
+++ b/scripts/freezelist
@@ -9,30 +9,24 @@ import sys
from optparse import OptionParser
from ansible.parsing.dataloader import DataLoader
-from ansible.vars import VariableManager
-from ansible.inventory import Inventory
+from ansible.vars.manager import VariableManager
+from ansible.inventory.manager import InventoryManager
+from ansible import constants as C
parser = OptionParser(version="1.0")
-parser.add_option('-i', dest='inventory', default=None,
+parser.add_option('-i', dest='inventory', default=C.DEFAULT_HOST_LIST,
help="Path to inventory file/dir")
opts, args = parser.parse_args(sys.argv[1:])
-variable_manager = VariableManager()
loader = DataLoader()
-
-
-if opts.inventory:
- inv = Inventory(loader=loader,variable_manager=variable_manager,
host_list=opts.inventory)
-else:
- inv = Inventory(loader=loader,variable_manager=variable_manager)
-
-variable_manager.set_inventory(inv)
+inv = InventoryManager(loader=loader, sources=opts.inventory)
+variable_manager = VariableManager(loader=loader, inventory=inv)
frozen = []
unfrozen = []
for host in sorted(inv.get_hosts()):
- vars = variable_manager.get_vars(loader=loader, host=host)
+ vars = variable_manager.get_vars(host=host)
freezes = vars.get('freezes', None)
if freezes:
diff --git a/scripts/hosts_with_var_set b/scripts/hosts_with_var_set
index 19a2858..ec35858 100755
--- a/scripts/hosts_with_var_set
+++ b/scripts/hosts_with_var_set
@@ -5,15 +5,16 @@
# list all hosts with their corresponding vars
# Note that the script will attempt to "match" the supplied value of the var
against the values if it the var is multivalued
+from ansible import constants as C
from ansible.parsing.dataloader import DataLoader
-from ansible.vars import VariableManager
-from ansible.inventory import Inventory
+from ansible.vars.manager import VariableManager
+from ansible.inventory.manager import InventoryManager
import sys
from optparse import OptionParser
parser = OptionParser(version="1.0")
-parser.add_option('-i', dest='inventory', default=None,
+parser.add_option('-i', dest='inventory', default=C.DEFAULT_HOST_LIST,
help="Path to inventory file/dir")
parser.add_option('-o', dest='variable', default=None,
help="variable name to check")
@@ -25,20 +26,11 @@ if ((opts.variable == None and opts.all_vars == None) or
(opts.variable != None
print "Usage: hosts_with_var_set -o varname[=value] | -a"
sys.exit(-1)
-variable_manager = VariableManager()
loader = DataLoader()
-
-if opts.inventory:
- inv = Inventory(loader=loader,variable_manager=variable_manager,
host_list=opts.inventory)
-else:
- inv = Inventory(loader=loader,variable_manager=variable_manager)
-
-variable_manager.set_inventory(inv)
-
-
+inv = InventoryManager(loader=loader, sources=opts.inventory)
+variable_manager = VariableManager(loader=loader, inventory=inv)
matching=True
-
if opts.variable != None:
if opts.variable.find("=") == -1:
matching=False
@@ -51,7 +43,7 @@ if opts.variable != None:
var_set = []
for host in sorted(inv.get_hosts()):
- vars = variable_manager.get_vars(loader=loader, host=host)
+ vars = variable_manager.get_vars(host=host)
if opts.variable == None:
# remove expanded 'all' groups
vars.pop('groups')
diff --git a/scripts/list-vms-per-host b/scripts/list-vms-per-host
index a689a2b..def0d9b 100755
--- a/scripts/list-vms-per-host
+++ b/scripts/list-vms-per-host
@@ -1,6 +1,6 @@
#!/usr/bin/python -tt
# Author: Toshio Kuratomi <toshio(a)fedoraproject.org>
-# Copyright: December, 2015
+# Copyright: December 2015, November 2016
# License: LGPLv3+
import sys
import copy
@@ -14,6 +14,9 @@ from ansible.cli.adhoc import AdHocCLI
class ResultAccumulator(CallbackBase):
+ CALLBACK_VERSION = 2.0
+ CALLBACK_NAME = 'accumulator'
+
def __init__(self, *args, **kwargs):
super(ResultAccumulator, self).__init__(*args, **kwargs)
self.unreachable = set()
@@ -23,7 +26,12 @@ class ResultAccumulator(CallbackBase):
self.unreachable.add(result._host.get_name())
def v2_runner_on_ok(self, result, *args, **kwargs):
- for vm in (vm for vm in result._result.keys() if vm not in ('invocation',
'changed', '_ansible_no_log')):
+ for vm in result._result.keys():
+ if not '.fedoraproject.org' in vm:
+ # Seemingly, Ansible keeps adding more random keys to the
+ # result dict every time, so let's just kill the tailing
+ # once and for all. If it doesn't look like a hostname, ignore.
+ continue
self.host_status[(result._host.get_name(), vm)] =
(result._result[vm]['state'], str(result._result[vm]['autostart']))
diff --git a/scripts/vhost-info b/scripts/vhost-info
index 3a32d34..740aad5 100755
--- a/scripts/vhost-info
+++ b/scripts/vhost-info
@@ -8,8 +8,9 @@
import sys
from collections import namedtuple
from ansible.parsing.dataloader import DataLoader
-from ansible.vars import VariableManager
-from ansible.inventory import Inventory
+from ansible.vars.manager import VariableManager
+from ansible.inventory.manager import InventoryManager
+from ansible import constants as C
from ansible.playbook.play import Play
from ansible.executor.task_queue_manager import TaskQueueManager
from ansible.plugins.callback import CallbackBase
@@ -50,7 +51,7 @@ class OutputCallback(CallbackBase):
parser = OptionParser(version = "1.0")
parser.add_option('--host', default=[], action='append', help="hosts
to act on, defaults to virtservers")
-parser.add_option('--hosts-from-file', default=None, dest="host_file",
help="read list of hosts from this file")
+parser.add_option('--hosts-from-file', default=C.DEFAULT_HOST_LIST,
dest="host_file", help="read list of hosts from this file")
(opts, args) = parser.parse_args(sys.argv[1:])
if not opts.host:
@@ -60,23 +61,17 @@ else:
-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', 'timeout'])
+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', 'timeout',
'diff'])
-# initialize needed objects
-variable_manager = VariableManager()
-loader = DataLoader()
-
-options = Options(connection='ssh', module_path=None, forks=25, 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, timeout=10)
+options = Options(connection='ssh', module_path=None, forks=25, 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, timeout=10, diff=False)
# create inventory and pass to var manager
-if opts.host_file:
- inventory = Inventory(loader=loader, variable_manager=variable_manager,
host_list=opts.host_file)
-else:
- inventory = Inventory(loader=loader, variable_manager=variable_manager)
-
-variable_manager.set_inventory(inventory)
+loader = DataLoader()
+inv = InventoryManager(loader=loader, sources=opts.host_file)
+variable_manager = VariableManager(loader=loader, inventory=inv)
+unpatched_spectre =
loader.load_from_file('/srv/private/ansible/vars.yml')['non_spectre_patched']
# create play with tasks
play_source = dict(
@@ -87,12 +82,11 @@ play_source = dict(
)
play = Play().load(play_source, variable_manager=variable_manager, loader=loader)
-
cb=OutputCallback()
tqm = None
try:
tqm = TaskQueueManager(
- inventory=inventory,
+ inventory=inv,
variable_manager=variable_manager,
loader=loader,
options=options,
@@ -103,9 +97,12 @@ try:
finally:
if tqm is not None:
tqm.cleanup()
+
for vhostname in sorted(cb.mem_per_host):
freemem = cb.mem_per_host[vhostname] - cb.mem_used_in_vm[vhostname]
freecpu = cb.cpu_per_host[vhostname] - cb.cpu_used_in_vm[vhostname]
- print '%s:\t%s/%s mem(unused/total)\t%s/%s cpus(unused/total)' % (
- vhostname, freemem, cb.mem_per_host[vhostname], freecpu,
cb.cpu_per_host[vhostname])
-
+ insecure = ''
+ if vhostname in unpatched_spectre:
+ insecure = ' (NOT PATCHED FOR SPECTRE)'
+ print '%s:\t%s/%s mem(unused/total)\t%s/%s cpus(unused/total) %s' % (
+ vhostname, freemem, cb.mem_per_host[vhostname], freecpu,
cb.cpu_per_host[vhostname], insecure)
diff --git a/scripts/which_playbook b/scripts/which_playbook
index 4665837..6091e0b 100755
--- a/scripts/which_playbook
+++ b/scripts/which_playbook
@@ -26,7 +26,9 @@
import os
import sys
-import ansible.inventory
+from ansible.parsing.dataloader import DataLoader
+from ansible.inventory.manager import InventoryManager
+from ansible import constants as C
from optparse import OptionParser
host_path = '/srv/web/infra/ansible/playbooks/hosts'
@@ -57,15 +59,12 @@ def main():
parser = OptionParser(version = "1.0")
- parser.add_option('-i', dest='inventory', default=None,
+ parser.add_option('-i', dest='inventory',
default=C.DEFAULT_HOST_LIST,
help="Path to inventory file/dir")
opts,hosts = parser.parse_args(sys.argv[1:])
- if opts.inventory:
- inv = ansible.inventory.Inventory(host_list=opts.inventory)
- else:
- inv = ansible.inventory.Inventory()
-
+ loader = DataLoader()
+ inv = InventoryManager(loader=loader, sources=opts.inventory)
for host in hosts:
matched_host = None
@@ -75,7 +74,7 @@ def main():
break
if host == os.path.basename(h_pb).replace(pb_extension, ''):
matched_host = h_pb
- for group in inv.groups_for_host(host):
+ for group in inv.hosts[host].groups:
if matched_group:
break
for g_pb in group_playbooks: