2323# Pass in a message
2424- name: Get latest timestamps
2525 latest_timestamps:
26- current_timestamps_dict : "{{ appliances_repo_timestamps }}"
27- sources_dict : "{{ appliances_repo_timestamp_sources }} "
26+ repos_dict : "{{ appliances_repo_timestamp_sources }}"
27+ content_url : "https://ark.stackhpc.com/pulp/content "
2828 register: result
2929
3030'''
4444from ansible .module_utils .basic import AnsibleModule
4545import requests
4646from bs4 import BeautifulSoup
47+ from copy import deepcopy
4748
4849def run_module ():
4950 module_args = dict (
50- current_timestamps_dict = dict (type = 'dict' , required = True ),
51- sources_dict = dict (type = 'dict ' , required = True )
51+ repos_dict = dict (type = 'dict' , required = True ),
52+ content_url = dict (type = 'str ' , required = True )
5253 )
5354
5455 result = dict (
@@ -62,17 +63,23 @@ def run_module():
6263 supports_check_mode = True
6364 )
6465
65- latest_timestamps = {}
66+ original_timestamps = dict (module .params ['repos_dict' ])
67+ latest_timestamps = deepcopy (original_timestamps )
6668 changed_timestamps = []
67- for repo in module .params ['sources_dict' ]:
68- latest_timestamps [repo ] = module .params ['sources_dict' ][repo ]
69- for version in module .params ['sources_dict' ][repo ]:
70- html_txt = requests .get (url = module .params ['sources_dict' ][repo ][version ]).text
71- timestamp_link_list = BeautifulSoup (html_txt ,features = "html.parser" ).body .find ('pre' ).find_all ()
72- timestamp_link_list = map (lambda x : x .string ,timestamp_link_list ) # xml tags
73- latest_timestamps [repo ][version ] = list (timestamp_link_list )[- 1 ][:- 1 ] # last timestamp in list with trailing / removed
74- if module .params ['sources_dict' ][repo ][version ] != module .params ['current_timestamps_dict' ][repo ][version ]:
75- changed_timestamps .append (repo + ' ' + version )
69+
70+ for repo in original_timestamps :
71+ for version in original_timestamps [repo ]:
72+
73+ html_txt = requests .get (
74+ url = module .params ['content_url' ] + '/' + original_timestamps [repo ][version ]['path' ]
75+ ).text
76+ timestamp_link_list = BeautifulSoup (html_txt ,features = "html.parser" ).body .find ('pre' ).find_all () # getting raw list of timestamps from html
77+ timestamp_link_list = map (lambda x : x .string ,timestamp_link_list ) # stripping xml tags
78+ latest_timestamp = list (timestamp_link_list )[- 1 ][:- 1 ] # last timestamp in list with trailing / removed
79+
80+ latest_timestamps [repo ][version ]['timestamp' ] = latest_timestamp
81+ if original_timestamps [repo ][version ]['timestamp' ] != latest_timestamp :
82+ changed_timestamps .append (repo + ' ' + version + ': ' + original_timestamps [repo ][version ]['timestamp' ]+ ' -> ' + latest_timestamp )
7683
7784 result ['latest_dict' ] = latest_timestamps
7885 result ['changed_timestamps' ] = changed_timestamps
0 commit comments