Skip to content
Snippets Groups Projects
Commit 03f7f80c authored by ines.maya's avatar ines.maya
Browse files

first commit

parents
No related branches found
No related tags found
No related merge requests found
backup
test
\ No newline at end of file
# TP 3 : Ansible
Inès MAYA
## Fonctionnement
cmd : ansible-playbook -i inv.yml ansible.yml
\ No newline at end of file
[defaults]
invetory = hosts
remote_user = root
host_key_checking = false
[privilege_escalation]
become = true
become_method = sudo
become_user = root
become_ask_pass = false
\ No newline at end of file
- hosts: all
vars:
# Variables pour les interfaces
interfaces:
H1:
eth0:
address: 192.168.1.10
netmask: 255.255.255.0
gateway: 192.168.1.1
H2:
eth0:
address: 192.168.2.10
netmask: 255.255.255.0
gateway: 192.168.2.1
R1:
eth0:
address: 192.168.1.1
netmask: 255.255.255.0
eth1:
address: 10.0.0.1
netmask: 255.255.255.0
R2:
eth0:
address: 192.168.2.1
netmask: 255.255.255.0
eth1:
address: 10.0.0.2
netmask: 255.255.255.0
tasks:
- name: Restart interfaces hostss"
template:
src: interfaces.j2
dest: /etc/network/interfaces.d/{{ item.key }}-{{ item.value.key }}
loop: "{{ interfaces | dict2items | subelements('value') }}"
notify: restart networking
- name: Configurer les routes statiques pour H1 et H2
lineinfile:
path: /etc/network/interfaces.d/{{ item }}-eth0
line: " post-up route add default gw {{ interfaces[item].eth0.gateway }}"
loop:
- H1
- H2
handlers:
- name: restart networking
service:
name: networking
state: restarted
hosts 0 → 100644
[py3-hosts]
H1
R1
H2
[py3-hosts:vars]
ansible_python_interpreter=/usr/bin/python3
\ No newline at end of file
# Loop over all the variables that start with inventory_hostname followed by "_ifname" (e.g., H1_ifname, R1_ifname1, etc.)
{% for var in vars if var.startswith(inventory_hostname + '_ifname') %}
# Get the interface name from the variable value (e.g., eth0, eth1, etc.)
iface {{ vars[var] }} inet static
# Get the IP address from another variable that has the same suffix as the current variable (e.g., H1_ipaddr, R1_ipaddr1, etc.)
address {{ vars[inventory_hostname + '_ipaddr' + var[-1]] }}
# If the host belongs to the hosts group, add a default gateway from another variable that has the same suffix as well (e.g., H1_gwaddr, H2_gwaddr, etc.)
{% if inventory_hostname in groups['hosts'] %}
gateway {{ vars[inventory_hostname + '_gwaddr' + var[-1]] }}
{% endif %}
# If the host belongs to the routers group, add a static route to reach the other subnet via another variable that has an incremented suffix (e.g., R1_ipaddr2, R2_ipaddr2, etc.)
{% if inventory_hostname in groups['routers'] %}
post-up ip route add {{ '192.168.' + ('10' if '20' in vars[inventory_hostname + '_ipaddr' + var[-1]] else '20') + '.0/24' }} via {{ vars[inventory_hostname + '_ipaddr' + str(int(var[-1]) + 1)] }}
{% endif %}
{% endfor %}
\ No newline at end of file
H1
H2
R1
R1
[routers]
R1
R2
[hosts]
H1
H2
\ No newline at end of file
# templates/interfaces.j2
auto {{ item.value.key }}
iface {{ item.value.key }} inet static
address {{ item.value.value.address }}
netmask {{ item.value.value.netmask }}
{% if item.value.value.gateway is defined %}
gateway {{ item.value.value.gateway }}
{% endif %}
\ No newline at end of file
v2.yml 0 → 100644
---
- name: Configure network interfaces
hosts: all
become: true
vars:
# Interface configuration
h1_eth0:
ip_address: "192.168.1.1"
netmask: "255.255.255.0"
gateway: "192.168.1.254"
h2_eth0:
ip_address: "192.168.2.1"
netmask: "255.255.255.0"
gateway: "192.168.2.254"
r1_eth0:
ip_address: "192.168.1.254"
netmask: "255.255.255.0"
r1_eth1:
ip_address: "192.168.3.1"
netmask: "255.255.255.0"
gateway: "192.168.3.254"
r2_eth0:
ip_address: "192.168.2.254"
netmask: "255.255.255.0"
r2_eth1:
ip_address: "192.168.3.254"
netmask: "255.255.255.0"
tasks:
# Configure H1 interface
- name: Configure H1 interface
template:
src: interfaces.j2
dest: /etc/network/interfaces.d/eth0.cfg
vars:
ifname: "eth0"
iface: "iface {{ ifname }} inet static"
address: "{{ h1_eth0.ip_address }}"
netmask: "{{ h1_eth0.netmask }}"
gateway: "{{ h1_eth0.gateway }}"
when: inventory_hostname == "H1"
# notify: restart networking
# Configure H2 interface
- name: Configure H2 interface
template:
src: interfaces.j2
dest: /etc/network/interfaces.d/eth0.cfg
vars:
ifname: "eth0"
iface: "iface {{ ifname }} inet static"
address: "{{ h2_eth0.ip_address }}"
netmask: "{{ h2_eth0.netmask }}"
gateway: "{{ h2_eth0.gateway }}"
when: inventory_hostname == "H2"
# notify: restart networking
# Configure R1 interfaces
- name: Configure R1 interface eth0
template:
src: interfaces.j2
dest: /etc/network/interfaces.d/eth0.cfg
vars:
ifname: "eth0"
iface: "iface {{ ifname }} inet static"
address: "{{ r1_eth0.ip_address }}"
netmask: "{{ r1_eth0.netmask }}"
when: inventory_hostname == "R1"
# notify: restart networking
- name: Configure R1 interface eth1
template:
src: interfaces.j2
dest: /etc/network/interfaces.d/eth1.cfg
vars:
ifname: "eth1"
iface: "iface {{ ifname }} inet static"
address: "{{ r1_eth1.ip_address }}"
netmask: "{{ r1_eth1.netmask }}"
gateway: "{{ r1_eth1.gateway }}"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment