---

- name: Make sure we have all required packages
  become_user: root
  package:
    name: "{{ item }}"
    state: present
  with_items:
    - curl
    - docker
    - expect
    - findutils
    - gcc
    - git
    - glib2-devel
    - glibc-devel
    - glibc-static
    - hostname
    - iproute
    - iptables
    - libxml2-devel
    - make
    - nfs-utils
    - nmap-ncat
    - openssl
    - openssl-devel
    - pkgconfig
    - python
    - python2-crypto
    - python-devel
    - python-virtualenv
    - PyYAML
    - rpcbind
    - rsync
    - sed
    - socat
    - tar
    - wget

- name: Update all packages
  package:
    name: '*'
    state: latest

- name: Setup swap to prevent kernel firing off the OOM killer
  shell: |
    truncate -s 8G /root/swap && \
    export SWAPDEV=$(losetup --show -f /root/swap | head -1) && \
    mkswap $SWAPDEV && \
    swapon $SWAPDEV && \
    swapon --show

- name: ensure directories exist as needed
  file:
    path: "{{ item }}"
    state: directory
  with_items:
    - /opt/cni/bin
    - /etc/cni/net.d

- name: set sysctl vm.overcommit_memory=1 for CentOS
  sysctl:
    name: vm.overcommit_memory
    state: present
    value: 1
  when: ansible_distribution == 'CentOS'

- name: inject hostname into /etc/hosts
  lineinfile:
    dest: /etc/hosts
    line: '{{ ansible_default_ipv4.address }} {{ ansible_nodename }}'
    insertafter: 'EOF'
    regexp: '{{ ansible_default_ipv4.address }}\s+{{ ansible_nodename }}'
    state: present

- name: start docker daemon
  systemd:
    state: started
    name: docker
