---
# file: roles/head-prep/tasks/main.yml

- name:  Install prerequisites for using PPA repos
  apt:
    name={{ item }}
    update_cache=yes
    cache_valid_time=3600
  with_items:
    - python-pycurl
    - software-properties-common

- name: Add Ansible/Juju repositories
  apt_repository:
    repo={{ item }}
  with_items:
     - "{{ juju_apt_repo | default('ppa:juju/stable') }}"
     - "{{ ansible_apt_repo | default('ppa:ansible/ansible') }}"

- name: Install packages
  apt:
    name={{ item }}
    state=present
    update_cache=yes
    cache_valid_time=3600
  with_items:
    - uvtool
    - git
    - bzr
    - juju-core
    - python-pip
    - python-novaclient
    - python-neutronclient
    - python-keystoneclient
    - python-glanceclient
    - python-lxml
    - virt-top

- name: Make sure Ansible is newest version
  apt:
    name: "ansible"
    state: latest
    update_cache: yes
    cache_valid_time: 3600
  tags:
    - skip_ansible_lint # ansible-lint complains about latest, need this or old built in 1.5.x version may be used if already installed.

- name: Install Python packages
  pip:
    name={{ item}}
    state=present
  with_items:
    - urllib3
    - pyopenssl
    - ndg-httpsclient
    - pyasn1

- name: Prep user account by adding to libvirtd group and generating SSH key
  user:
    name={{ ansible_user_id }}
    generate_ssh_key=yes
    groups="libvirtd" append=yes

- name: Register public key in variable
  shell: cat {{ ansible_user_dir }}/.ssh/id_rsa.pub
  register: sshkey
  tags:
    - skip_ansible_lint # FIXME: this should be done a different way

- name: Add public key to this user account
  authorized_key:
    user={{ ansible_user_id }}
    key="{{ sshkey.stdout }}"

- name: Disable host key checking in ~/.ssh/config
  lineinfile:
    dest={{ ansible_user_dir }}/.ssh/config
    line="StrictHostKeyChecking no"
    create=yes
    owner={{ ansible_user_id }} mode=0600

- name: Disable host key checking in ~/.ansible.cfg
  copy:
    src=ansible.cfg
    dest={{ ansible_user_dir }}/.ansible.cfg
    owner={{ ansible_user_id }} mode=0644

- name: Copy node key
  when: not on_maas
  copy:
    src={{ ansible_user_dir }}/.ssh/id_rsa
    dest={{ ansible_user_dir }}/node_key
    owner={{ ansible_user }}
    mode=0600
    remote_src=True

- name: Copy node key (MaaS)
  when: on_maas
  copy:
    src={{ maas_node_key }}
    dest={{ ansible_user_dir }}/node_key
    owner={{ ansible_user }}
    mode=0600
    remote_src=True

