33# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
44
55from __future__ import (absolute_import , division , print_function )
6-
76__metaclass__ = type
87
98import os
109import stat
1110
1211from ansible import context
1312from ansible .cli import CLI
14- from ansible .cli .arguments import optparse_helpers as opt_help
15- from ansible .errors import AnsibleError , AnsibleOptionsError
13+ from ansible .cli .arguments import option_helpers as opt_help
14+ from ansible .errors import AnsibleError
1615from ansible .executor .playbook_executor import PlaybookExecutor
1716from ansible .module_utils ._text import to_bytes
1817from ansible .playbook .block import Block
1918from ansible .utils .display import Display
20- from ansible .utils .collection_loader import set_collection_playbook_paths
19+ from ansible .utils .collection_loader import AnsibleCollectionLoader , get_collection_name_from_path , set_collection_playbook_paths
2120from ansible .plugins .loader import add_all_plugin_dirs
2221
22+
2323display = Display ()
2424
2525
@@ -46,25 +46,23 @@ def init_parser(self):
4646 opt_help .add_module_options (self .parser )
4747
4848 # ansible playbook specific opts
49- self .parser .add_option ('--list-tasks' , dest = 'listtasks' , action = 'store_true' ,
50- help = "list all tasks that would be executed" )
51- self .parser .add_option ('--list-tags' , dest = 'listtags' , action = 'store_true' ,
52- help = "list all available tags" )
53- self .parser .add_option ('--step' , dest = 'step' , action = 'store_true' ,
54- help = "one-step-at-a-time: confirm each task before running" )
55- self .parser .add_option ('--start-at-task' , dest = 'start_at_task' ,
56- help = "start the playbook at the task matching this name" )
57-
58- def post_process_args (self , options , args ):
59- options , args = super (PlaybookCLI , self ).post_process_args (options , args )
60-
61- if len (args ) == 0 :
62- raise AnsibleOptionsError ("You must specify a playbook file to run" )
49+ self .parser .add_argument ('--list-tasks' , dest = 'listtasks' , action = 'store_true' ,
50+ help = "list all tasks that would be executed" )
51+ self .parser .add_argument ('--list-tags' , dest = 'listtags' , action = 'store_true' ,
52+ help = "list all available tags" )
53+ self .parser .add_argument ('--step' , dest = 'step' , action = 'store_true' ,
54+ help = "one-step-at-a-time: confirm each task before running" )
55+ self .parser .add_argument ('--start-at-task' , dest = 'start_at_task' ,
56+ help = "start the playbook at the task matching this name" )
57+ self .parser .add_argument ('args' , help = 'Playbook(s)' , metavar = 'playbook' , nargs = '+' )
58+
59+ def post_process_args (self , options ):
60+ options = super (PlaybookCLI , self ).post_process_args (options )
6361
6462 display .verbosity = options .verbosity
65- self .validate_conflicts (options , runas_opts = True , vault_opts = True , fork_opts = True )
63+ self .validate_conflicts (options , runas_opts = True , fork_opts = True )
6664
67- return options , args
65+ return options
6866
6967 def run (self ):
7068
@@ -94,6 +92,12 @@ def run(self):
9492
9593 set_collection_playbook_paths (b_playbook_dirs )
9694
95+ playbook_collection = get_collection_name_from_path (b_playbook_dirs [0 ])
96+
97+ if playbook_collection :
98+ display .warning ("running playbook inside collection {0}" .format (playbook_collection ))
99+ AnsibleCollectionLoader ().set_default_collection (playbook_collection )
100+
97101 # don't deal with privilege escalation or passwords when we don't need to
98102 if not (context .CLIARGS ['listhosts' ] or context .CLIARGS ['listtasks' ] or
99103 context .CLIARGS ['listtags' ] or context .CLIARGS ['syntax' ]):
@@ -201,3 +205,9 @@ def _process_block(b):
201205 host_vars .get ('ansible_lo' ).get ('ipv4_secondaries' )[0 ]['address' ],
202206 'ansible_ssh_host' : host ,
203207 }
208+
209+ @staticmethod
210+ def _flush_cache (inventory , variable_manager ):
211+ for host in inventory .list_hosts ():
212+ hostname = host .get_name ()
213+ variable_manager .clear_facts (hostname )
0 commit comments