cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1760
Views
2
Helpful
5
Replies

Passing a list of changed hosts into gitlab pipeline for ansible

chade2540
Level 1
Level 1

I’m curious if anyone has created a better solution to something I have run up on. Despite days juggling different ideas and searching around I have not seen an example of ansible playbooks that does not explicitly match all inventory hosts in their ansible playbook to push configuration changes from a CI/CD pipeline. This may work for some but we have hundreds of devices. I feel like there is a better way to do this and as such my current solution is to type in the merge request title in Gitlab as a list of devices that are changing and this works great because I can then in my pipeline pass that var into the playbook call. However, due to the nature, if a user forgets that in the change control process, it breaks. Is anyone else doing something better or have any pointers? I’ve considered parsing the git log and finding configuration files that changed and looping through that but I also feel like there’s something easier I am just missing.

For extra context our current solution is:
Using GitLab create a merge request of changed configs, pass in the hosts changing in merge request title, in my pipeline call the push ansible playbook and pass in the merge request title as the hosts var.

1 Accepted Solution

Accepted Solutions

chade2540
Level 1
Level 1

Here is the code I wrote that solved this for me.
https://github.com/ChadPunk/Ansible-Gitlab-List-of-changed-devices-for-pipeline
If you need any help don't hesitate to reach out.

View solution in original post

5 Replies 5

Thank you for your reply. I’m not looking to limit ansible’s concurrent
processes. I’m rather looking to limit the hosts: var all together in a
playbook run for a limited amount of hosts based on which hosts
configuration changed in a certain directory and pass that in dynamically
via a gitlab CI/CD pipeline.

In the past my devices were set by type (vendor, verion etc), location, edge, core etc.. all within the names. This made it easy to select devices when making changes. I think in your case youcan pass a list of changed hosts into a GitLab pipeline by using the pipeline variables feature. You can set the pipeline variables to a list of the hosts you want to target with your Ansible playbook. Then, in the pipeline configuration, you can reference the pipeline variables to pass the list into your Ansible playbook.

Please mark this as helpful or solution accepted to help others
Connect with me https://bigevilbeard.github.io

This is actually what I am already doing but in a unique way (which I am trying to get away from). Currently I use the merge title to compile a list of devices and then in the pipeline using the GitLab 'CI_COMMIT_TITLE' and pass that into the playbook. So if a user changes a bunch of devices they would in the title of the merge request specify the devices changing ie. "core1,core2,site-a,site-b". GitLab would then use that message and pass it into the playbook as a var. This does however create some challenges. If the user mistypes or forgets all together, it will fail. I have since posting the original question discovered a better way on my own that I will share with the community when I have the time to do a write up, that dynamically does this behind the scenes and works great.

chade2540
Level 1
Level 1

Here is the code I wrote that solved this for me.
https://github.com/ChadPunk/Ansible-Gitlab-List-of-changed-devices-for-pipeline
If you need any help don't hesitate to reach out.

I appreciate you taking the time to write that out as it may help others but I was already doing this but more of a manual way to create the list of changed devices (see one of my previous comments). I created a dynamic way to do this using the GitLab API to do this automatically and without user intervention. I accepted that as the solution.