Skip to main content

Command Palette

Search for a command to run...

Getting Repo from Github using ansible,Ansible-Vault and installing tomcat server using Playbook

Updated
2 min read

Getting code from GitHub Public-Repo

To get code from the repo we need to use the git module in our playbook

---
- hosts: all
  connection: ssh

  tasks:
    - name: getting code from github
      git:
        repo: https://github.com/vishalsiripuram/newSpringWar.git
        dest: "/var/my-pub-repo/"

by running the above playbook we'll get the below error


TASK [getting code from github] ********************************************************************************************************************************************
fatal: [developer]: FAILED! => {"changed": false, "msg": "Failed to find required executable git in paths: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin"}

because here we've not installed git in our remote server

So we need to install git first then get the code from the repo

Syntax:

git:

repo: "repo URL"

dest: "destination path"

---
- hosts: all
  connection: ssh

  tasks:
    - name: installing git
      yum:
        name: git
        state: present
    - name: getting code from github
      git:
        repo: https://github.com/vishalsiripuram/newSpringWar.git
        dest: "/var/my-pub-repo/"

by running the above playbook our code will be stored at the destination location.

By default code from the main branch will get stored

To get code from other branches we've to mention branches under version.

- name: getting code from github
      git:
        repo: https://github.com/vishalsiripuram/newSpringWar.git
        dest: "/var/my-pub-repo/"
        version: branch-name

Getting code from GitHub Private-Repo

To get the code from Private-Repo we've to add token in the URL.

Syntax:

https://<<github-token>>@github.com/username/repo-name.git

- name: getting code from github
      git:
        repo: https://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@github.com/vishalsiripuram/zoo-design.git
        dest: "/var/my-pub-repo/"
        version: master

Installing tomcat webserver using a playbook

Note: we can't edit any file in a remote server using a playbook.

So here we are getting files that we need to edit in remote into master then edit and copy it to the remote path.

---
- hosts: dev
  connection: ssh

  tasks:
    - name: get tomcat tar file from url
      get_url:
        url: https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.73/bin/apache-tomcat-9.0.73.tar.gz
        dest: /root

    - name: installing java
      yum:
        name: java-1.8.0-openjdk
        state: present

    - name: untar tomcat jar file
      command: tar xvf /root/apache-tomcat-9.0.73.tar.gz

    - name: copying context.xml
      copy:
        src: /root/context.xml
        dest: /root/apache-tomcat-9.0.73/webapps/manager/META-INF

    - name: copiyng roles file
      copy:
        src: /root/tomcat-users.xml
        dest: /root/apache-tomcat-9.0.73/conf

    - name: starting tomcat
      shell: nohup /root/apache-tomcat-9.0.73/bin/startup.sh

To execute a shell script we need to shell module and nohup command/

  shell: nohup /root/apache-tomcat-9.0.73/bin/startup.sh

Ansible Vault

To secure sensitive content of our project like passwords, tokens etc... we use Ansible vault.

To encrypt a file

ansible-vault encrypt <<file-name>>

To decrypt a file

ansible-vault decrypt <<file-name>>

To change the password to a file

ansible-vault rekey <<file-name>>

To encrypt a new file

ansible-vault create <<file-name>>

Note: If you forget ansible-vault password it's not recoverable.So make sure to store it somewhere.