feat: aggiunti esempi per 9_template
This commit is contained in:
parent
3537b927db
commit
d230bc50ab
7 changed files with 85 additions and 3 deletions
13
9_template/1_playbook.yaml
Normal file
13
9_template/1_playbook.yaml
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
---
|
||||
- name: edito il file resolv.conf
|
||||
hosts: localhost
|
||||
vars:
|
||||
dns1: xxx.xxx.xxx.xxx
|
||||
dns2: yyy.yyy.yyy.yyy
|
||||
domain: pippo.net
|
||||
|
||||
tasks:
|
||||
- name: salvo nella /tmp in locale il risultato del template
|
||||
ansible.builtin.template:
|
||||
src: 1_resolv.conf.j2
|
||||
dest: /tmp/resolv.conf
|
||||
15
9_template/2_playbook.yaml
Normal file
15
9_template/2_playbook.yaml
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
---
|
||||
- name: edito il file resolv.conf
|
||||
hosts: localhost
|
||||
vars:
|
||||
dns:
|
||||
- xxx.xxx.xxx.xxx
|
||||
- yyy.yyy.yyy.yyy
|
||||
- zzz.zzz.zzz.zzz
|
||||
domain: pippo.net
|
||||
|
||||
tasks:
|
||||
- name: salvo nella /tmp in locale il risultato del template
|
||||
ansible.builtin.template:
|
||||
src: 2_resolv.conf.j2
|
||||
dest: /tmp/resolv.conf
|
||||
19
9_template/3_playbook.yaml
Normal file
19
9_template/3_playbook.yaml
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
---
|
||||
- name: edito il file resolv.conf
|
||||
hosts: localhost
|
||||
vars:
|
||||
dns_prod:
|
||||
- xxx.xxx.xxx.xxx
|
||||
- yyy.yyy.yyy.yyy
|
||||
- zzz.zzz.zzz.zzz
|
||||
dns_dr:
|
||||
- aaa.aaa.aaa.aaa
|
||||
- bbb.bbb.bbb.bbb
|
||||
- ccc.ccc.ccc.ccc
|
||||
domain: pippo.net
|
||||
|
||||
tasks:
|
||||
- name: salvo nella /tmp in locale il risultato del template
|
||||
ansible.builtin.template:
|
||||
src: 3_resolv.conf.j2
|
||||
dest: /tmp/resolv.conf
|
||||
|
|
@ -1,9 +1,27 @@
|
|||
# Alcuni esempi pratici sull'utilizzo dei template in un playbook Ansible
|
||||
|
||||
Di default i template vengono salvati nella directory templates (all'inteno della directory del playbook) ed hanno estensione .j2 (jinja)
|
||||
Di default i template sono salvati nella directory templates (all'inteno della directory del playbook) ed hanno estensione .j2 (jinja)
|
||||
|
||||
1)
|
||||
1) Playbook di esempio che utilizza un semplice template jinja, lo popola con alcune variabili e lo copia nella destinazione indicata nel task:
|
||||
- per lanciare il playbook:
|
||||
```
|
||||
ansible-playbook ...
|
||||
ansible-playbook 1_playbook.yaml
|
||||
cat /tmp/resolv.conf # ← per vedere se il file e' stato editato correttamente
|
||||
```
|
||||
2) Playbook di esempio che utilizza un semplice template jinja, lo popola ciclando con un for per le n variabili e lo copia nella destinazione indicata nel task:
|
||||
- per lanciare il playbook:
|
||||
```
|
||||
ansible-playbook 2_playbook.yaml
|
||||
cat /tmp/resolv.conf # ← per vedere se il file e' stato editato correttamente
|
||||
```
|
||||
3) Playbook di esempio che utilizza un semplice template jinja, lo popola ciclando con un for, selezionando le n variabili con un if, e lo copia nella destinazione indicata nel task:
|
||||
- per lanciare il playbook selezionando il **primo** set di variabili:
|
||||
```
|
||||
ansible-playbook 3_playbook.yaml -e env=prod
|
||||
cat /tmp/resolv.conf # ← per vedere se il file e' stato editato correttamente
|
||||
```
|
||||
- per lanciare il playbook selezionando il **secondo** set di variabili:
|
||||
```
|
||||
ansible-playbook 3_playbook.yaml -e env=dr
|
||||
cat /tmp/resolv.conf # ← per vedere se il file e' stato editato correttamente
|
||||
```
|
||||
|
|
|
|||
3
9_template/templates/1_resolv.conf.j2
Normal file
3
9_template/templates/1_resolv.conf.j2
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
nameserver {{dns1}}
|
||||
nameserver {{dns2}}
|
||||
search {{domain}}
|
||||
4
9_template/templates/2_resolv.conf.j2
Normal file
4
9_template/templates/2_resolv.conf.j2
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
{% for ip in dns %}
|
||||
nameserver {{ip}}
|
||||
{% endfor %}
|
||||
search {{domain}}
|
||||
10
9_template/templates/3_resolv.conf.j2
Normal file
10
9_template/templates/3_resolv.conf.j2
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
{% if env == "dr" %}
|
||||
{% for ip in dns_dr %}
|
||||
nameserver {{ip}}
|
||||
{% endfor %}
|
||||
{% else %}
|
||||
{% for ip in dns_prod %}
|
||||
nameserver {{ip}}
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
search {{domain}}
|
||||
Loading…
Add table
Reference in a new issue