mirror of
https://github.com/trailofbits/algo.git
synced 2025-09-10 05:53:27 +02:00
Updated playbook.py from recent ansible version
This commit is contained in:
parent
80f04de3ec
commit
bfd0895159
1 changed files with 29 additions and 19 deletions
|
@ -3,7 +3,6 @@
|
||||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||||
|
|
||||||
from __future__ import (absolute_import, division, print_function)
|
from __future__ import (absolute_import, division, print_function)
|
||||||
|
|
||||||
__metaclass__ = type
|
__metaclass__ = type
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
@ -11,15 +10,16 @@ import stat
|
||||||
|
|
||||||
from ansible import context
|
from ansible import context
|
||||||
from ansible.cli import CLI
|
from ansible.cli import CLI
|
||||||
from ansible.cli.arguments import optparse_helpers as opt_help
|
from ansible.cli.arguments import option_helpers as opt_help
|
||||||
from ansible.errors import AnsibleError, AnsibleOptionsError
|
from ansible.errors import AnsibleError
|
||||||
from ansible.executor.playbook_executor import PlaybookExecutor
|
from ansible.executor.playbook_executor import PlaybookExecutor
|
||||||
from ansible.module_utils._text import to_bytes
|
from ansible.module_utils._text import to_bytes
|
||||||
from ansible.playbook.block import Block
|
from ansible.playbook.block import Block
|
||||||
from ansible.utils.display import Display
|
from ansible.utils.display import Display
|
||||||
from ansible.utils.collection_loader import set_collection_playbook_paths
|
from ansible.utils.collection_loader import AnsibleCollectionLoader, get_collection_name_from_path, set_collection_playbook_paths
|
||||||
from ansible.plugins.loader import add_all_plugin_dirs
|
from ansible.plugins.loader import add_all_plugin_dirs
|
||||||
|
|
||||||
|
|
||||||
display = Display()
|
display = Display()
|
||||||
|
|
||||||
|
|
||||||
|
@ -46,25 +46,23 @@ class PlaybookCLI(CLI):
|
||||||
opt_help.add_module_options(self.parser)
|
opt_help.add_module_options(self.parser)
|
||||||
|
|
||||||
# ansible playbook specific opts
|
# ansible playbook specific opts
|
||||||
self.parser.add_option('--list-tasks', dest='listtasks', action='store_true',
|
self.parser.add_argument('--list-tasks', dest='listtasks', action='store_true',
|
||||||
help="list all tasks that would be executed")
|
help="list all tasks that would be executed")
|
||||||
self.parser.add_option('--list-tags', dest='listtags', action='store_true',
|
self.parser.add_argument('--list-tags', dest='listtags', action='store_true',
|
||||||
help="list all available tags")
|
help="list all available tags")
|
||||||
self.parser.add_option('--step', dest='step', action='store_true',
|
self.parser.add_argument('--step', dest='step', action='store_true',
|
||||||
help="one-step-at-a-time: confirm each task before running")
|
help="one-step-at-a-time: confirm each task before running")
|
||||||
self.parser.add_option('--start-at-task', dest='start_at_task',
|
self.parser.add_argument('--start-at-task', dest='start_at_task',
|
||||||
help="start the playbook at the task matching this name")
|
help="start the playbook at the task matching this name")
|
||||||
|
self.parser.add_argument('args', help='Playbook(s)', metavar='playbook', nargs='+')
|
||||||
|
|
||||||
def post_process_args(self, options, args):
|
def post_process_args(self, options):
|
||||||
options, args = super(PlaybookCLI, self).post_process_args(options, args)
|
options = super(PlaybookCLI, self).post_process_args(options)
|
||||||
|
|
||||||
if len(args) == 0:
|
|
||||||
raise AnsibleOptionsError("You must specify a playbook file to run")
|
|
||||||
|
|
||||||
display.verbosity = options.verbosity
|
display.verbosity = options.verbosity
|
||||||
self.validate_conflicts(options, runas_opts=True, vault_opts=True, fork_opts=True)
|
self.validate_conflicts(options, runas_opts=True, fork_opts=True)
|
||||||
|
|
||||||
return options, args
|
return options
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
|
|
||||||
|
@ -94,6 +92,12 @@ class PlaybookCLI(CLI):
|
||||||
|
|
||||||
set_collection_playbook_paths(b_playbook_dirs)
|
set_collection_playbook_paths(b_playbook_dirs)
|
||||||
|
|
||||||
|
playbook_collection = get_collection_name_from_path(b_playbook_dirs[0])
|
||||||
|
|
||||||
|
if playbook_collection:
|
||||||
|
display.warning("running playbook inside collection {0}".format(playbook_collection))
|
||||||
|
AnsibleCollectionLoader().set_default_collection(playbook_collection)
|
||||||
|
|
||||||
# don't deal with privilege escalation or passwords when we don't need to
|
# don't deal with privilege escalation or passwords when we don't need to
|
||||||
if not (context.CLIARGS['listhosts'] or context.CLIARGS['listtasks'] or
|
if not (context.CLIARGS['listhosts'] or context.CLIARGS['listtasks'] or
|
||||||
context.CLIARGS['listtags'] or context.CLIARGS['syntax']):
|
context.CLIARGS['listtags'] or context.CLIARGS['syntax']):
|
||||||
|
@ -201,3 +205,9 @@ class PlaybookCLI(CLI):
|
||||||
host_vars.get('ansible_lo').get('ipv4_secondaries')[0]['address'],
|
host_vars.get('ansible_lo').get('ipv4_secondaries')[0]['address'],
|
||||||
'ansible_ssh_host': host,
|
'ansible_ssh_host': host,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _flush_cache(inventory, variable_manager):
|
||||||
|
for host in inventory.list_hosts():
|
||||||
|
hostname = host.get_name()
|
||||||
|
variable_manager.clear_facts(hostname)
|
||||||
|
|
Loading…
Add table
Reference in a new issue