Skip to content
Merged

Devel #1843

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions IM/CtxtAgentBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,13 +494,18 @@ def install_ansible_roles(self, general_conf_data, playbook):
with open(playbook) as f:
yaml_data = yaml.safe_load(f)

collections_needs_git = False
# First add collections
galaxy_collections = []
if 'ansible_collections' in general_conf_data and general_conf_data['ansible_collections']:
for galaxy_name in general_conf_data['ansible_collections']:
if galaxy_name:
self.logger.debug("Install %s collection with ansible-galaxy.", galaxy_name)

# Git is needed by ansible-galaxy for Git-sourced collections and roles.
if galaxy_name.startswith("git"):
collections_needs_git = True

parts = galaxy_name.split(",")
if len(parts) > 1:
name = parts[0]
Expand All @@ -512,6 +517,12 @@ def install_ansible_roles(self, general_conf_data, playbook):
galaxy_collections.append(dep)

if galaxy_collections:
if collections_needs_git:
task = {"package": "name=git state=present"}
task["name"] = "Install git"
task["become"] = "yes"
yaml_data[0]['tasks'].append(task)

now = str(int(time.time() * 100))
filename = "/tmp/galaxy_collections_%s.yml" % now # nosec
yaml_deps = yaml.safe_dump({"collections": galaxy_collections}, default_flow_style=True)
Expand Down Expand Up @@ -555,15 +566,16 @@ def install_ansible_roles(self, general_conf_data, playbook):
yaml_data[0]['tasks'].append(task)

# and then add roles
roles_needs_git = False
galaxy_dependencies = []
needs_git = False
if 'ansible_roles' in general_conf_data and general_conf_data['ansible_roles']:
for galaxy_name in general_conf_data['ansible_roles']:
if galaxy_name:
self.logger.debug("Install %s with ansible-galaxy.", galaxy_name)

if galaxy_name.startswith("git"):
needs_git = True
# Git is needed by ansible-galaxy for Git-sourced roles.
if not collections_needs_git and galaxy_name.startswith("git"):
roles_needs_git = True

parts = galaxy_name.split("|")
if len(parts) > 1:
Expand All @@ -582,13 +594,13 @@ def install_ansible_roles(self, general_conf_data, playbook):

galaxy_dependencies.append(dep)

if needs_git:
task = {"package": "name=git state=present"}
task["name"] = "Install git"
task["become"] = "yes"
yaml_data[0]['tasks'].append(task)

if galaxy_dependencies:
if roles_needs_git:
task = {"package": "name=git state=present"}
task["name"] = "Install git"
task["become"] = "yes"
yaml_data[0]['tasks'].append(task)

now = str(int(time.time() * 100))
filename = "/tmp/galaxy_roles_%s.yml" % now # nosec
yaml_deps = yaml.safe_dump(galaxy_dependencies, default_flow_style=True)
Expand Down
4 changes: 2 additions & 2 deletions test/files/test_ansible.radl
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ configure node (
@begin
---
- tasks:
- fail: msg="Incorrect Ansible Version. Expected 2.19.10 but found {{ ansible_version.string }}"
when: ansible_version.string != "2.19.10"
- fail: msg="Incorrect Ansible Version. Expected 2.19.15 but found {{ ansible_version.string }}"
when: ansible_version.string != "2.19.15"

@end
)
Expand Down
8 changes: 6 additions & 2 deletions test/unit/test_ctxt_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,12 @@ def test_100_install_git_ansible_collection(self):
with open(res, 'r') as f:
yaml_data = yaml.safe_load(f)

self.assertEqual(len(yaml_data[0]['tasks']), 2)
self.assertEqual(yaml_data[0]['tasks'][1]['register'], "collections_install")
self.assertEqual(yaml_data[0]['tasks'][0]['package'], "name=git state=present")
self.assertEqual(yaml_data[0]['tasks'][0]['name'], "Install git")
self.assertEqual(yaml_data[0]['tasks'][1]['name'],
"Create YAML file to install the collections with ansible-galaxy")
self.assertEqual(yaml_data[0]['tasks'][2]['register'], "collections_install")
self.assertEqual(len(yaml_data[0]['tasks']), 3)
os.unlink(res)


Expand Down
Loading