---

- name: "Enable static ip for {{ cobbler_internal_network_nic }}"
  blockinfile:
    dest: /etc/network/interfaces
    content: |
      auto {{ cobbler_internal_network_nic }}
      iface {{ cobbler_internal_network_nic }} inet static
            address {{ cobbler_dhcp_router_ip }}
            netmask {{ cobbler_dhcp_subnet_mask }}
            broadcast {{ cobbler_dhcp_broadcast_ip }}
            dns-domain {{ cobbler_dhcp_domain_name }}
            dns-search {{ cobbler_dhcp_domain_name }}

- name: Enable ip forwarding in both /proc and sysctl file
  sysctl:
    name: "{{ item }}"
    value: 1
    sysctl_set: yes
    state: present
    reload: yes
  with_items:
    - "net.ipv4.conf.default.forwarding"
    - "net.ipv4.conf.all.forwarding"
    - "net.ipv4.ip_forward"

- name: "Enable NAT MASQUERADE on {{ cobbler_external_network_nic }}"
  iptables:
    table: nat
    chain: POSTROUTING
    out_interface: "{{ cobbler_external_network_nic }}"
    jump: MASQUERADE

- name: "Forward the traffic from {{ cobbler_external_network_nic }} to {{ cobbler_internal_network_nic }}"
  iptables:
    chain: FORWARD
    in_interface: "{{ cobbler_external_network_nic }}"
    out_interface: "{{ cobbler_internal_network_nic }}"
    ctstate: RELATED,ESTABLISHED
    jump: ACCEPT

- name: "Forward the traffic from {{ cobbler_internal_network_nic }} to {{ cobbler_external_network_nic }}"
  iptables:
    chain: FORWARD
    in_interface: "{{ cobbler_internal_network_nic }}"
    out_interface: "{{ cobbler_external_network_nic }}"
    jump: ACCEPT
