diff --git a/awx/main/models/jobs.py b/awx/main/models/jobs.py index 6ac43cc98a..a071cb48c3 100644 --- a/awx/main/models/jobs.py +++ b/awx/main/models/jobs.py @@ -769,11 +769,18 @@ class Job(UnifiedJob, JobOptions, SurveyJobMixin, JobNotificationMixin): modified = parser.parse(modified, tzinfos=[tzutc()]) if not host.ansible_facts_modified or modified > host.ansible_facts_modified: ansible_facts = cache.get(host_key) + try: + ansible_facts = json.loads(ansible_facts) + except Exception: + ansible_facts = None + if ansible_facts is None: cache.delete(host_key) continue host.ansible_facts = ansible_facts host.ansible_facts_modified = modified + if 'insights' in ansible_facts and 'system_id' in ansible_facts['insights']: + host.insights_system_id = ansible_facts['insights']['system_id'] host.save() system_tracking_logger.info('New fact for inventory {} host {}'.format(host.inventory.name, host.name), extra=dict(inventory_id=host.inventory.id, host_name=host.name, diff --git a/awx/main/tests/unit/models/test_jobs.py b/awx/main/tests/unit/models/test_jobs.py index 5592407013..9b2e3a60d3 100644 --- a/awx/main/tests/unit/models/test_jobs.py +++ b/awx/main/tests/unit/models/test_jobs.py @@ -119,13 +119,15 @@ def test_finish_job_fact_cache(job, hosts, inventory, mocker, new_time): host_key = job.memcached_fact_host_key(hosts[1].name) modified_key = job.memcached_fact_modified_key(hosts[1].name) - job._get_memcache_connection().set(host_key, 'blah') + ansible_facts_new = {"foo": "bar", "insights": {"system_id": "updated_by_scan"}} + job._get_memcache_connection().set(host_key, json.dumps(ansible_facts_new)) job._get_memcache_connection().set(modified_key, new_time.isoformat()) job.finish_job_fact_cache() hosts[0].save.assert_not_called() hosts[2].save.assert_not_called() - assert hosts[1].ansible_facts == 'blah' + assert hosts[1].ansible_facts == ansible_facts_new + assert hosts[1].insights_system_id == "updated_by_scan" hosts[1].save.assert_called_once_with()