Make NTP service configurable via IDF

Change-Id: Ice17d77cec3a6408305824c554fd93899db4dd8d
diff --git a/playbooks/roles/configure-targethosts/tasks/sync-time.yml b/playbooks/roles/configure-targethosts/tasks/sync-time.yml
index 5914b35..1c96d12 100644
--- a/playbooks/roles/configure-targethosts/tasks/sync-time.yml
+++ b/playbooks/roles/configure-targethosts/tasks/sync-time.yml
@@ -16,24 +16,44 @@
 #
 # SPDX-License-Identifier: Apache-2.0
 # ============LICENSE_END=========================================================
+- name: Add the OS specific variables
+  include_vars: '{{ ansible_os_family }}.yml'
 
-- name: Restart chrony service
+- name: Remove NTP if installed
+  action: >
+    {{ ansible_pkg_mgr }} name={{ item }} state=absent update_cache=yes
+  with_items:
+    - "ntp"
+
+- name: Install Chrony
+  action: >
+    {{ ansible_pkg_mgr }} name={{ item }} state=present update_cache=yes
+  with_items:
+    - "chrony"
+
+- name: Copy the chrony.conf template file
+  template:
+    src: "chrony.conf.j2"
+    dest: "{{ chrony_config_location }}"
+    owner: "root"
+    group: "root"
+    mode: "0755"
+  become: true
+  when: idf.ntp_servers is defined
+
+- name: Restart chrony
   service:
-    name: "{{ (ansible_pkg_mgr == 'apt') | ternary('chrony', 'chronyd') }}"
-    state: restarted
+    name: "{{ chrony_service_name }}"
+    state: "restarted"
+  become: true
 
-- name: Synchronize time
-  shell: "chronyc -a 'burst 4/4' && chronyc -a makestep"
-  args:
-    executable: /bin/bash
-  changed_when: True
-  register: chrony_got_time
-  until: chrony_got_time.rc == 0
-  retries: 5
-  delay: 5
-  environment:
-    http_proxy: "{{ lookup('env','http_proxy') }}"
-    https_proxy: "{{ lookup('env','https_proxy') }}"
-    no_proxy: "{{ lookup('env','no_proxy') }}"
+
+- name: "NTP should be synchronized"
+  shell: timedatectl  | grep 'synchronized'
+  register: synchronized
+  changed_when: false
+  until: "'yes' in synchronized.stdout"
+  retries: 300
+  delay: 2
 
 # vim: set ts=2 sw=2 expandtab:
diff --git a/playbooks/roles/configure-targethosts/templates/chrony.conf.j2 b/playbooks/roles/configure-targethosts/templates/chrony.conf.j2
new file mode 100644
index 0000000..de6b840
--- /dev/null
+++ b/playbooks/roles/configure-targethosts/templates/chrony.conf.j2
@@ -0,0 +1,41 @@
+# List of NTP servers to use
+
+{% for item in idf.ntp_servers %}
+{%   if item['options'] is not defined %}
+server {{ item['server'] }}
+{%   elif item['options'] is defined %}
+server {{ item['server'] }}{% for opt in item['options'] %} {{ opt['option'] }}{% if opt['val'] is defined %} {{ opt['val'] }}{% endif %}{% endfor %}
+
+{%   endif %}
+
+{% endfor %}
+
+# Ignore stratum in source selection.
+stratumweight 0
+
+# Specify the key used as password for chronyc.
+commandkey 1
+
+# Record the rate at which the system clock gains/losses time.
+driftfile {{ chrony_config_driftfile }}
+
+# In first three updates step the system clock instead of slew
+# if the adjustment is larger than 10 seconds.
+makestep 10 3
+
+# Enable kernel RTC synchronization.
+rtcsync
+
+# Listen for commands only on localhost.
+bindcmdaddress 127.0.0.1
+bindcmdaddress ::1
+
+keyfile {{ chrony_config_keyfile }}
+
+# Generate command key if missing.
+generatecommandkey
+
+logchange 0.5
+
+logdir {{ chrony_config_logdir }}
+
diff --git a/playbooks/roles/configure-targethosts/vars/Debian.yml b/playbooks/roles/configure-targethosts/vars/Debian.yml
new file mode 100644
index 0000000..fa38673
--- /dev/null
+++ b/playbooks/roles/configure-targethosts/vars/Debian.yml
@@ -0,0 +1,25 @@
+---
+# ============LICENSE_START=======================================================
+#  Copyright (C) 2019 The Nordix Foundation. All rights reserved.
+# ================================================================================
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# SPDX-License-Identifier: Apache-2.0
+# ============LICENSE_END=========================================================
+chrony_service_name: chrony
+chrony_config_location: /etc/chrony/chrony.conf
+chrony_config_driftfile: /var/lib/chrony/chrony.drift
+chrony_config_keyfile: /etc/chrony/chrony.keys
+chrony_config_logdir: /var/log/chrony
+
+# vim: set ts=2 sw=2 expandtab:
diff --git a/playbooks/roles/configure-targethosts/vars/RedHat.yml b/playbooks/roles/configure-targethosts/vars/RedHat.yml
new file mode 100644
index 0000000..426900b
--- /dev/null
+++ b/playbooks/roles/configure-targethosts/vars/RedHat.yml
@@ -0,0 +1,25 @@
+---
+# ============LICENSE_START=======================================================
+#  Copyright (C) 2019 The Nordix Foundation. All rights reserved.
+# ================================================================================
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# SPDX-License-Identifier: Apache-2.0
+# ============LICENSE_END=========================================================
+chrony_service_name: chronyd
+chrony_config_location: /etc/chrony.conf
+chrony_config_driftfile: /var/lib/chrony/drift
+chrony_config_keyfile: /etc/chrony.keys
+chrony_config_logdir: /var/log/chrony
+
+# vim: set ts=2 sw=2 expandtab:
diff --git a/playbooks/roles/configure-targethosts/vars/Suse.yml b/playbooks/roles/configure-targethosts/vars/Suse.yml
new file mode 100644
index 0000000..426900b
--- /dev/null
+++ b/playbooks/roles/configure-targethosts/vars/Suse.yml
@@ -0,0 +1,25 @@
+---
+# ============LICENSE_START=======================================================
+#  Copyright (C) 2019 The Nordix Foundation. All rights reserved.
+# ================================================================================
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# SPDX-License-Identifier: Apache-2.0
+# ============LICENSE_END=========================================================
+chrony_service_name: chronyd
+chrony_config_location: /etc/chrony.conf
+chrony_config_driftfile: /var/lib/chrony/drift
+chrony_config_keyfile: /etc/chrony.keys
+chrony_config_logdir: /var/log/chrony
+
+# vim: set ts=2 sw=2 expandtab: