---
- name: Add cloud-init config file
  template:
    src: cloud.cfg.j2
    dest: /etc/cloud/cloud.cfg

- name: Don't requiretty for sudo
  lineinfile:
    path: /etc/sudoers
    regexp: '^Defaults\t!requiretty$'
    insertafter: '^Defaults\s+env_reset$'
    line: 'Defaults !requiretty'
    validate: 'visudo -cf %s'

- name: Get cloud-init version
  shell: "cloud-init --version 2>&1 | awk '{print $NF}'"
  register: cloudinit_version

- block:
  - name: Find cloud-init DHCP file
    shell: 'ls /usr/lib/python*/*-packages/cloudinit/net/dhcp.py'
    register: cloudinit_dhcp_file

  # Fix for https://bugs.launchpad.net/bugs/1821102 until 19.2 is released.
  - name: Ephemeral DHCP fix
    lineinfile:
      path: "{{ cloudinit_dhcp_file.stdout }}"
      insertafter: '^    def __enter__\(self\):'
      line: '        raise NoDHCPLeaseError()  # Nectar disabling Ephemeral'
      firstmatch: yes
      state: present
      backup: yes
  when:
    - cloudinit_version.stdout is version_compare('18.2', '>')
    - cloudinit_version.stdout is version_compare('19.2', '<')

---
- name: Add cloud-init config file
  template:
    src: cloud.cfg.j2
    dest: /etc/cloud/cloud.cfg

- name: Don't requiretty for sudo
  lineinfile:
    path: /etc/sudoers
    regexp: '^Defaults\t!requiretty$'
    insertafter: '^Defaults\s+env_reset$'
    line: 'Defaults !requiretty'
    validate: 'visudo -cf %s'

- name: Get cloud-init version
  shell: "cloud-init --version 2>&1 | awk '{print $NF}'"
  register: cloudinit_version

- debug:
    msg: "cloud-init version is {{ cloudinit_version.stdout }}"

- block:
  - name: Find cloud-init DHCP file
    shell: 'ls /usr/lib/python*/*-packages/cloudinit/net/dhcp.py'
    register: cloudinit_dhcp_file

  # Fix for https://bugs.launchpad.net/bugs/1821102 until 19.2 is released.
  - name: Ephemeral DHCP fix
    lineinfile:
      path: "{{ cloudinit_dhcp_file.stdout }}"
      insertafter: '^    def __enter__\(self\):'
      line: '        raise NoDHCPLeaseError()  # Nectar disabling Ephemeral'
      firstmatch: yes
      state: present
      backup: yes
  when:
    - cloudinit_version.stdout is version_compare('18.2', '>')
    - cloudinit_version.stdout is version_compare('19.2', '<')

Print cloud-init version during build
Print cloud-init version during build

Change-Id: I7c9b68def4f4eea0198d584e83b799c8cfef3b0b
