Ansible Switch Basics - Time Zone and NTP


The goal of this article is to provide a set of Ansible tasks to cover the basics of an initial switch setup. These tasks are typically run once when the switch is initially provisioned.
The assumption is you have Ansbile installed on a server that can reach the switch management network and that you have a basic understanding of Ansible. The examples below make use of only Ansible core modules so a default Ansible installation should work fine.
Details on Time Zone and NTP
Setting up the time zone and NTP service is very important to have consistent logs across your network. To set the time zone on a debian based system you can use "dpkg-reconfigure tzdata" which will prompt the user with an interaction menu to pick your location. For the purposes of automation use the noninteractive version to specifically set the time zone in /etc/timezone. For the NTP service in this example the default NTP servers are removed and a set of local servers are configured.
Ansible Tasks
  • Set the time zone in /etc/timezone
  • Use dpkg-reconfigure to set the system tzdata
  • Remove the default NTP servers
  • Configure new NTP servers
  • Restart the NTP service
#Setup the switch time zone (Note this will always report changed if re-run after the dpkg-reconfigure)
#For a list of time zones check: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
- name: Set time zone /etc/timezone
copy: content="America/New_York" dest=/etc/timezone
tags:
- time_setup
- initial_setup

#Update switch tzdata for the new time zone using dpkg-reconfigure
- name: Update tzdata with new time zone information
command: dpkg-reconfigure -f noninteractive tzdata
tags:
- time_setup
- initial_setup

#Remove all of the default Cumulus time servers from the ntp.conf file
- name: Setup NTP remove default NTP servers
lineinfile: dest=/etc/ntp.conf state=absent regexp="cumulusnetworks.pool.ntp.org"
tags:
- time_setup
- initial_setup

#Configure specific time servers by inserting your servers
#after the sample one in the conf file
- name: Setup NTP servers
lineinfile: dest=/etc/ntp.conf state=present insertafter="#server ntp.your-provider.example" line="server {{ item }}"
with_items:
- 192.168.100.253
- 192.168.100.254
tags:
- time_setup
- initial_setup

#Restart the NTP service to apply server changes
- name: Restart NTP service
service: name=ntp state=restarted
tags:
- time_setup
- initial_setup
For more information on installing Ansible and the Ansible modules used in these examples

0 replies

Be the first to reply!

Reply