---
- name: Jenkins Master Setup Playbook
  hosts: Jenkins
  become: yes
  remote_user: root
  gather_facts: no

  vars_files:
    - vars/common_vars.yml

  pre_tasks:
    - name: install python2, ansible requires it
      raw: test -e /usr/bin/python || (apt -y update && apt install -y python-minimal)
      register: output
      changed_when: output.stdout != ""
      tags: always

    - name: gather facts
      setup:

    - name: install common dependencies
      include: tasks/common-install.yml

  roles:
    - ansible-role-jenkins

  tasks:
    - name: install packages needed by the jenkins master
      include: tasks/jenkins/jenkins-master-package-install.yml

    - name: install Jenkins Job Builder
      pip:
        name: jenkins-job-builder
        state: latest

    - include: tasks/jenkins/jenkins-jobs-ini-install.yml

    - name: add the jobs to jenkins
      command: jenkins-jobs update --delete-old /vagrant/ansible/jenkins-jobs

    - name: "restart Jenkins"
      service: name=jenkins state=restarted

---
- name: Jenkins Master Setup Playbook
  hosts: Jenkins
  become: yes
  remote_user: root
  gather_facts: no

  vars_files:
    - vars/common_vars.yml

  pre_tasks:
    - name: install python2, ansible requires it
      raw: test -e /usr/bin/python || (apt -y update && apt install -y python-minimal)
      register: output
      changed_when: output.stdout != ""
      tags: always

    - name: gather facts
      setup:

    - name: install common dependencies
      include: tasks/common-install.yml

  roles:
    - ansible-role-jenkins

  tasks:
    - name: install packages needed by the jenkins master
      include: tasks/jenkins/jenkins-master-package-install.yml

    - name: install Jenkins Job Builder
      pip:
        name: jenkins-job-builder
        state: latest

    - include: tasks/jenkins/jenkins-jobs-ini-install.yml

    - name: "restart Jenkins"
      service: name=jenkins state=restarted

    - name: Wait for Jenkins to start up
      shell: "curl -D - --silent --max-time 5 http://{{ jenkins_hostname }}:{{ jenkins_http_port }}{{ jenkins_url_prefix }}/cli/"
      register: result
      until: (result.stdout.find("403 Forbidden") != -1) or (result.stdout.find("200 OK") != -1) and (result.stdout.find("Please wait while") == -1)
      retries: "{{ jenkins_connection_retries }}"
      delay: "{{ jenkins_connection_delay }}"
      changed_when: false
      check_mode: no

    - name: add the jobs to jenkins
      command: jenkins-jobs update --delete-old /vagrant/ansible/jenkins-jobs


Make initial Jenkins provision more reliable
Make initial Jenkins provision more reliable

By restarting Jenkins and waiting for it to load before adding
the jobs. This should also ensure the plugins are loaded properly.
