---
- block:
  - name: Install the common deb packages
    apt: pkg={{ jenkins_common_deb_package_requirements }} state={{ jenkins_pkg_state }} update_cache=yes cache_valid_time=3600

  when: ansible_distribution_file_variety == "Debian"
  tags: [ 'jenkins', 'jenkins_common', 'jenkins_master', 'jenkins_slave', 'jenkins_slaves', 'jenkins_pkgs' ]

- block:
  - name: Install the common RH/CentOS packages
    yum: pkg={{ jenkins_common_rh_package_requirements }} state={{ jenkins_pkg_state }}

  when: ansible_distribution_file_variety != "Debian"
  tags: [ 'jenkins', 'jenkins_common', 'jenkins_master', 'jenkins_slave', 'jenkins_slaves', 'jenkins_pkgs' ]

- block:
  - name: Create the ssh key on the master node
    user: name={{ jenkins_username }} generate_ssh_key=True 
    delegate_to: '{{ item }}'
    with_items: '{{ groups.jenkins_master }}'

  - name: Get the master ssh keys
    become: True
    become_user: '{{ jenkins_username }}'
    shell: cat ~/.ssh/id_rsa.pub
    register: jenkins_pubkeys

  - name: Deploy the public ssh key on the slaves
    authorized_key: user={{ jenkins_username }} key={{ item[0] }}
    delegate_to: '{{ item[1] }}'
    with_nested:
      - '{{ jenkins_pubkeys.stdout }}'
      - "{{ groups['jenkins_slaves'] }}"

  when: jenkins_slaves_via_ssh
  tags: [ 'jenkins', 'jenkins_common', 'jenkins_master', 'jenkins_slave', 'jenkins_slaves' ]

- block:
    - name: Create the maven setting directory
      file: dest={{ jenkins_dest }}/{{ item }} state=directory
      with_items: '{{ jenkins_maven_settings_dirs }}'

    - name: Fetch the maven settings template file
      become: False
      become_user: root
      run_once: True
      get_url: url={{ jenkins_maven_settings_url }} dest=/tmp/settings.xml.j2 force=yes
      delegate_to: localhost

    - name: Install the maven settings
      template: src=/tmp/settings.xml.j2 dest={{ jenkins_dest }}/.m2/settings.xml

  become: True
  become_user: '{{ jenkins_username }}'
  when: jenkins_maven_config
  tags: [ 'jenkins', 'jenkins_common', 'jenkins_master', 'jenkins_slave', 'jenkins_maven_config' ]