Commit 9fb0c9d3 authored by Thomas Boni's avatar Thomas Boni
Browse files

Merge branch '474-new-job-ansible_playbook' into 'latest'

Resolve "[New job] - ansible_playbook"

Closes #474

See merge request r2devops/hub!336
parents b4bbf584 df7d52bc
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
# Changelog
All notable changes to this job will be documented in this file.

## [0.1.0] - 2022-03-23
* Initial version
+30 −0
Original line number Diff line number Diff line
## Objective

This job will run an [`ansible-playbook`](https://docs.ansible.com/ansible/latest/user_guide/playbooks.html) command to automate deployments. You can install multiple roles from a `requirements.yml` file, see the [documentation](https://docs.ansible.com/ansible/latest/galaxy/user_guide.html#installing-multiple-roles-from-a-file).

## How to use it

1. Add this job URL inside the `include` list of your `.gitlab-ci.yml` file (see the [quick setup](/use-the-hub/#quick-setup)). You can specify [a fixed version](#changelog) instead of `latest`.
    ```yaml
      - remote: 'https://jobs.r2devops.io/latest/ansible_playbook.yml'
    ```
1. If you need to customize the job (stage, variables, ...) 👉 check the [jobs
   customization](/use-the-hub/#jobs-customization)
1. Well done, your job is ready to work ! 😀

## Job details

* Job name: `ansible_playbook`
* Docker image:
[`alpine:3.15.1`](https://hub.docker.com/r/_/alpine)
* Default stage: `deploy`
* When: `always`

### Variables

| Name | Description | Default |
| ---- | ----------- | ------- |
| `ANSIBLE_WORKSPACE` <img width=100/> | The path where is located your ansible project <img width=175/>| `.` <img width=100/>|
| `ANSIBLE_INVENTORY_FILE` <img width=100/> | The inventory file where are described roles and groups. This variable should be specified in `Gitalb > CI/CD Settings` as file. <img width=175/>| `` <img width=100/>|
| `SSH_PRIVATE_KEY_FILE` <img width=100/> | ⚠️ Mandatory variable. The name of your private SSH key. his variable should be specified in `Gitalb > CI/CD Settings` as file. <img width=175/>| `` <img width=100/>|
| `ADDITIONAL_OPTIONS` <img width=100/> | Other [options](https://docs.ansible.com/ansible/latest/cli/ansible-playbook.html#common-options) you may want to use<img width=175/>| `` <img width=100/>|
+51 −0
Original line number Diff line number Diff line
stages:
  - deploy


ansible_playbook:
  image:
    name: alpine:3.15.1
    entrypoint: [""]
  stage: deploy
  variables:
    ANSIBLE_WORKSPACE : "."
    ANSIBLE_INVENTORY_FILE: ""
    SSH_PRIVATE_KEY_FILE: ""
    ADDITIONAL_OPTIONS: ''
  before_script:

    # Warn user if variable not set
    - if [ -z $SSH_PRIVATE_KEY_FILE ]; then
    -   echo "You should fulfill the variable SSH_PRIVATE_KEY_FILE with your key to use this job."
    -   exit 1
    - fi
    - apk add --no-cache ansible openssh

  script:

    # Installation and options setup
    - cd ${CI_PROJECT_DIR}/${ANSIBLE_PLAYBOOK}
    - cat $SSH_PRIVATE_KEY_FILE > id_rsa
    - chmod 600 id_rsa
    - cat $ANSIBLE_INVENTORY_FILE > inventory
    - options="-i inventory --private-key=id_rsa"

    - for extension in ".yml" ".yaml"; do
    -   file="requirements${extension}"
    # Check for write access and file not empty
    -   if [ -r ${file} ] && [ -s ${file} ]; then
          #could be used for installing some roles
    -     apk add --no-cache git
    -     ansible-galaxy install -r ${file}
    -     break
    -   fi
    - done

    - for extension in ".yml" ".yaml"; do
    -   file="main${extension}"
    -   if [ -r ${file} ] && [ -s ${file} ]; then
    -     ansible-playbook $options $ADDITIONAL_OPTIONS $file
          # Stop the jobs with success
    -     break
    -   fi
    - done
+9 −0
Original line number Diff line number Diff line
name: ansible_playbook
description: This job will run an `ansible-playbook` command to automate deployments.
default_stage: deploy
icon: 📦
maintainer: GridexX
license: MIT
images:
  alpine:3.15.1
tools:
 No newline at end of file
+1 −0
Original line number Diff line number Diff line
* Initial version
 No newline at end of file