3
3
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
4
4
5
5
from __future__ import (absolute_import , division , print_function )
6
-
7
6
__metaclass__ = type
8
7
9
8
import os
10
9
import stat
11
10
12
11
from ansible import context
13
12
from 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
16
15
from ansible .executor .playbook_executor import PlaybookExecutor
17
16
from ansible .module_utils ._text import to_bytes
18
17
from ansible .playbook .block import Block
19
18
from 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
21
20
from ansible .plugins .loader import add_all_plugin_dirs
22
21
22
+
23
23
display = Display ()
24
24
25
25
@@ -46,25 +46,23 @@ def init_parser(self):
46
46
opt_help .add_module_options (self .parser )
47
47
48
48
# 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 )
63
61
64
62
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 )
66
64
67
- return options , args
65
+ return options
68
66
69
67
def run (self ):
70
68
@@ -94,6 +92,12 @@ def run(self):
94
92
95
93
set_collection_playbook_paths (b_playbook_dirs )
96
94
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
+
97
101
# don't deal with privilege escalation or passwords when we don't need to
98
102
if not (context .CLIARGS ['listhosts' ] or context .CLIARGS ['listtasks' ] or
99
103
context .CLIARGS ['listtags' ] or context .CLIARGS ['syntax' ]):
@@ -201,3 +205,9 @@ def _process_block(b):
201
205
host_vars .get ('ansible_lo' ).get ('ipv4_secondaries' )[0 ]['address' ],
202
206
'ansible_ssh_host' : host ,
203
207
}
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