Darcy Nues1943

  • Home
  • Sitemap
Home  ›  Steps to Upload Ansible Playbooks Bitbucket Git Command

Steps to Upload Ansible Playbooks Bitbucket Git Command

Written By Rinaldi Andshoughter ۱۴۰۱ فروردین ۲۲, دوشنبه Add Comment Edit
  • Introduction
  • Ansible Git Module
  • Our Requirement and Prerequisites
    • Step1: Configuring Git login
    • Step2:  Creating Ansible vault to Shop the Git username and token
    • Step3:  The Ansible Git Instance Playbook
    • Step4:  Launch the Playbook with Ansible Git
    • Step5: Validate the Deployment
  • Using Git token instead of username and Password
  • Conclusion

Introduction

Ansible works seamlessly with Git and you might have had a requirement to download your source code or software directly from the Github, Bitbucket, Gitlab etc.

In this post, we are going to explore various tits and bits of Ansible Git module and how to apply it in real-time with an example.

Ansible Git Example

Ansible Git Module

Ansible Git module can be used only to bank check out a Source lawmaking from the Git repository and deploy it on the server.

Consider that you take your Node JS web application hosted on the Git hub Repository and you lot would like to become information technology deployed during the server provisioning.

Ansible tin can practice everything here, correct from installing the necessary tools and software and installing the packet dependencies and starting the NodeJS server etc.

As a summary, the Ansible GIT module tin be used for the following purposes

  1. To Checkout or to download the code from the Git repository
  2. Create a Git archive from Git repo
  3. Fetch all pull requests
  4. Clone a Git repository

Once y'all have downloaded or checked out the code with the ansible git control, you lot can use the regular git commands for all other requirements like Button and pull

Our Requirement and Prerequisites

Our requirement is to download a NodeJS lawmaking from the Github private repository and install the necessary packages using npm install and get-go the NodeJS Evolution  server using thenode control

Since nosotros are going to download the code from the private repository. we need to find a fashion to login to the Github repository from the Git CLI.

Note*: Though, we are taking only GITHUB hither.  you lot can apply this commodity equally a reference for other SCM'south like Gitlab, Bitbucket, BeanStalkApp etc. As all these repositories are having the similar authentication features.

Step1: Configuring Git login

To login to GITHUB or to any SCM tools [ flake saucepan, gitlab] they usually provide different methods and some pop methods are given below.

  1. Using the SSH cardinal
  2. Using the username and countersign on the URL
    • https:// user:pass @github.com/path
  3. Using the username and security token on the URL
    • https:// user:token @github.com/path

While the SSH Key-based hallmark is the most secure and recommended one. Just to explore. I am going to use the third method here which is simple and easy.

I chose this method as I likewise want to use the Ansible vaultfeature to securely store and recollect the username and security token.

Here I presume that you are ready with your Username and Security token (or) username. ( if y'all want to follow along and utilize the Ansible vault)

Let'due south walk further.

Step2:  Creating Ansible vault to Store the Git username and token

Look at the post-obit Screen recording to know how to create and encrypt the secrets with Ansible vault


I presume that you take taken a await at the screen recording and noticed that we have created a new ansible-vault named secrets.yaml and saved the GitHub username and token in a key value format.

this can be used inside the ansible-playbook as a variables file and the username and token can be used every bit a variable using Jinja2 templates syntax{{ gituser}}:{{gitpass}}

The username and countersign used in this video would be deleted for security purposes but you lot can use this same methodology to shop your username and countersign earlier proceeding further

Step3:  The Ansible Git Example Playbook

Here is the playbook I have written to download the NODE JS lawmaking available in my individual repository to install the necessary tools start the Node server finally and launch the application.

Make certain you replace the repository URL in the post-obit playbook to your own private repository before trying it out.

                ---                                  - name:                Install and Launch the Unproblematic NodeJS Application                                  hosts:                nodeserver                                  vars_files:                                                      -                  secrets.yml                                  vars:                                  - destdir:                /apps/SampleNodeApp                                  tasks:                                  -                proper name : install Node and NPM                                  become:                yes                                  annals:                ymrepo                                  yum:                                  name:                nodejs                                  land:                latest                                  -                name : validate the nodejs installation                                  debug:                msg="Installation of node is Successfull"                                  when:                ymrepo is changed                                  - proper name:                Version of Node and NPM                                  shell:                "npm -v && node -5"                                  register:                versioninfo                                  - name:                Version Info                                  debug:                                  msg:                "Version info                  {{ versioninfo.stdout_lines }}"                                  when:                versioninfo is inverse                                  - name:                Download the NodeJS code from the GitRepo                                  become:                yes                                  git:                                  repo:                'https://{{gituser}}:{{gitpass}}@github.com/AKSarav/SampleNodeApp.git'                                  dest:                "{{ destdir }}"                                  - name:                Alter the ownership of the directory                                  get:                yes                                  file:                                  path:                "{{destdir}}"                                  possessor:                "vagrant"                                  register:                chgrpout                                  - proper noun:                Install Dependencies with NPM install command                                  trounce:                "npm install"                                  args:                                  chdir:                "{{ destdir }}"                                  register:                npminstlout                                  - proper name:                Debug npm install command                                  debug:                msg='{{npminstlout.stdout_lines}}'                                  - name:                Start the App                                  async:                10                                  poll:                0                                  crush:                "(node index.js > nodesrv.log ii>&ane &)"                                  args:                                  chdir:                "{{ destdir }}"                                  register:                appstart                                  - proper name:                Validating the port is open                                  tags:                nodevalidate                                  wait_for:                                  host:                "localhost"                                  port:                5000                                  delay:                10                                  timeout:                thirty                                  state:                started                                  msg:                "NodeJS server is not running"              

In the preceding playbook, you need to re-look a few things to understand it amend.

First is the URL, You can see nosotros have used the Jinja2 templates {{gituser}} and {{gitpass}} in the URL. During the runtime, these would be replaced with actual values.

                'https://{{gituser}}:{{gitpass}}@github.com/AKSarav/SampleNodeApp.git'              

and the second one you need to check is the variable file nosotros have mentioned at the top with vars_files directive.

                vars_files:-                secrets.yml

Y'all tin run across that we used secrets.yml file as our variable file.  Rest all are usual syntaxes only

At the last, we have also use wait_for module to brand sure that the NODEJS server is started and running past monitoring the port 5000

At present, y'all might think. the secrets.yml file is encrypted by ansible vault and how can we utilise information technology as a variable file in the playbook. We will discuss this next

Step4:  Launch the Playbook with Ansible Git

Now you can launch the playbook with ansible-playbook command merely in that location is a little risk that you have to add together one more argument/flag while starting the playbook.

ansible-playbook gitexample.yml                --enquire-vault-laissez passer              

As you have guessed,  the additional parameter we have added is to make ansible-playbook go the countersign of the ansible-vault from the user every bit it needs the password to decrypt the secrets.yml file used inside the playbook.

Refer to the following screen recording clip to encounter how the playbook is running on real-fourth dimension and to see the output

If you want more info on the Git hash information. you tin use -vv  while you lot are invoking the playbook

ansible-playbook gitexample.yml                --ask-vault-pass --vv              

It would testify the Git commit hash details along with before and later GIT information

Step5: Validate the Deployment

Promise your Ansible Playbook has run successfully and now information technology is the time for validation.  Yous might accept figured out that my remote server name is mwiapp01 as information technology is displayed on the preceding snapshot.

Now permit us admission the URL http://mwiapp01:5000 and test it

Ansible Git Example

Using Git token instead of username and Countersign

In the previous playbook, you tin encounter that nosotros have used the username and the password which is not a secure solution.

Github now supports user access tokens in place of username and password for authentication

Refer to this article to know how to create personal admission token in GitHub

In one case you accept the token with y'all tin simply supplant the GIT URL from

                'https://{{gituser}}:{{gitpass}}@github.com/AKSarav/SampleNodeApp.git'              

to this

                'https://{{gittoken}}@github.com/AKSarav/SampleNodeApp.git'              

Hither is the complete example playbook you tin refer to, in this upgraded playbook  we take used boosted modules and features like

  • Ansible Assert
  • pre_task
  • post_task
  • slack notification etc
---   - name: Install and Launch the Uncomplicated NodeJS Application     hosts: testserver     vars_files:        - gitsecrets.yml     vars:        - destdir: /apps/sampleapp     pre_tasks:       - name : install dependencies before starting         get: yes         register: aptinstall         apt:            proper noun:              - nodejs             - npm             - git            state: latest            update_cache: aye              - name : validate the nodejs installation         debug: msg="Installation of node is Successfull"         when: aptinstall is changed          tasks:        - name: Version of Node and NPM          shell:             "npm -5 && nodejs -v"          register: versioninfo        - proper noun: Validate if the installation is intact          affirm:           that: "versioninfo is changed"               - name: Create Dest Directory if not exists          become: yes          file:            path: "{{ destdir }}"           state: directory           possessor: vagrant           group: vagrant           style: 0755        - proper name: Download the NodeJS code from the GitRepo          become: yes          git:             repo: 'https://{{gittoken}}@github.com/AKSarav/SampleNodeApp.git'             dest: "{{ destdir }}"        - name: Modify the buying of the directory          become: yes          file:            path: "{{destdir}}"            owner: "vagrant"          register: chgrpout        - name: Install Dependencies with NPM install control          shell:             "npm install"          args:             chdir: "{{ destdir }}"          annals: npminstlout        - name: Debug npm install command          debug: msg='{{npminstlout.stdout_lines}}'        - name: Start the App          async: x          poll: 0          trounce:             "(node index.js > nodesrv.log two>&1 &)"          args:            chdir: "{{ destdir }}"          register: appstart        - proper name: Validating the port is open          tags: nodevalidate          wait_for:            host: "localhost"            port: 5000            delay: 10            timeout: xxx            land: started            msg: "NodeJS server is non running"     post_tasks:         - name: notify Slack that the servers have been updated            tags: slack           community.general.slack:             token: T026******PF/B02U*****N/WOa7r**********Ao0jnWn             msg: |                 ### StatusUpdate ###                 --------------------------------------                 ``                 `Server`: {{ansible_host}}                 `Status`: NodeJS Sample Application installed successfully                 --------------------------------------             channel: '#ansible'             color: proficient             username: 'Ansible on {{ inventory_hostname }}'             link_names: 0             parse: 'none'           delegate_to: localhost

If you are using this playbook, make sure you are updating your secrets file also, with new variable gittoken with your token value.

This playbook is covered in detail in the following commodity.

Ansible Pre Tasks and Mail service Tasks Example | Devops Junction

Conclusion

Promise you lot find this article helping. this article was primarily designed to give an overview of how the Ansible Git module can exist used to deploy the lawmaking from the remote.

At that place are other few things you tin do with the Ansible git module which I observe it non interesting. Though I like the Ansible Git module, Information technology would have been smashing if they would have given the other necessary features of Git such as Pull, Button, Fetch, Merge etc.

I hope it would be added in the future, I will update this article as and when I am finding more than options available with this module.

I will also write an commodity for Ansible and Git CLI in near time to come with all Git cadre comands. Till and so Cheers.

Rate this article [ratings]

Thanks
Sarav AK

Buy Me a Coffee at ko-fi.com

Follow us onFacebook orTwitter
For more applied videos and tutorials. Subscribe to our channel
Find me on Linkedin My Profile
For any Consultation or to hire us [email protected]
If you lot like this article. Show your Support! Buy me a Java.

Signup for Exclusive "Subscriber-merely" Content


darcynues1943.blogspot.com

Source: https://www.middlewareinventory.com/blog/ansible-git-example/

Share this post

0 Response to "Steps to Upload Ansible Playbooks Bitbucket Git Command"

ارسال یک نظر

پیام جدیدتر پیام قدیمی تر صفحهٔ اصلی
اشتراک در: نظرات پیام (Atom)

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel

Popular Post

  • what to write if you dont know mrs or miss
    Asked by: Mariuta Irumugarrieta asked in category:...
  • Can I Upload My Own Music to Amazon Alexa 2019 Mary Magdalene From Toronto
    Although there's something to be said for the convenience of streaming music...
  • Disney Princess Magical Secret Book Toys R Us
    VIDE...



banner



Copyright - Darcy Nues1943