|
| 1 | +--- |
| 2 | +title: Roles |
| 3 | +description: No Fuss Computings Ansible playbook Roles |
| 4 | +date: 2024-04-24 |
| 5 | +template: project.html |
| 6 | +about: https://gitlab.com/nofusscomputing/projects/ansible/ansible_playbooks |
| 7 | +--- |
| 8 | + |
| 9 | +This playbook is designed to fetch a device's/virtual machine's rendered configuration from NetBox and run an ansible role that is mapped to the NetBox device/virtual machine role. |
| 10 | + |
| 11 | + |
| 12 | +## Playbook AWX / Ansible Automation Platform Template Import |
| 13 | + |
| 14 | +This playbook includes the [AWX feature](awx.md) where it imports the playbook as job templates in to AWX / Ansible Automation Platform. The following job templates that will be created: |
| 15 | + |
| 16 | +- **Playbook/Role/Ansible** Setup a hosts configured role |
| 17 | + |
| 18 | +- **Playbook/Role/Helm Chart** Deploy a Helm Chart |
| 19 | + |
| 20 | +- **Playbook/Role/Kubernetes Manifest** Deploy a Kubernetes Manifest |
| 21 | + |
| 22 | + |
| 23 | +## Requirements |
| 24 | + |
| 25 | +- The inventory must contain the following variables: `role` and `serial` which the value of `serial` must match those within NetBox. |
| 26 | + |
| 27 | + !!! tip |
| 28 | + The `netbox.netbox.nb_inventory` inventory plugin does contain the required variables. |
| 29 | + |
| 30 | +- NetBox has been setup with the required custom fields. |
| 31 | + |
| 32 | + !!! info |
| 33 | + You can use our Ansible Role `nofusscomputing.kubernetes.kubernetes_netbox` to setup NetBox with the required fields. See [Documentation for more info](../collection/kubernetes/roles/kubernetes_netbox/index.md). |
| 34 | + |
| 35 | +- Rendered configuration contains the required variables for the Ansible Role being used. |
| 36 | + |
| 37 | + !!! info |
| 38 | + We provide [publicly accessible templates](https://gitlab.com/nofusscomputing/infrastructure/configuration-management/netbox/-/tree/development/templates) for our roles, should you wish not to create your own. |
| 39 | + |
| 40 | + |
| 41 | +## Usage |
| 42 | + |
| 43 | +This playbook is broken up into different role types, they are: |
| 44 | + |
| 45 | +- Ansible Role |
| 46 | + |
| 47 | +- Helm Chart |
| 48 | + |
| 49 | +- Kubernetes Manifest |
| 50 | + |
| 51 | + |
| 52 | +### Ansible Role |
| 53 | + |
| 54 | +- job tag `ansible_role` |
| 55 | + |
| 56 | +This playbook requires the following variables be set. |
| 57 | + |
| 58 | +``` yaml |
| 59 | + |
| 60 | +role_map: # Mandatory, Dict. |
| 61 | + kubernetes_node: # Mandatory, String. Netbox device/Virtual Machine role slug |
| 62 | + name: nofusscomputing.kubernetes.nfc_kubernetes # Mandatory, String. Name of the Ansible Role that will be run |
| 63 | + tasks_from: main # Optional, String. Name of the task file within the role that will be used. |
| 64 | + |
| 65 | +``` |
| 66 | + |
| 67 | +Environmental variables `NETBOX_API` and `NETBOX_TOKEN`, must be set for the url (with protocol) and token to access NetBox. |
| 68 | + |
| 69 | +The remaining required variables that must be set are those that are required by the Ansible Role. These variables must be part of the device/virtual machine rendered configuration. |
| 70 | + |
| 71 | + |
| 72 | +### Helm Chart |
| 73 | + |
| 74 | +On the Ansible Controller, helm must be installed as must the PyYaml Python module. |
| 75 | + |
| 76 | +- job tag `helm_chart` |
| 77 | + |
| 78 | +This playbook requires the following variables be set. |
| 79 | + |
| 80 | +``` yaml |
| 81 | + |
| 82 | +role_map: # Mandatory, Dict. |
| 83 | + nginx_ingress: # Mandatory, String. Chart Name |
| 84 | + name: nginx # Mandatory, String. Helm deployment name |
| 85 | + repo: |
| 86 | + name: nginx # Mandatory, String. Name to give the repository |
| 87 | + url: https://kubernetes.github.io/ingress-nginx # Mandatory, String. Helm Chart repository URL |
| 88 | + chart: ingress-nginx # Mandatory, String. Name of the chart withing the helm repo. |
| 89 | + version: '4.8.2' # Mandatory, String. Chart version to deploy |
| 90 | + namespace: ingress # Optional, String. Kubernetes namespace to deploy chart to. |
| 91 | + create_namespace: true # Optional, String. Create Namespoace? |
| 92 | + release_values: # Optional, Dict. Chart Values. |
| 93 | + # Optional, String. Template filename for chart values |
| 94 | + template_file: "{{ inventory_dir + '/../../templates/helm-chart-values/nginx.yaml.j2'}}" |
| 95 | +``` |
| 96 | +
|
| 97 | +!!! tip |
| 98 | + AS a helm repository can contain multiple helm charts, keeping the `repo` dictionary the same across different helm role is recommended so that you don't end up with multiple helm repositories pointing to the same content. |
| 99 | + |
| 100 | +The following environmental variables must be set so that the ansible controller can connect to the kubernetes host: |
| 101 | + |
| 102 | +- `K8S_AUTH_HOST`, `K8S_AUTH_API_KEY`, `K8S_AUTH_SSL_CA_CERT` and optionally `K8S_AUTH_VERIFY_SSL` |
| 103 | + |
| 104 | +or |
| 105 | + |
| 106 | +- `K8S_AUTH_KUBECONFIG` |
| 107 | + |
| 108 | +The remaining required variables that must be set are those that are required by the template file if specified. These variables must be part of the device/virtual machine rendered configuration or included in the Ansible Inventory. |
| 109 | + |
| 110 | + |
| 111 | +### Kubernetes Manifest |
| 112 | + |
| 113 | +On the Ansible Controller, kubectl must be installed as must the PyYaml and jsonpatch Python module. |
| 114 | + |
| 115 | +- job tag `kubernetes_manifest` |
| 116 | + |
| 117 | +This playbook requires the following variables be set. |
| 118 | + |
| 119 | +``` yaml |
| 120 | +
|
| 121 | +role_map: # Mandatory, Dict. |
| 122 | + ingress_my_website: # Mandatory, String. Chart Name |
| 123 | + name: The ingress for my website # Mandatory, String. Arbitrary name. |
| 124 | + state: present # Optional, String. present or absent |
| 125 | + # Mandatory, String. Template filename containing the kubernetes manifest. |
| 126 | + template: "{{ inventory_dir + '/../../templates/kubernetes/my_website.yaml.j2'}}" |
| 127 | +``` |
| 128 | + |
| 129 | +The following environmental variables must be set so that the ansible controller can connect to the kubernetes host: |
| 130 | + |
| 131 | +- `K8S_AUTH_HOST`, `K8S_AUTH_API_KEY`, `K8S_AUTH_SSL_CA_CERT` and optionally `K8S_AUTH_VERIFY_SSL` |
| 132 | + |
| 133 | +or |
| 134 | + |
| 135 | +- `K8S_AUTH_KUBECONFIG` |
| 136 | + |
| 137 | +The remaining required variables that must be set are those that are required by the template file if specified. These variables must be part of the device/virtual machine rendered configuration or included in the Ansible Inventory. |
| 138 | + |
| 139 | + |
| 140 | +### Workflow |
| 141 | + |
| 142 | +This playbook has the following workflow: |
| 143 | + |
| 144 | +1. Confirm environmental variables `NETBOX_API` and `NETBOX_TOKEN` are set |
| 145 | + |
| 146 | +1. Fetching of the host (Device / Virtual Machine) ID from NetBox using `inventory_hostname` and `serial` as the filter |
| 147 | + |
| 148 | +1. Fetching of the hosts rendered config |
| 149 | + |
| 150 | +1. Saves the rendered config to a tmp file |
| 151 | + |
| 152 | +1. Load tmp file (load variables into `hostvars`) |
| 153 | + |
| 154 | +1. Removes tmp file |
| 155 | + |
| 156 | +1. Variable Validation |
| 157 | + |
| 158 | + - _Role `kubernetes_node`_ Confirms required variables are set |
| 159 | + |
| 160 | +1. Runs the role as specified in `role_map` |
| 161 | + |
| 162 | + |
| 163 | +## Playbook Definition |
| 164 | + |
| 165 | +``` yaml title="role.yaml" linenums="1" |
| 166 | +
|
| 167 | +--8<-- "role.yaml" |
| 168 | +
|
| 169 | +``` |
0 commit comments