ansible git autodeploy by webhooks

How to automate deployment from git repository hosted on github / bitbucket to ubuntu server using ansible?

In github repostory settings->webhooks i can add webhook for pull to master, that will call my server
webhooks

I have self-made solution, written in node.js, that pulls from git and restarts service, but want to migrate to ansible

I’m new to ansible, but know little about ansible-playbook

There is github webhook module for ansible https://docs.ansible.com/ansible/latest/github_hooks_module.html

But i dont understand, what .yml file i need to write

Also i need to pass secret API keys using enviroment variables. How to store it in keyring?

How to help

Write .yml file for ansible-playbook, that service web server for hook with secret field and git pull plus run git hooks like npm i, that specified in repo(from unprivileged user) and say, how to use it

Say, can i use github webhook module with bitbucket?

Answer

The github_hooks module is deprecated and used from Ansible to create Hooks on Github. What you want is a hook-call that is thrown, when f.e. a Git push was done (so the other way around).

Github needs a HTTP service to call, when you want that Ansible starts doing something. Ansible itself cannot do it, because it isn’t a server and it doesn’t listen to anything. Its just a “script”.

You have two options:
Put all you Ansible scripts etc. into AWX/Ansible Tower. That is a Webservice, that has a REST-API. Please have a look into https://keithtenzer.com/2019/06/24/ci-cd-with-ansible-tower-and-github/ for a good description.

If you don’t want to install AWX. Install somekind of HTTP server on the ansible control machine and write a script (like the one you already did) that starts the Ansible-Playbook, when Github is calling it.

Attribution
Source : Link , Question Author : Евгений Новиков , Answer Author : TRW

Leave a Comment