Skip to content

Commit acbfa19

Browse files
feat: ansible local-exec
- mvp for ansible being called via tf - todo: cleanup
1 parent 80f3767 commit acbfa19

11 files changed

Lines changed: 291 additions & 111 deletions

.gitignore

Lines changed: 166 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# ETC
2-
grafana-config.ini
2+
.terraform.lock.hcl
33
dashboard.yml
4+
grafana-config.ini
5+
hosts
46
node-exporter.json
57
playbooks/*
68
prometheus.yml
79
tfplan
8-
.terraform.lock.hcl
910

1011
# General
1112
.DS_Store
@@ -70,6 +71,169 @@ override.tf.json
7071
.terraformrc
7172
terraform.rc
7273

74+
# Byte-compiled / optimized / DLL files
75+
__pycache__/
76+
*.py[cod]
77+
*$py.class
78+
79+
# C extensions
80+
*.so
81+
82+
# Distribution / packaging
83+
.Python
84+
build/
85+
develop-eggs/
86+
dist/
87+
downloads/
88+
eggs/
89+
.eggs/
90+
lib/
91+
lib64/
92+
parts/
93+
sdist/
94+
var/
95+
wheels/
96+
share/python-wheels/
97+
*.egg-info/
98+
.installed.cfg
99+
*.egg
100+
MANIFEST
101+
102+
# PyInstaller
103+
# Usually these files are written by a python script from a template
104+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
105+
*.manifest
106+
*.spec
107+
108+
# Installer logs
109+
pip-log.txt
110+
pip-delete-this-directory.txt
111+
112+
# Unit test / coverage reports
113+
htmlcov/
114+
.tox/
115+
.nox/
116+
.coverage
117+
.coverage.*
118+
.cache
119+
nosetests.xml
120+
coverage.xml
121+
*.cover
122+
*.py,cover
123+
.hypothesis/
124+
.pytest_cache/
125+
cover/
126+
127+
# Translations
128+
*.mo
129+
*.pot
130+
131+
# Django stuff:
132+
*.log
133+
local_settings.py
134+
db.sqlite3
135+
db.sqlite3-journal
136+
137+
# Flask stuff:
138+
instance/
139+
.webassets-cache
140+
141+
# Scrapy stuff:
142+
.scrapy
143+
144+
# Sphinx documentation
145+
docs/_build/
146+
147+
# PyBuilder
148+
.pybuilder/
149+
target/
150+
151+
# Jupyter Notebook
152+
.ipynb_checkpoints
153+
154+
# IPython
155+
profile_default/
156+
ipython_config.py
157+
158+
# pyenv
159+
# For a library or package, you might want to ignore these files since the code is
160+
# intended to run in multiple environments; otherwise, check them in:
161+
# .python-version
162+
163+
# pipenv
164+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
165+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
166+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
167+
# install all needed dependencies.
168+
#Pipfile.lock
169+
170+
# poetry
171+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
172+
# This is especially recommended for binary packages to ensure reproducibility, and is more
173+
# commonly ignored for libraries.
174+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
175+
#poetry.lock
176+
177+
# pdm
178+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
179+
#pdm.lock
180+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
181+
# in version control.
182+
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
183+
.pdm.toml
184+
.pdm-python
185+
.pdm-build/
186+
187+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
188+
__pypackages__/
189+
190+
# Celery stuff
191+
celerybeat-schedule
192+
celerybeat.pid
193+
194+
# SageMath parsed files
195+
*.sage.py
196+
197+
# Environments
198+
.env
199+
.venv
200+
env/
201+
venv/
202+
ENV/
203+
env.bak/
204+
venv.bak/
205+
206+
# Spyder project settings
207+
.spyderproject
208+
.spyproject
209+
210+
# Rope project settings
211+
.ropeproject
212+
213+
# mkdocs documentation
214+
/site
215+
216+
# mypy
217+
.mypy_cache/
218+
.dmypy.json
219+
dmypy.json
220+
221+
# Pyre type checker
222+
.pyre/
223+
224+
# pytype static type analyzer
225+
.pytype/
226+
227+
# Cython debug symbols
228+
cython_debug/
229+
230+
# PyCharm
231+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
232+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
233+
# and can be added to the global gitignore or merged into this file. For a more nuclear
234+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
235+
#.idea/
236+
73237
# INCLUDE
74238
!**/*.example
75239
!**/.gitkeep

.tool-versions

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
terraform 1.9.8
2+
python 3.11.10

ansible.tf

Lines changed: 0 additions & 77 deletions
This file was deleted.

hosts.tpl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[node_exporters]
2+
%{ for host in node_exporter_hosts ~}
3+
${host.name} ansible_host=${host.ip} ansible_user=${host.ssh_user} ansible_port=${host.ssh_port}
4+
%{ endfor ~}
5+
6+
[all:vars]
7+
node_exporter_port=${node_exporter_port}
8+
ansible_ssh_private_key_file=${ssh_private_key_path}

main.tf

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,24 @@ resource "null_resource" "wait_for_containers" {
162162
}
163163
}
164164

165-
resource "null_resource" "wait_for_remote_containers" {
166-
depends_on = [
167-
null_resource.run_ansible
168-
]
165+
resource "local_file" "ansible_inventory" {
166+
content = templatefile("${path.module}/hosts.tpl", {
167+
node_exporter_hosts = var.node_exporter_hosts
168+
node_exporter_port = var.node_exporter_port
169+
ssh_private_key_path = pathexpand(var.ssh_private_key_path)
170+
})
171+
filename = "${path.module}/hosts"
172+
}
173+
174+
resource "null_resource" "ansible_playbook" {
175+
depends_on = [local_file.ansible_inventory]
176+
177+
triggers = {
178+
inventory_content = local_file.ansible_inventory.content
179+
playbook_hash = filemd5("${path.module}/node_exporter.yml")
180+
}
169181

170182
provisioner "local-exec" {
171-
command = "sleep 10"
183+
command = "ansible-playbook -i ${local_file.ansible_inventory.filename} node_exporter.yml"
172184
}
173185
}

node_exporter.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
- name: Deploy node-exporter containers
3+
hosts: all
4+
gather_facts: false
5+
tasks:
6+
- name: Pull node-exporter image
7+
community.docker.docker_image:
8+
name: prom/node-exporter:latest
9+
source: pull
10+
force_source: true
11+
12+
- name: Start node-exporter container
13+
community.docker.docker_container:
14+
name: node-exporter
15+
image: prom/node-exporter:latest
16+
state: started
17+
restart_policy: unless-stopped
18+
published_ports:
19+
- "{{ node_exporter_port }}:9100"
20+
container_default_behavior: no_defaults

providers.tf

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ terraform {
44
source = "kreuzwerker/docker"
55
version = "~> 3.0.2"
66
}
7-
ansible = {
8-
source = "ansible/ansible"
9-
version = "~> 1.3.0"
10-
}
117
}
128
}
139

@@ -18,5 +14,3 @@ provider "docker" {
1814
config_file = pathexpand("~/.docker/config.json")
1915
}
2016
}
21-
22-
provider "ansible" {}

requirements.txt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
ansible-compat==24.9.1
2+
ansible-core==2.17.5
3+
ansible-lint==24.9.2
4+
attrs==24.2.0
5+
black==24.10.0
6+
bracex==2.5.post1
7+
certifi==2024.8.30
8+
cffi==1.17.1
9+
charset-normalizer==3.4.0
10+
click==8.1.7
11+
cryptography==43.0.3
12+
docker==7.1.0
13+
filelock==3.16.1
14+
idna==3.10
15+
importlib_metadata==8.5.0
16+
Jinja2==3.1.4
17+
jsonschema==4.23.0
18+
jsonschema-specifications==2024.10.1
19+
markdown-it-py==3.0.0
20+
MarkupSafe==3.0.2
21+
mdurl==0.1.2
22+
mypy-extensions==1.0.0
23+
packaging==24.1
24+
pathspec==0.12.1
25+
platformdirs==4.3.6
26+
pycparser==2.22
27+
Pygments==2.18.0
28+
PyYAML==6.0.2
29+
referencing==0.35.1
30+
requests==2.32.3
31+
resolvelib==1.0.1
32+
rich==13.9.3
33+
rpds-py==0.20.0
34+
ruamel.yaml==0.18.6
35+
ruamel.yaml.clib==0.2.12
36+
subprocess-tee==0.4.2
37+
urllib3==2.2.3
38+
wcmatch==10.0
39+
yamllint==1.35.1
40+
zipp==3.20.2

requirements.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
collections:
3+
- name: ansible.posix
4+
- name: community.general
5+
6+
roles:
7+
- name: asdf
8+
src: https://github.com/pythoninthegrass/ansible-role-asdf.git
9+
version: master
10+
- name: geerlingguy.swap
11+
src: https://github.com/pythoninthegrass/ansible-role-swap.git
12+
version: master
13+
- name: geerlingguy.docker
14+
- name: geerlingguy.pip

0 commit comments

Comments
 (0)