Skip to content

Commit a6a0c7f

Browse files
committed
feat(ansible_docker_installation): add docker_installation using ansible as a prompt
1 parent 7deb630 commit a6a0c7f

2 files changed

Lines changed: 145 additions & 2 deletions

File tree

Lines changed: 142 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,143 @@
11
def ansible_docker_install(input):
2-
return ""
2+
3+
docker_hosts = input.hosts
4+
docker_inventory = (f"[docker_nodes]\n" + "\n".join(docker_hosts))
5+
docker_ansible_port = input.ansible_port
6+
docker_ansible_user = input.ansible_user
7+
docker_items_in_task = "{{ item }}"
8+
docker_prerequisite_packages_in_task = "{{ prerequisite_packages }}"
9+
ansible_architecture_in_task = "{{ ansible_architecture }}"
10+
ansible_distribution_release_in_task = "{{ ansible_distribution_release }}"
11+
docker_packages_in_task = "{{ docker_packages }}"
12+
docker_services_in_task = "{{ docker_services }}"
13+
14+
15+
16+
prompt = f"""
17+
Generate a Python code to generate an Ansible project (project name is app/media/MyAnsible)
18+
that dynamically provisions Ansible resources ensuring a modular, flexible structure. Only provide
19+
Python code, no explanations or markdown formatting, without ```python entry.
20+
The project should be organized as follows:
21+
22+
The structure of this project must be as follows:
23+
```
24+
├── ansible.cfg
25+
├── group_vars
26+
│   |── docker_nodes
27+
│  
28+
├── hosts
29+
├── host_vars
30+
├── docker_playbook.yml
31+
└── roles
32+
└── install_docker
33+
├── defaults
34+
│   └── main.yml
35+
├── files
36+
│   └── sample.sh
37+
├── handlers
38+
│   └── main.yml
39+
├── tasks
40+
│   └── main.yml
41+
├── templates
42+
│   └── sample.j2
43+
└── vars
44+
└── main.yml
45+
```
46+
- The content of ansible.cfg must be as follows:
47+
```
48+
[defaults]
49+
host_key_checking=false
50+
```
51+
- group_vars directory includes a single file called "docker_nodes" and the content of this file must be as follows:
52+
```
53+
ansible_port: {docker_ansible_port}
54+
ansible_user: {docker_ansible_user}
55+
```
56+
- there is file called "hosts" which its content must be as follows:
57+
```
58+
{docker_inventory}
59+
```
60+
- There is an empty directory called "host_vars" with no files included
61+
- There is a file called "docker_playbook.yml" which its content must be as follows:
62+
```
63+
- hosts: all
64+
roles:
65+
- install_docker
66+
```
67+
- There is a directory called "roles" which a sub-directory called "install_docker" (roles/install_docker)
68+
"install_docker" has multiple sub-directories, so let's dive deeper into each its sub-directories:
69+
- (install_docker/tasks): This path has a file called "main.yml" which its content must be as follows:
70+
```
71+
---
72+
- name: Install prerequisite packages
73+
apt:
74+
name: "{docker_items_in_task}"
75+
state: present
76+
loop: "{docker_prerequisite_packages_in_task}""
77+
- name: Create directory for Docker keyrings
78+
file:
79+
path: /etc/apt/keyrings
80+
state: directory
81+
mode: '0755'
82+
- name: Download Docker's official GPG key
83+
get_url:
84+
url: https://download.docker.com/linux/ubuntu/gpg
85+
dest: /etc/apt/keyrings/docker.asc
86+
mode: '0644'
87+
- name: Add Docker repository to apt sources
88+
copy:
89+
content: |
90+
deb [arch={ansible_architecture_in_task} signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu {ansible_distribution_release_in_task} stable
91+
dest: /etc/apt/sources.list.d/docker.list
92+
- name: Update apt cache after adding Docker repo
93+
apt:
94+
update_cache: yes
95+
- name: Install Docker packages
96+
apt:
97+
name: "{docker_items_in_task}"
98+
state: present
99+
loop: "{docker_packages_in_task}""
100+
- name: Ensure Docker and containerd services are started and enabled
101+
service:
102+
name: "{docker_items_in_task}"
103+
state: started
104+
enabled: yes
105+
loop: "{docker_services_in_task}""
106+
```
107+
- (install_docker/vars): This path has a file called "main.yml" which its content must be as follows:
108+
```
109+
prerequisite_packages:
110+
- ca-certificates
111+
- curl
112+
113+
docker_services:
114+
- docker
115+
- containerd
116+
117+
docker_packages:
118+
- docker-ce
119+
- docker-ce-cli
120+
- containerd.io
121+
- docker-buildx-plugin
122+
- docker-compose-plugin
123+
```
124+
125+
finally just give me a python code without any note that can generate a project folder with the
126+
given schema without ```python entry. and we dont need any base directory in the python code.
127+
the final ansible template must work very well without any error!
128+
129+
the python code you give me, must have structure like that:
130+
131+
import os
132+
project_name = "app/media/MyAnsible"
133+
foo_dir = os.path.join(project_name, "bar")
134+
x_dir = os.path.join(modules_dir, "y")
135+
136+
# Create project directories
137+
os.makedirs(ansible_dir, exist_ok=True)
138+
139+
# Create main.tf
140+
with open(os.path.join(project_name, "main.tf"), "w") as main_file:
141+
# any thing you need
142+
"""
143+
return prompt

app/template_generators/ansible/install/nginx.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ def ansible_nginx_install_ubuntu(input):
2929
└── install_nginx
3030
├── defaults
3131
│   └── main.yml
32+
├── files
33+
│   └── sample.sh
3234
├── handlers
3335
│   └── main.yml
3436
├── tasks
3537
│   └── main.yml
3638
├── templates
37-
│   └── main.yml
39+
│   └── sample.j2
3840
└── vars
3941
└── main.yml
4042
```

0 commit comments

Comments
 (0)