[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/README.md b/README.md
index 6293c3b..d13f181 100644
--- a/README.md
+++ b/README.md
@@ -10,24 +10,60 @@
 
 Minimum ansible version: 2.9.5
 
+## What this role does
+
+This runs a Jenkins server, which can be configured to
+
+1. Disable the default installation wizard.
+2. Create an account for administrator.
+3. Set up the proxy configuration if Jenkins runs behind the reverse proxy.
+
+## What this role doesn't do
+
+This role only handles the installation of Jenkins process and does not handle
+
+1. Plugin setting
+2. Jenkins job definition
+
+
 ## Defaults
 
-List of default values for variables:
-* `example_default1: example_value1`
-* `example_default2: example_value2`
+# Username and password for administrator, they only works for the first time installation.
+jenkins_admin_username: "admin"
+jenkins_admin_password: "chang_me"
+
+# JAVA OPTIONS for Jenkins process, the default one will disable the setup wizard.
+jenkins_java_options: "-Djenkins.install.runSetupWizard=false"
+
+# Jenkins proxy setting, used for downloading the plugin if your Jenkins can't access outside.
+jenkins_proxy_host: ""
+jenkins_proxy_port: ""
+jenkins_proxy_noproxy:
+  - "127.0.0.1"
+  - "localhost"
 
 ## Example Playbook
 
 ```yaml
 - hosts: all
   vars:
-    example_default1: example_value1
-    example_default2: example_value2
+    jenkins_admin_username: "admin"
+    jenkins_admin_password: "change_me"
+    jenkins_proxy_host: "https://site.example.org"
+    jenkins_proxy_port: "443"
   roles:
     - jenkins
 
 ```
 
+## Testing
+
+There are many tests in the `molecule/` directory, covering the  HTTP health check
+and Jenkins Job Builder accessibility with administrator account.
+
+If you want to add functionality to this role, please write tests to cover that
+functionality.
+
 ## License and Author
 
 © 2020 Open Networking Foundation <support@opennetworking.org>
diff --git a/defaults/main.yml b/defaults/main.yml
index 2e82f71..7d5a9a8 100644
--- a/defaults/main.yml
+++ b/defaults/main.yml
@@ -7,15 +7,22 @@
 jenkins_connection_delay: 5
 jenkins_connection_retries: 60
 
+jenkins_localhost: "127.0.0.1:8080"
 jenkins_home: /var/lib/jenkins
 jenkins_process_user: jenkins
 jenkins_process_group: jenkins
 
-jenkins_url_prefix: ""
 jenkins_java_options: "-Djenkins.install.runSetupWizard=false"
 
 jenkins_init_changes:
-  - option: "JENKINS_ARGS"
-    value: "--prefix={{ jenkins_url_prefix }}"
   - option: "{{ jenkins_java_options_env_var }}"
     value: "{{ jenkins_java_options }}"
+
+jenkins_proxy_host: ""
+jenkins_proxy_port: ""
+jenkins_proxy_noproxy:
+  - "127.0.0.1"
+  - "localhost"
+
+jenkins_admin_username: "admin"
+jenkins_admin_password: "changeme"
diff --git a/handlers/main.yml b/handlers/main.yml
index f9c19a7..17944b8 100644
--- a/handlers/main.yml
+++ b/handlers/main.yml
@@ -4,6 +4,16 @@
 # SPDX-FileCopyrightText: © 2020 Open Networking Foundation <support@opennetworking.org>
 # SPDX-License-Identifier: Apache-2.0
 
+- name: unmask-jenkins
+  systemd:
+    name: "{{ jenkins_service }}"
+    masked: false
+
+- name: enable-jenkins
+  systemd:
+    name: "{{ jenkins_service }}"
+    enabled: true
+
 - name: start-jenkins
   systemd:
     name: jenkins
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
diff --git a/vars/Debian.yml b/vars/Debian.yml
index 0b97a23..8b90e4b 100644
--- a/vars/Debian.yml
+++ b/vars/Debian.yml
@@ -14,11 +14,5 @@
 # name of the Jenkins service
 jenkins_service: "jenkins"
 
-jenkins_proxy_host: ""
-jenkins_proxy_port: ""
-
-jenkins_admin_username: "admin"
-jenkins_admin_password: "changeme"
-
 jenkins_init_file: /etc/default/jenkins
 jenkins_java_options_env_var: JAVA_ARGS