Docs: IaC edits (#114086)
* Removed Grizzly from table * Fixes * Review * Reviews * Edits * More edits * Prettier * Remove agent * Review, removing Agent docs * Prettier * Fixes * Prettier * Edit * Update docs/sources/as-code/infrastructure-as-code/ansible/ansible-cloud-stack/index.md Co-authored-by: Clayton Cornell <131809008+clayton-cornell@users.noreply.github.com> * Update docs/sources/as-code/infrastructure-as-code/ansible/ansible-cloud-stack/index.md Co-authored-by: Clayton Cornell <131809008+clayton-cornell@users.noreply.github.com> * Update docs/sources/as-code/infrastructure-as-code/ansible/ansible-cloud-stack/index.md Co-authored-by: Clayton Cornell <131809008+clayton-cornell@users.noreply.github.com> * Update docs/sources/as-code/infrastructure-as-code/ansible/ansible-cloud-stack/index.md Co-authored-by: Clayton Cornell <131809008+clayton-cornell@users.noreply.github.com> * Update docs/sources/as-code/infrastructure-as-code/ansible/ansible-cloud-stack/index.md Co-authored-by: Clayton Cornell <131809008+clayton-cornell@users.noreply.github.com> * Update docs/sources/as-code/infrastructure-as-code/ansible/ansible-cloud-stack/index.md Co-authored-by: Clayton Cornell <131809008+clayton-cornell@users.noreply.github.com> * Update docs/sources/as-code/infrastructure-as-code/ansible/ansible-cloud-stack/index.md Co-authored-by: Clayton Cornell <131809008+clayton-cornell@users.noreply.github.com> * Update docs/sources/as-code/infrastructure-as-code/ansible/ansible-cloud-stack/index.md Co-authored-by: Clayton Cornell <131809008+clayton-cornell@users.noreply.github.com> * Update docs/sources/as-code/infrastructure-as-code/ansible/ansible-cloud-stack/index.md Co-authored-by: Clayton Cornell <131809008+clayton-cornell@users.noreply.github.com> * Update docs/sources/as-code/infrastructure-as-code/ansible/ansible-cloud-stack/index.md Co-authored-by: Clayton Cornell <131809008+clayton-cornell@users.noreply.github.com> * Update docs/sources/as-code/infrastructure-as-code/ansible/ansible-cloud-stack/index.md Co-authored-by: Clayton Cornell <131809008+clayton-cornell@users.noreply.github.com> * Update docs/sources/as-code/infrastructure-as-code/ansible/ansible-cloud-stack/index.md Co-authored-by: Clayton Cornell <131809008+clayton-cornell@users.noreply.github.com> * Update docs/sources/as-code/infrastructure-as-code/ansible/ansible-cloud-stack/index.md Co-authored-by: Clayton Cornell <131809008+clayton-cornell@users.noreply.github.com> * Update docs/sources/as-code/infrastructure-as-code/ansible/ansible-cloud-stack/index.md Co-authored-by: Clayton Cornell <131809008+clayton-cornell@users.noreply.github.com> * Update docs/sources/as-code/infrastructure-as-code/ansible/ansible-cloud-stack/index.md Co-authored-by: Clayton Cornell <131809008+clayton-cornell@users.noreply.github.com> * Update docs/sources/as-code/infrastructure-as-code/ansible/ansible-cloud-stack/index.md Co-authored-by: Clayton Cornell <131809008+clayton-cornell@users.noreply.github.com> * Feedback * Prettier * Variable style edits --------- Co-authored-by: Clayton Cornell <131809008+clayton-cornell@users.noreply.github.com>
This commit is contained in:
co-authored by
Clayton Cornell
parent
fbe29596f1
commit
bf509de89f
+185
-169
@@ -4,64 +4,78 @@ keywords:
|
||||
- Quickstart
|
||||
- Grafana Cloud
|
||||
- Ansible
|
||||
title: Create and manage a Grafana Cloud stack using Ansible
|
||||
title: Create and manage your Grafana Cloud stack using Ansible
|
||||
menuTitle: Manage stack using Ansible
|
||||
weight: 100
|
||||
canonical: https://grafana.com/docs/grafana/latest/as-code/infrastructure-as-code/ansible/ansible-cloud-stack/
|
||||
---
|
||||
|
||||
# Create and manage a Grafana Cloud stack using Ansible
|
||||
# Create and manage your Grafana Cloud stack using Ansible
|
||||
|
||||
Learn how to add a data source, a dashboard, and a folder to a Grafana Cloud stack using Ansible collection for Grafana.
|
||||
This guide shows you how to create a Grafana Cloud stack and add a data source, dashboard, and folder using the Ansible Collection for Grafana. You'll manage your Grafana infrastructure through Ansible playbooks.
|
||||
|
||||
## Before you begin
|
||||
|
||||
Before you begin, you should have the following available:
|
||||
Before you begin, make sure you have the following available:
|
||||
|
||||
- A Grafana Cloud account.
|
||||
- A Grafana Cloud account
|
||||
- [Ansible](https://docs.ansible.com/ansible/latest/installation_guide/index.html) installed on your machine
|
||||
|
||||
## Install the Grafana Ansible collection
|
||||
|
||||
Install the Grafana Ansible collection:
|
||||
|
||||
```sh
|
||||
ansible-galaxy collection install grafana.grafana
|
||||
```
|
||||
|
||||
This collection provides all the modules needed to manage Grafana Cloud stacks and resources.
|
||||
|
||||
## Create a Cloud stack
|
||||
|
||||
1. Create a Grafana Cloud Access Policy and get a token.
|
||||
You'll need this for the Ansible playbook to be able to create a Grafana Cloud stack.
|
||||
Refer to [Create a Grafana Cloud Access Policy](/docs/grafana-cloud/security-and-account-management/authentication-and-permissions/access-policies/create-access-policies/).
|
||||
First, create a Grafana Cloud Access Policy and get a token. You'll need this for the Ansible playbook to be able to create a Grafana Cloud stack. Refer to [Create a Grafana Cloud Access Policy](/docs/grafana-cloud/security-and-account-management/authentication-and-permissions/access-policies/create-access-policies/).
|
||||
|
||||
1. Create an Ansible playbook file.
|
||||
Next, create an Ansible playbook file. This Ansible playbook creates a Grafana Cloud stack using the [Cloud stack module](https://docs.ansible.com/ansible/latest/collections/grafana/grafana/cloud_stack_module.html#ansible-collections-grafana-grafana-cloud-stack-module).
|
||||
|
||||
This Ansible playbook will create a Grafana Cloud stack by using the [Cloud stack module](https://docs.ansible.com/ansible/latest/collections/grafana/grafana/cloud_stack_module.html#ansible-collections-grafana-grafana-cloud-stack-module).
|
||||
To do so, create a file named `cloud-stack.yml` and add the following:
|
||||
|
||||
Create a file named `cloud-stack.yml` and add the following:
|
||||
```yaml
|
||||
- name: Create Grafana Cloud stack
|
||||
connection: local
|
||||
hosts: localhost
|
||||
|
||||
```yaml
|
||||
- name: Create Grafana Cloud stack
|
||||
connection: local
|
||||
hosts: localhost
|
||||
vars:
|
||||
grafana_cloud_api_key: '<CLOUD_ACCESS_POLICY_TOKEN>'
|
||||
stack_name: '<STACK_NAME>'
|
||||
org_name: '<ORG_NAME>'
|
||||
|
||||
vars:
|
||||
grafana_cloud_api_key: '<Your Cloud Access Policy token>'
|
||||
stack_name: '<stack-name>'
|
||||
org_name: '<org-name>'
|
||||
tasks:
|
||||
- name: Create a Grafana Cloud stack
|
||||
grafana.grafana.cloud_stack:
|
||||
name: '{{ stack_name }}'
|
||||
stack_slug: '{{ stack_name }}'
|
||||
cloud_api_key: '{{ grafana_cloud_api_key }}'
|
||||
org_slug: '{{ org_name }}'
|
||||
delete_protection: true
|
||||
state: present
|
||||
register: stack_result
|
||||
|
||||
tasks:
|
||||
- name: Create a Grafana Cloud stack
|
||||
grafana.grafana.cloud_stack:
|
||||
name: '{{ stack_name }}'
|
||||
stack_slug: '{{ stack_name }}'
|
||||
cloud_api_key: '{{ grafana_cloud_api_key }}'
|
||||
org_slug: '{{ org_name }}'
|
||||
delete_protection: true
|
||||
state: present
|
||||
```
|
||||
- name: Display stack URL
|
||||
debug:
|
||||
msg: 'Stack created at: {{ stack_result.url }}'
|
||||
```
|
||||
|
||||
1. Replace the following field values:
|
||||
- `<token>` with a token from the Cloud Access Policy you created in the Grafana Cloud portal.
|
||||
- `<stack-name>` with the name of your stack.
|
||||
- `<org-name>` with the name of the organization in Grafana Cloud.
|
||||
Replace the placeholders with your values:
|
||||
|
||||
## Create an API key in the Grafana stack
|
||||
- _`<CLOUD_ACCESS_POLICY_TOKEN>`_: Token from the Cloud Access Policy you created in the Grafana Cloud portal
|
||||
- _`<STACK_NAME>`_: Name of your stack
|
||||
- _`<ORG_NAME>`_: Name of the organization in Grafana Cloud
|
||||
|
||||
Create an API key in the Grafana stack.
|
||||
You'll need this key to configure Ansible to be able to create data source, folders, and dashboards.
|
||||
The playbook registers the stack creation result and displays the stack URL, which you'll need for subsequent resource management.
|
||||
|
||||
## Create an API key in your Grafana stack
|
||||
|
||||
Create an API key in the Grafana stack. You'll need this key to configure Ansible to create data sources, folders, and dashboards.
|
||||
|
||||
1. Log into your Grafana Cloud instance.
|
||||
2. Click **Administration** and select **API keys**.
|
||||
@@ -70,171 +84,169 @@ You'll need this key to configure Ansible to be able to create data source, fold
|
||||
5. In **Role**, select **Admin** or **Editor** to associate the role with this API key.
|
||||
6. Click **Copy** to save it for later use.
|
||||
|
||||
## Add a data source
|
||||
## Add resources using playbooks
|
||||
|
||||
This guide uses the InfluxDB data source.
|
||||
The required arguments vary depending on the type of data source you select.
|
||||
### Add a data source
|
||||
|
||||
1. Create a file named `data-source.yml` and add the following:
|
||||
The following steps use the InfluxDB data source. The required arguments vary depending on the type of data source you select.
|
||||
|
||||
```yaml
|
||||
- name: Add/Update data source
|
||||
connection: local
|
||||
hosts: localhost
|
||||
Create a file named `data-source.yml`:
|
||||
|
||||
vars:
|
||||
data_sources:
|
||||
[
|
||||
{
|
||||
name: '<data-source-name>',
|
||||
type: 'influxdb',
|
||||
url: '<data-source-url>',
|
||||
user: '<username>',
|
||||
secureJsonData: { password: '<password>' },
|
||||
database: '<db-name>',
|
||||
id: <id>,
|
||||
uid: '<uid>',
|
||||
access: 'proxy',
|
||||
},
|
||||
]
|
||||
```yaml
|
||||
- name: Add/Update data source
|
||||
connection: local
|
||||
hosts: localhost
|
||||
|
||||
grafana_api_key: '<API-Key>'
|
||||
stack_name: '<stack-name>'
|
||||
vars:
|
||||
grafana_url: 'https://<STACK_NAME>.grafana.net'
|
||||
grafana_api_key: '<GRAFANA_API_KEY>'
|
||||
data_source_config:
|
||||
name: '<DATA_SOURCE_NAME>'
|
||||
type: 'influxdb'
|
||||
url: '<DATA_SOURCE_URL>'
|
||||
user: '<USERNAME>'
|
||||
secureJsonData:
|
||||
password: '<PASSWORD>'
|
||||
database: '<DATABASE_NAME>'
|
||||
uid: '<UID>'
|
||||
access: 'proxy'
|
||||
|
||||
tasks:
|
||||
- name: Create/Update Data sources
|
||||
grafana.grafana.datasource:
|
||||
datasource: '{{ item }}'
|
||||
stack_slug: '{{ stack_name }}'
|
||||
grafana_api_key: '{{ grafana_api_key }}'
|
||||
state: present
|
||||
loop: '{{ data_sources }}'
|
||||
```
|
||||
tasks:
|
||||
- name: Create/Update Data source
|
||||
grafana.grafana.datasource:
|
||||
dataSource: '{{ data_source_config }}'
|
||||
grafana_url: '{{ grafana_url }}'
|
||||
grafana_api_key: '{{ grafana_api_key }}'
|
||||
state: present
|
||||
```
|
||||
|
||||
1. Replace the following field values:
|
||||
- `<data-source-name>` with the name of the data source to be added in Grafana.
|
||||
- `<data-source-url>` with URL of your data source.
|
||||
- `<username>` with the username for authenticating with your data source.
|
||||
- `<password>` with the password for authenticating with your data source.
|
||||
- `<db-name>` with name of your database.
|
||||
- `<id>` with the ID for your data source in Grafana.
|
||||
- `<uid>` wth the UID for your data source in Grafana.
|
||||
- `<stack-name>` with the name of your stack.
|
||||
- `<API-key>` with the [API key created in the Grafana instance](#create-an-api-key-in-the-grafana-stack).
|
||||
Replace the placeholders with your values:
|
||||
|
||||
## Add a folder
|
||||
- _`<DATA_SOURCE_NAME>`_: Name of the data source to be added in Grafana
|
||||
- _`<DATA_SOURCE_URL>`_: URL of your data source
|
||||
- _`<USERNAME>`_: Username for authenticating with your data source
|
||||
- _`<PASSWORD>`_: Password for authenticating with your data source
|
||||
- _`<DATABASE_NAME>`_: Name of your database
|
||||
- _`<UID>`_: UID for your data source in Grafana
|
||||
- _`<STACK_NAME>`_: Name of your stack
|
||||
- _`<GRAFANA_API_KEY>`_: API key created in the Grafana instance
|
||||
|
||||
This Ansible playbook creates a folder in your Grafana instance by using the [Folder module](https://docs.ansible.com/ansible/latest/collections/grafana/grafana/folder_module.html#ansible-collections-grafana-grafana-folder-module).
|
||||
### Add a folder
|
||||
|
||||
1. Create a file named `folder.yml` and add the following:
|
||||
This playbook creates a folder in your Grafana instance using the [Folder module](https://docs.ansible.com/ansible/latest/collections/grafana/grafana/folder_module.html#ansible-collections-grafana-grafana-folder-module).
|
||||
|
||||
```yaml
|
||||
- name: Add/Update Folders
|
||||
connection: local
|
||||
hosts: localhost
|
||||
Create a file named `folder.yml`:
|
||||
|
||||
vars:
|
||||
folders: [{ title: '<folder-name>', uid: '<uid>' }]
|
||||
```yaml
|
||||
- name: Add/Update Folders
|
||||
connection: local
|
||||
hosts: localhost
|
||||
|
||||
stack_name: '<stack-name>'
|
||||
grafana_api_key: <API-key>
|
||||
vars:
|
||||
grafana_url: 'https://<STACK_NAME>.grafana.net'
|
||||
grafana_api_key: '<GRAFANA_API_KEY>'
|
||||
folders:
|
||||
- title: '<FOLDER_NAME>'
|
||||
uid: '<UID>'
|
||||
|
||||
tasks:
|
||||
- name: Create/Update a Folder in Grafana
|
||||
grafana.grafana.folder:
|
||||
title: '{{ item.title }}'
|
||||
uid: '{{ item.uid }}'
|
||||
stack_slug: '{{ stack_name }}'
|
||||
grafana_api_key: '{{ grafana_api_key }}'
|
||||
state: present
|
||||
loop: '{{ folders }}'
|
||||
```
|
||||
tasks:
|
||||
- name: Create/Update a Folder in Grafana
|
||||
grafana.grafana.folder:
|
||||
title: '{{ item.title }}'
|
||||
uid: '{{ item.uid }}'
|
||||
grafana_url: '{{ grafana_url }}'
|
||||
grafana_api_key: '{{ grafana_api_key }}'
|
||||
state: present
|
||||
loop: '{{ folders }}'
|
||||
```
|
||||
|
||||
1. Replace the following field values:
|
||||
- `<folder-name>` with the name of the folder to be added in Grafana.
|
||||
- `<uid>` with the UID for your folder in Grafana.
|
||||
- `<stack-name>` with the name of your stack.
|
||||
- `<API-key>` with the [API key created in the Grafana instance](#create-an-api-key-in-the-grafana-stack).
|
||||
Replace the placeholders with your values:
|
||||
|
||||
## Add a dashboard to the folder
|
||||
- _`<FOLDER_NAME>`_: Name of the folder to be added in Grafana
|
||||
- _`<UID>`_: UID for your folder in Grafana
|
||||
- _`<STACK_NAME>`_: Name of your stack
|
||||
- _`<GRAFANA_API_KEY>`_: API key created in the Grafana instance
|
||||
|
||||
This Ansible playbook iterates through the dashboard JSON source code files in the folder referenced in `dashboards_path` and adds them in the Grafana instance by using the [Dashboard module](https://docs.ansible.com/ansible/latest/collections/grafana/grafana/dashboard_module.html#ansible-collections-grafana-grafana-dashboard-module).
|
||||
### Add a dashboard to the folder
|
||||
|
||||
1. Create a file named `dashboard.yml` and add the following:
|
||||
This playbook iterates through the dashboard JSON source code files in the folder referenced in `dashboards_path` and adds them to the Grafana instance using the [Dashboard module](https://docs.ansible.com/ansible/latest/collections/grafana/grafana/dashboard_module.html#ansible-collections-grafana-grafana-dashboard-module).
|
||||
|
||||
```yaml
|
||||
- name: Add/Update Dashboards
|
||||
connection: local
|
||||
hosts: localhost
|
||||
Create a file named `dashboard.yml`:
|
||||
|
||||
vars:
|
||||
dashboards_path: <path-to-dashboard-files> # Example "./dashboards"
|
||||
stack_name: "<stack-name>"
|
||||
grafana_api_key: <API-key>
|
||||
```yaml
|
||||
- name: Add/Update Dashboards
|
||||
connection: local
|
||||
hosts: localhost
|
||||
|
||||
tasks:
|
||||
- name: Find dashboard files
|
||||
find:
|
||||
paths: "{{ dashboards_path }}"
|
||||
file_type: file
|
||||
recurse: Yes
|
||||
patterns: "*.json"
|
||||
register: files_matched
|
||||
no_log: True
|
||||
vars:
|
||||
grafana_url: 'https://<STACK_NAME>.grafana.net'
|
||||
grafana_api_key: '<GRAFANA_API_KEY>'
|
||||
dashboards_path: '<PATH_TO_DASHBOARD_FILES>' # Example "./dashboards"
|
||||
|
||||
- name: Create list of dashboard file names
|
||||
set_fact:
|
||||
dashboard_file_names: "{{ dashboard_file_names | default ([]) + [item.path] }}"
|
||||
loop: "{{ files_matched.files }}"
|
||||
no_log: True
|
||||
tasks:
|
||||
- name: Find dashboard files
|
||||
find:
|
||||
paths: '{{ dashboards_path }}'
|
||||
file_type: file
|
||||
recurse: true
|
||||
patterns: '*.json'
|
||||
register: files_matched
|
||||
no_log: true
|
||||
|
||||
- name: Create/Update a dashboard
|
||||
grafana.grafana.dashboard:
|
||||
dashboard: "{{ lookup('ansible.builtin.file','{{ item }}' ) }}"
|
||||
stack_slug: "{{ stack_name }}"
|
||||
grafana_api_key: "{{ grafana_api_key }}"
|
||||
state: present
|
||||
loop: "{{ dashboard_file_names }}"
|
||||
```
|
||||
- name: Create list of dashboard file names
|
||||
set_fact:
|
||||
dashboard_file_names: '{{ dashboard_file_names | default([]) + [item.path] }}'
|
||||
loop: '{{ files_matched.files }}'
|
||||
no_log: true
|
||||
|
||||
1. Replace the following field values:
|
||||
- `<path-to-dashboard-files>` with the path to the folder containing dashboard JSON source code files.
|
||||
- `<stack-name>` with the name of your stack.
|
||||
- `<API-key>` with the [API key created in the Grafana instance](#create-an-api-key-in-the-grafana-stack).
|
||||
- name: Create/Update a dashboard
|
||||
grafana.grafana.dashboard:
|
||||
dashboard: "{{ lookup('ansible.builtin.file', item) }}"
|
||||
grafana_url: '{{ grafana_url }}'
|
||||
grafana_api_key: '{{ grafana_api_key }}'
|
||||
state: present
|
||||
loop: '{{ dashboard_file_names }}'
|
||||
```
|
||||
|
||||
Replace the placeholders with your values:
|
||||
|
||||
- _`<PATH_TO_DASHBOARD_FILES>`_: Path to the folder containing dashboard JSON source code files
|
||||
- _`<STACK_NAME>`_: Name of your stack
|
||||
- _`<GRAFANA_API_KEY>`_: API key created in the Grafana instance
|
||||
|
||||
## Run the Ansible playbooks
|
||||
|
||||
In a terminal, run the following commands from the directory where all of the Ansible playbooks are located.
|
||||
|
||||
1. To create the Grafana Cloud stack.
|
||||
Create the Grafana Cloud stack:
|
||||
|
||||
```shell
|
||||
ansible-playbook cloud-stack.yml
|
||||
```
|
||||
```sh
|
||||
ansible-playbook cloud-stack.yml
|
||||
```
|
||||
|
||||
1. To add a data source to the Grafana stack.
|
||||
Add a data source to the Grafana stack:
|
||||
|
||||
```shell
|
||||
ansible-playbook data-source.yml
|
||||
```
|
||||
```sh
|
||||
ansible-playbook data-source.yml
|
||||
```
|
||||
|
||||
1. To add a folder to the Grafana stack
|
||||
Add a folder to the Grafana stack:
|
||||
|
||||
```shell
|
||||
ansible-playbook folder.yml
|
||||
```
|
||||
```sh
|
||||
ansible-playbook folder.yml
|
||||
```
|
||||
|
||||
1. To add a dashboard to the folder in your Grafana stack.
|
||||
Add a dashboard to the folder in your Grafana stack:
|
||||
|
||||
```shell
|
||||
ansible-playbook dashboard.yml
|
||||
```
|
||||
```sh
|
||||
ansible-playbook dashboard.yml
|
||||
```
|
||||
|
||||
## Validation
|
||||
## Validate your configuration
|
||||
|
||||
Once you run the Ansible playbooks, you should be able to verify the following:
|
||||
After you've run the Ansible playbooks, you can verify the following:
|
||||
|
||||
- The new Grafana stack is created and visible in the Cloud Portal.
|
||||
- The new Grafana Cloud stack is created and visible in the Cloud Portal.
|
||||
|
||||

|
||||
|
||||
@@ -242,18 +254,22 @@ Once you run the Ansible playbooks, you should be able to verify the following:
|
||||
|
||||

|
||||
|
||||
- A new folder in Grafana.
|
||||
In the following image, a folder named `Demos` was added.
|
||||
- A new folder is available in your Grafana stack. In the following image, a folder named `Demos` was added.
|
||||
|
||||

|
||||
|
||||
- A new dashboard in the Grafana stack.
|
||||
In the following image a dashboard named `InfluxDB Cloud Demos` was created inside the "Demos" folder.
|
||||
- A new dashboard is visible in the Grafana stack. In the following image, a dashboard named `InfluxDB Cloud Demos` was created inside the "Demos" folder.
|
||||
|
||||

|
||||
|
||||
## Summary
|
||||
## Next steps
|
||||
|
||||
In this guide, you created a Grafana Cloud stack along with a data source, folder, and dashboard imported from a JSON file using Ansible.
|
||||
You've successfully created a Grafana Cloud stack along with a data source, a folder, and a dashboard using Ansible. Your Grafana infrastructure is now managed through code.
|
||||
|
||||
To learn more about managing Grafana using Ansible, refer to the [Grafana Ansible collection](https://docs.ansible.com/ansible/latest/collections/grafana/grafana/).
|
||||
To learn more about managing Grafana with Infrastructure as code:
|
||||
|
||||
- [Grafana Ansible collection documentation](https://docs.ansible.com/ansible/latest/collections/grafana/grafana/)
|
||||
- [Ansible playbook best practices](https://docs.ansible.com/ansible/latest/user_guide/playbooks_best_practices.html)
|
||||
- [Grafana API documentation](/docs/grafana/latest/developers/http_api/)
|
||||
- [Grafana Cloud API documentation](https://grafana.com/docs/grafana-cloud/developer-resources/api-reference/)
|
||||
- [Infrastructure as Code with Terraform](/docs/grafana/latest/as-code/infrastructure-as-code/terraform/)
|
||||
|
||||
Reference in New Issue
Block a user