@@ -54,9 +54,12 @@ def options(func):
5454 option = click .option ('---debug' , type = str , help = 'Start task run with ansible in debug mode' ,
5555 default = False , required = False )
5656 func = option (func )
57- option = click .option ('---inventory' , help = 'Override embedded inventory specification ' ,
57+ option = click .option ('---inventory' , help = 'Specify inventory path if not using embedded inventory' ,
5858 required = False )
5959 func = option (func )
60+ option = click .option ('---inventory-tmp-dir' , help = 'Override path where we create embedded temporary inventory' ,
61+ required = False )
62+ func = option (func )
6063 return func
6164
6265 def invoke_bastion_mode (self , bastion_settings , invocation , remote_command , kwargs ):
@@ -148,8 +151,12 @@ def invoke_bastion_mode(self, bastion_settings, invocation, remote_command, kwar
148151 sys .exit (1 )
149152 logger .info ('Checking for locally changed files ...' )
150153 if loc_is_git :
151- cmd = 'git diff-index --name-only HEAD -- && git ls-files --others --exclude-standard'
152- local_changed = os .popen (cmd ).readlines ()
154+ # List modified and untracked files
155+ changed_cmd = '''git diff-index HEAD --name-status'''
156+ changed_files = [f .strip ().split ('\t ' )[1 ] for f in os .popen (changed_cmd ).readlines () if not f .startswith ('D\t ' )]
157+ untracked_cmd = '''git ls-files --others --exclude-standard'''
158+ untracked_files = [f .strip () for f in os .popen (untracked_cmd ).readlines ()]
159+ local_changed = changed_files + untracked_files
153160 else :
154161 # If local path is not a git repo then
155162 # we'll only sync files in the current working directory
@@ -160,6 +167,7 @@ def invoke_bastion_mode(self, bastion_settings, invocation, remote_command, kwar
160167 logger .info ('Checking for remotely changed files ...' )
161168 no_clobber = settings .get ('at_no_clobber' )
162169 if rem_is_git :
170+ remote_changed_cmd = '''{} | awk '$1 != "D" {{print $2}}' && {}''' .format (changed_cmd , untracked_cmd )
163171 remote_changed = remote_sub_process .call (remote_dir , cmd )
164172 if remote_changed :
165173 if no_clobber :
@@ -182,12 +190,6 @@ def invoke_bastion_mode(self, bastion_settings, invocation, remote_command, kwar
182190 remote_path = os .path .normpath (_remote_path ).replace ('\\ ' ,'/' )
183191 logger .debug ('Syncing {} to remote {}' .format (file_path , remote_path ))
184192 sftp_sync .to_remote (file_path , remote_path )
185- # remote_command = ' '.join([a for a in sys.argv if a != '---bastion-mode'][1:])
186- # tasks_file_override = invocation.get('tasks_file_override')
187- # if tasks_file_override:
188- # remote_command = 'tasks -f {} {}'.format(tasks_file_override, remote_command)
189- # else:
190- # remote_command = 'tasks {}'.format(remote_command)
191193 remote_command_result = remote_sub_process .call (remote_dir , remote_command , stdout_listen = True )
192194 if remote_command_result .returncode > 0 :
193195 logger .error ('Remote command failed with: %s' % ' ' .join (remote_command_result .stderr ))
@@ -217,7 +219,10 @@ def invocation(self,
217219 'ansible_playbook_command' , 'ansible-playbook' )
218220 # Embedded inventory logic
219221 embedded_inventory = False
222+ # Employ an exit trap if we're using bastion mode
220223 trap = ''
224+ # Where to create the temporary inventory (if applicable)
225+ inventory_dir = kwargs .get ('_inventory_tmp_dir' ) or yaml_vars .get ('inventory_dir' )
221226 inventory_input = kwargs .get ('_inventory' )
222227 embedded_inventory_string = yaml_vars .get ('inventory' )
223228 if not inventory_input and not embedded_inventory_string :
@@ -233,12 +238,13 @@ def invocation(self,
233238 trap = 'trap "rm -f %s" EXIT' % ans_inv_fp
234239 else :
235240 if bastion_settings .get ('enabled' ):
236- ans_inv_fp = '/tmp/ansible.inventory.%s.tmp.ini' % str (uuid .uuid4 ())
241+ inventory_dir = '/tmp' if not inventory_dir else inventory_dir
242+ ans_inv_fp = '{}/ansible.inventory.{}.tmp.ini' .format (inventory_dir , uuid .uuid4 ())
237243 ans_inv_fso_desc = None
238244 if not debug :
239245 trap = 'trap "rm -f %s" EXIT' % ans_inv_fp
240246 else :
241- ans_inv_fso_desc , ans_inv_fp = mkstemp (prefix = 'ansible-inventory' , suffix = '.tmp.ini' )
247+ ans_inv_fso_desc , ans_inv_fp = mkstemp (prefix = 'ansible-inventory' , suffix = '.tmp.ini' , dir = inventory_dir )
242248 logger .info ("No inventory specified" )
243249 logger .info ("Created temporary inventory file %s (normally deleted after run)" % ans_inv_fp )
244250 inventory_input = embedded_inventory_string
0 commit comments