---
# tasks/main.yml
# define tasks here

# install packages
- name: Ensure dependencies are installed.
  apt: pkg=curl state=installed

- name: Add Jenkins apt repository key.
  apt_key:
    url: "{{ jenkins_repo_key_url }}"
    state: present

- name: Add Jenkins apt repository.
  apt_repository:
    repo: "{{ jenkins_repo_url }}"
    state: present
    update_cache: yes

- name: Ensure Jenkins is installed.
  apt: pkg=jenkins state=installed

# start jenkins
- name: Ensure Jenkins is started and runs on startup.
  service: name=jenkins state=started enabled=yes

- name: Wait for Jenkins to start up.
  shell: curl --head --silent http://{{ jenkins_hostname }}:8080/cli/
  register: result
  until: result.stdout.find("200 OK") != -1
  retries: "{{ jenkins_connection_retries }}"
  delay: "{{ jenkins_connection_delay }}"
  changed_when: false

# install cli
- name: Get the jenkins-cli jarfile from the Jenkins server.
  get_url:
    url: "http://{{ jenkins_hostname }}:8080/jnlpJars/jenkins-cli.jar"
    dest: "{{ jenkins_jar_location }}"
  register: jarfile_get
  until: "'OK' in jarfile_get.msg or 'file already exists' in jarfile_get.msg"
  retries: 5
  delay: 10

# - name: Copy ssh key for authentication
  # copy: src={ HOME }/.ssh/jenkins.pub dest="/var/lib/jenkins/.ssh/"

# configure jenkins
# - name: Update jenkins config
  # lineinfile: dest=/var/lib/jenkins/config.xml regexp={{ item.reg }} line={{ item.line }}
  # with_items:
    # - {"reg": "<useSecurity>.*</useSecurity>", "line": "<useSecurity>false</useSecurity>"}

# setup .gitconfig
- name: Setup .gitconfig for jenkins user
  template: dest="{{ jenkins_home }}/.gitconfig" src="gitconfig.j2" owner="jenkins" group="jenkins"

# update and install plugins
- name: Create Jenkins updates folder.
  file:
    path: /var/lib/jenkins/updates
    owner: jenkins
    group: jenkins
    mode: 0755
    state: directory

- name: Update Jenkins plugin data.
  shell: >
    curl -L https://updates.jenkins-ci.org/update-center.json | sed '1d;$d' > /var/lib/jenkins/updates/default.json
    creates=/var/lib/jenkins/updates/default.json

- name: Permissions for default.json updates info.
  file:
    path: /var/lib/jenkins/updates/default.json
    owner: jenkins
    group: jenkins
    mode: 0755

- name: Install Jenkins plugins.
  command: >
    java -jar {{ jenkins_jar_location }} -s http://{{ jenkins_hostname }}:8080/ install-plugin {{ item }}
    creates=/var/lib/jenkins/plugins/{{ item }}.jpi
  with_items: jenkins_plugins
  notify: restart jenkins

- name: setup folder for jenkins slave on localhost
  file: state=directory path=/var/lib/jenkins-slave owner=jenkins group=jenkins mode=0777

# install nginx as proxy
- name: Copy the nginx configuration file
  template: src=nginx.jenkins.conf.j2 dest=/etc/nginx/sites-available/jenkins.conf

- name: Enable Jenkins Nginx configuration
  file: src=/etc/nginx/sites-available/jenkins.conf dest=/etc/nginx/sites-enabled/jenkins.conf state=link
  notify:
   - reload nginx
