[AETHER-852] - Mask the service to enusre Jenkins will load the latest config.

- Only generate the init groovy script for first-time installation
- Remove init groovy script after first-time instllation.
- Moved the variable location

Change-Id: I0c0fb01f21353c817e2de986c86903df0387b9e4
diff --git a/tasks/Debian.yml b/tasks/Debian.yml
index 85ca137..4ca519d 100644
--- a/tasks/Debian.yml
+++ b/tasks/Debian.yml
@@ -24,14 +24,18 @@
     repo: "{{ jenkins_repo_url }}"
     update_cache: true
 
+# We need to prepare both init groovy script and Jenkins setting before starting it.
+- name: Mask the Jenkins to avoid it starting after first-time installation
+  systemd:
+    name: "{{ jenkins_service }}"
+    masked: true
+  when: "'jenkins' not in ansible_facts.packages"
+
 - name: Install Jenkins packages (Debian)
   apt:
     name: "jenkins"
     state: "present"
     update_cache: true
     cache_valid_time: 3600
-
-- name: Enable Jenkins Service
-  service:
-    name: "{{ jenkins_service }}"
-    enabled: true
+  notify:
+    - start-jenkins
diff --git a/tasks/main.yml b/tasks/main.yml
index 32b6c1b..1860396 100644
--- a/tasks/main.yml
+++ b/tasks/main.yml
@@ -7,18 +7,35 @@
 - name: include OS-specific vars
   include_vars: "{{ ansible_os_family }}.yml"
 
+- name: Gather the package facts
+  package_facts:
+    manager: auto
+
 - name: include OS-specific tasks
   include_tasks: "{{ ansible_os_family }}.yml"
 
 - name: Initial the Jenkins
   include_tasks: "settings.yml"
 
-- name: generate groovy for initializing local admin account
-  template:
-    src: init_admin.groovy.j2
-    dest: "{{ jenkins_home }}/init.groovy.d/basic-security.groovy"
-    owner: "{{ jenkins_process_user }}"
-    group: "{{ jenkins_process_group }}"
-    mode: 0775
-  notify:
-    - start-jenkins
+- name: Trigger handlers immediately in case Jenkins was installed
+  meta: flush_handlers
+
+- name: Wait for Jenkins to start up before proceeding.
+  uri:
+    url: "http://{{ jenkins_localhost }}/cli/"
+    method: GET
+    return_content: "yes"
+    timeout: 5
+    body_format: raw
+    follow_redirects: "no"
+    status_code: 200,403
+  register: result
+  until: (result.status == 403 or result.status == 200)
+      and (result.content.find("Please wait while") == -1)
+  retries: 60
+  delay: 5
+
+- name: Remove Jenkins security init scripts after first startup.
+  file:
+    path: "{{ jenkins_home }}/init.groovy.d/basic-security.groovy"
+    state: absent
diff --git a/tasks/settings.yml b/tasks/settings.yml
index 707e128..a776bae 100644
--- a/tasks/settings.yml
+++ b/tasks/settings.yml
@@ -29,3 +29,16 @@
     owner: "{{ jenkins_process_user }}"
     group: "{{ jenkins_process_group }}"
     mode: 0775
+
+- name: generate groovy for initializing local admin account
+  template:
+    src: init_admin.groovy.j2
+    dest: "{{ jenkins_home }}/init.groovy.d/basic-security.groovy"
+    owner: "{{ jenkins_process_user }}"
+    group: "{{ jenkins_process_group }}"
+    mode: 0775
+  when: "'jenkins' not in ansible_facts.packages"
+  notify:
+    - unmask-jenkins
+    - enable-jenkins
+    - start-jenkins