summaryrefslogtreecommitdiff
path: root/ansible/roles/proxmox_vm/tasks/set_network.yaml
blob: c75aa0fd108725f0d3f516b8c1dce6368c0cdc54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
- name: "{{ fqdn }} : set up static ip address on windows"
  win_shell: |
    Start-Transcript -Path C:\set_domain_network_log.txt -Append
    Get-NetIpAddress -InterfaceAlias 'Ethernet' | Remove-NetIPAddress -Confirm:$false
    New-NetIPAddress -InterfaceAlias 'Ethernet' -IPAddress "{{ ip }}" -PrefixLength 24 -DefaultGateway "{{ gateway }}"
    Set-DnsClientServerAddress -InterfaceAlias 'Ethernet' -ServerAddresses "{{ dns }}"
    Get-NetConnectionProfile -InterfaceAlias 'Ethernet' | Set-NetConnectionProfile -NetworkCategory Private
    Stop-Transcript
  async: 15
  poll: 0
  delegate_to: "{{ vm_ip }}"
  when: os_type == 'windows'
  vars:
    ansible_connection: "{{ win_connector }}"
    ansible_user: "{{ default_win_username }}"
    ansible_password: "{{ default_win_password }}"
    ansible_port: "{{ win_port }}"
    ansible_winrm_transport: basic
    ansible_winrm_server_cert_validation: ignore

- name: "{{ fqdn }} : configure network for linux"
  block:
    - name: "{{ fqdn }} : get default interface"
      ansible.builtin.shell: ip route get 8.8.8.8 | sed -n 's/.*dev \([^\ ]*\).*/\1/p'
      register: interface_result

    - name: "{{ fqdn }} : set default interface variable"
      ansible.builtin.set_fact:
        linux_interface_name: "{{ interface_result.stdout }}"
        netplan_ip_address: "{{ ip }}"

    - name: "{{ fqdn }} : find netplan configs in /etc/netplan"
      ansible.builtin.find:
        paths: /etc/netplan
        recurse: yes
      register: yaml_configs

    - name: "{{ fqdn }} : remove all netplan configs in /etc/netplan"
      ansible.builtin.file:
        path: "{{ item.path }}"
        state: absent
      loop: "{{ yaml_configs.files }}"

    - name: "{{ fqdn }} : set netplan static ip address"
      ansible.builtin.template:
        src: static_ip_netplan.yaml.j2
        dest: /etc/netplan/01-netcfg.yaml
        mode: '0644'

    - name: "{{ fqdn }} : apply netplan configuration"
      ansible.builtin.command: netplan apply
      async: 15
      poll: 0
  delegate_to: "{{ vm_ip }}"
  when: os_type == 'linux'
  vars:
    ansible_connection: "{{ linux_connector }}"
    ansible_user: "{{ default_linux_username }}"
    ansible_password: "{{ default_linux_password }}"
    ansible_port: "{{ linux_port }}"
    ansible_host_key_checking: false

- name: "{{ fqdn }} : update ip to {{ ip }}"
  set_fact:
    ansible_host: "{{ ip }}"

- name: "{{ fqdn }} : pause execution for 3 minute to allow ip change and reconnect"
  pause:
    minutes: 3