Skip to content

Git Configuration


Note

Docs Still under development

This playbook is intended to run our ansible role git_configuration.

AWX / Ansible Automation Platform

This playbook is setup to tak advantage of configuring AWX via the awx playbook / Job Template. The following job templates will be created:

  • Configure Gitlab / Github Repositories

    Configure Gitlab / Github repositories from code

  • Webhook/gitlab/Create Artifact

    After receiving a Gitlab webhook, Create an Artifact with the webhook data.

Docs ToDo

  • specify cli with tags and variables

  • "See it in action at" list

  • link to git_configuration docs including specifically the variables/tags for usage.

Playbook Definition

git_configuration.yaml
---
- name: Git Configuration as Code
  hosts: |-
    {%- if lookup('env', 'CI_PIPELINE_SOURCE') -%}
      localhost
    {%- else -%}
      {%- if nfc_pb_host is defined -%}
        {{ nfc_pb_host }}
      {%- else -%}
        all
      {%- endif -%}
    {%- endif %}
  gather_facts: false


  pre_tasks:


      # Play and task set time
    - name: Set Start Time
      ansible.builtin.set_fact:
        nfc_task_starttime: "{{ ('%Y-%m-%dT%H:%M:%S %z' | strftime) | string }}"
      no_log: "{{ nfc_pb_no_log_setup_facts | default(false) | bool }}"
      tags:
        - always


    # Setup dictionary 'nfc_automation'
    - name: Set Automation Facts
      ansible.builtin.set_fact:
        nfc_automation: {
          "time": {
            "start": "{{ nfc_task_starttime | string }}",
            "end": 0
          }
        }
      no_log: "{{ nfc_pb_no_log_setup_facts | default(false) | bool }}"
      when: nfc_automation is not defined
      tags:
        - always


    # Setup dictionary 'nfc_task'
    - name: Set Automation Facts
      ansible.builtin.set_fact:
        nfc_task: {
          "name": "{{ ansible_play_name | default('') | lower | string }}",
          "error": 0,
          "roles": "{{ ansible_play_role_names | default('') | string }}",
          "skip_tags": "{{ ansible_skip_tags | default([])| list }}",
          "start": "{{ nfc_task_starttime | string }}",
          "tags": "{{ ansible_run_tags | default([]) | list }}"
        }
      no_log: "{{ nfc_pb_no_log_setup_facts | default(false) | bool }}"
      tags:
        - always


    - name: Block - pre_tasks
      block:


        - name: Variable Setup - project_dir
          ansible.builtin.include_tasks:
            file: tasks/var_project_dir.yaml
            apply:
              tags:
                - always
          when: var_project_dir is not defined
          tags:
            - always


      rescue:

          # there was an error, set error object
        - name: Set error fact
          ansible.builtin.set_fact:
            nfc_task: "{{ nfc_task | combine({
                'error': 1
              }) }}"
          no_log: "{{ nfc_pb_no_log_setup_facts | default(false) | bool }}"
          tags:
            - always


      always:


          # Check if error occured and fail task
        - name: Error Check
          ansible.builtin.assert:
            that:
              - nfc_task.error | int == 0
            msg: Error occured, Fail the play run
          tags:
            - always


    # Don't use the 'roles' section.
  roles: []
    # if the included role(s) do not contain a rescue block, the playbook may stop
    # executing in this section (roles) with the post_tasks not running. This will
    # cause the artifacts to be incomplete. It's recommended to include your roles
    # in section(s) 'pre_tasks', 'tasks' or 'post_tasks' and from within a block with
   # rescue so that the playbook can complete and ensure that all artifacts are set.


  tasks:


    - name: Block - tasks
      block:

          # Check for error and fail play on error
        - name: Error Check
          ansible.builtin.assert:
            that:
              - nfc_task.error | int == 0
            msg: Error eccured, follow error path to fail play
          tags:
            - always


        - name: Configure git from Code
          ansible.builtin.include_role:
            name: git_configuration
          tags:
            - always


      rescue:


          # there was an error, set error object
        - name: Set error fact
          ansible.builtin.set_fact:
            nfc_task: "{{ nfc_task | combine({
                'error': 1
              }) }}"
          no_log: "{{ nfc_pb_no_log_setup_facts | default(false) | bool }}"
          tags:
            - always


      always:


          # Check if error occured and fail task
        - name: Error Check
          ansible.builtin.assert:
            that:
              - nfc_task.error | int == 0
            msg: Error occured, Fail the play run
          tags:
            - always


  post_tasks:

    - name: Tasks post_task
      block:


          # Check for error and fail play on error
        - name: Error Check
          ansible.builtin.assert:
            that:
              - nfc_task.error | int == 0
            msg: Error occured, follow error path to fail play
          tags:
            - always


        ########################################################################
        #
        # Your tasks here
        #
        ########################################################################


      rescue:


          # there was an error, set error object
        - name: Set error fact
          ansible.builtin.set_fact:
            nfc_task: "{{ nfc_task | combine({
                'error': 1
              }) }}"
          no_log: "{{ nfc_pb_no_log_setup_facts | default(false) | bool }}"
          tags:
            - always


      always:


          # Task and automation end time.
        - name: Fetch End time
          ansible.builtin.set_fact:
            nfc_task_endtime: "{{ '%Y-%m-%dT%H:%M:%S %z' | strftime }}"
          no_log: "{{ nfc_pb_no_log_setup_facts | default(false) | bool }}"
          tags:
            - always


          # Set task end time
        - name: Set task Facts
          ansible.builtin.set_fact:
            nfc_tasks: "{{ nfc_tasks | default([]) + [ nfc_task | combine({
                'end': nfc_task_endtime | string
              }) ] }}"
          no_log: "{{ nfc_pb_no_log_setup_facts | default(false) | bool }}"
          tags:
            - always


          # Set Automation end time.
          # every playbook must set this variable so that the end time
          # is equal to the fail time or the end of a group of playbooks.
        - name: Set automation end time
          ansible.builtin.set_fact:
            nfc_automation: "{{ nfc_automation | combine({
                'time': nfc_automation.time | combine({
                  'end': nfc_task_endtime | string
                })
              }) }}"
            nfc_task_endtime: null
          no_log: "{{ nfc_pb_no_log_setup_facts | default(false) | bool }}"
          tags:
            - always


          # Set the artifacts as a fact for subsequent playbook useage
          # Note: variable 'per_host' must be 'false' so that the artifacts
          # work within AWX / Ansible Automation Platform.
        - name: Create Automation Artifact
          ansible.builtin.set_stats:
            data:
              nfc_automation: "{{ nfc_automation }}"
              nfc_tasks: "{{ nfc_tasks }}"
            per_host: false
          tags:
            - always


          # Final error check to fail the entire play run on error 
        - name: Error Check
          ansible.builtin.assert:
            that:
              - nfc_task.error | int == 0
            msg: Error occured, Fail the play run
          tags:
            - always


  vars:
    ansible_connection: local
    nfc_pb_awx_tower_template:
      - name: "Configure Gitlab / Github Repositories"
        ask_credential_on_launch: true
        ask_tags_on_launch: true
        ask_inventory_on_launch: true
        description: |
          Create/Configure Gitlab or Github repositories from an inventory.
          Available tags:
            'gitlab' configure a Gitlab repository,
            'github' configure a Github repository.
        execution_environment: "No Fuss Computing EE"
        job_type: "run"
        job_tags: gitlab, github
        labels:
          - config
          - git
          - gitlab
          - github
        # credentials:
        #   - "Local"
        use_fact_cache: true
        survey_enabled: false

      - name: "Webhook/gitlab/Create Artifact"
        ask_credential_on_launch: false
        ask_limit_on_launch: false
        ask_instance_groups_on_launch: false
        ask_inventory_on_launch: true
        ask_job_type_on_launch: false
        ask_tags_on_launch: false
        ask_variables_on_launch: false
        description: |
          This job template is intended to create an Artifact
          from an inbound gitlab webhook.
          Available tags:
            'gitlab_webhook' Create an Artifact with webhook data,
        execution_environment: "No Fuss Computing EE"
        job_type: "run"
        job_tags: gitlab_webhook
        labels:
          - automation
          - gitlab
          - webhook
        use_fact_cache: true
        survey_enabled: false

About:

This page forms part of our Project Ansible Playbooks.

Page Metadata
Version: ToDo: place files short git commit here
Date Created: 2023-05-27
Date Edited: 2023-12-11

Contribution:

Would You like to contribute to our Ansible Playbooks project? You can assist in the following ways:

 

ToDo: Add the page list of contributors