Skip to content

Instantly share code, notes, and snippets.

@gridhead
Last active May 15, 2021 13:05
Show Gist options
  • Save gridhead/b97d284c7afc9a785c0dd61d7715ca46 to your computer and use it in GitHub Desktop.
Save gridhead/b97d284c7afc9a785c0dd61d7715ca46 to your computer and use it in GitHub Desktop.
Using Templating Engines for Generating Ansible Playbooks
- name: Enabling the {{ governor }} CPU Governor on System Boot
hosts: server2
remote_user: soumadeepdhar
become: yes
become_method: sudo
become_user: root
tasks:
- name: "STEP 1 - Creating a shellscript to enable {{ governor }} governor"
copy:
dest: "/usr/local/bin/perf-prof.sh"
content: |
#!/bin/bash
echo {{ governor }} | tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
- name: "STEP 2 - Changing permissions for the shellscript file"
file:
path: "/usr/local/bin/perf-prof.sh"
mode: 0744
- name: "STEP 3 - Creating a systemd service to start on system boot"
copy:
dest: "/etc/systemd/system/perf-prof.service"
content: |
[Unit]
After=network.target
[Service]
ExecStart=/usr/local/bin/perf-prof.sh
[Install]
WantedBy=default.target
- name: "STEP 4 - Changing permissions for the systemd service"
file:
path: "/etc/systemd/system/perf-prof.service"
mode: 0664
- name: "STEP 5 - Reloading the daemon to prep new systemd services"
ansible.builtin.systemd:
daemon_reload: yes
- name: "STEP 6 - Enable the created systemd service to start on system boot"
ansible.builtin.systemd:
name: perf-prof.service
enabled: yes
- name: "STEP 7 - Start the recently enabled systemd service right now"
ansible.builtin.systemd:
name: perf-prof.service
state: started

Using Templating Engines for Generating Ansible Playbooks

  1. Download the tempperf.py and cpugovnr.tyml to the same directory.
  2. Run the following command to view the usage.
python3 tempperf.py
[ i ] CPU Governor Systemd Config
      Usage : python3 tempperf.py <tempfile> <governor> <destfile>
  1. Now run the following command to swiftly create a playbook for setting powersave CPU governor on boot.
python3 tempperf.py cpugovnr.tyml powersave pwsvprof.yml
[ i ] CPU Governor Systemd Config
[ i ] Playbook written in 'pwsvprof.yml'
  1. Make sure to change the attributes inside of the pwsvprof.yml file according to your liking, and then run the playbook using the following command.
ansible-playbook -i inventory.ini pwsvprof.yml
- name: Enabling the powersave CPU Governor on System Boot
hosts: server2
remote_user: soumadeepdhar
become: yes
become_method: sudo
become_user: root
tasks:
- name: "STEP 1 - Creating a shellscript to enable performance governor"
copy:
dest: "/usr/local/bin/perf-prof.sh"
content: |
#!/bin/bash
echo powersave | tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
- name: "STEP 2 - Changing permissions for the shellscript file"
file:
path: "/usr/local/bin/perf-prof.sh"
mode: 0744
- name: "STEP 3 - Creating a systemd service to start on system boot"
copy:
dest: "/etc/systemd/system/perf-prof.service"
content: |
[Unit]
After=network.target
[Service]
ExecStart=/usr/local/bin/perf-prof.sh
[Install]
WantedBy=default.target
- name: "STEP 4 - Changing permissions for the systemd service"
file:
path: "/etc/systemd/system/perf-prof.service"
mode: 0664
- name: "STEP 5 - Reloading the daemon to prep new systemd services"
ansible.builtin.systemd:
daemon_reload: yes
- name: "STEP 6 - Enable the created systemd service to start on system boot"
ansible.builtin.systemd:
name: perf-prof.service
enabled: yes
- name: "STEP 7 - Start the recently enabled systemd service right now"
ansible.builtin.systemd:
name: perf-prof.service
state: started
import sys
from jinja2 import Template
def read_template(tempfile:str):
tempdata = None
try:
with open("cpugovnr.tyml", "r") as tempobjc:
tempdata = tempobjc.read()
except Exception as expt:
print("[ ! ] File could not be read due to " + str(expt))
return tempdata
def inherit_governor_to_template(tempdata:str, governor:str):
appldata = None
try:
template = Template(tempdata)
appldata = template.render(governor = governor)
except Exception as expt:
print("[ ! ] Template could not be applied due to " + str(expt))
return appldata
def write_applied_config_to_file(appldata:str, destfile:str):
otptrslt = -1
try:
with open(destfile, "w") as otptobjc:
otptobjc.write(appldata)
otptrslt = 0
except Exception as expt:
print("[ ! ] File could not be written due to " + str(expt))
return otptrslt
def main_function(tempfile:str, governor:str, destfile:str):
tempdata = read_template(tempfile)
if tempdata == None:
sys.exit(0)
else:
appldata = inherit_governor_to_template(tempdata, governor)
if appldata == None:
sys.exit(0)
else:
otptrslt = write_applied_config_to_file(appldata, destfile)
if otptrslt == 0:
print("[ i ] Playbook written in '" + destfile + "'")
sys.exit(0)
if __name__ == "__main__":
print("[ i ] CPU Governor Systemd Config")
if len(sys.argv) == 4:
main_function(sys.argv[1], sys.argv[2], sys.argv[3])
else:
print(" Usage : python3 tempperf.py <tempfile> <governor> <destfile>")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment