Skip to content

Instantly share code, notes, and snippets.

@jan-warchol
Last active March 14, 2020 09:38
Show Gist options
  • Save jan-warchol/4dd3f19623ec07fd3571a1f096c78e58 to your computer and use it in GitHub Desktop.
Save jan-warchol/4dd3f19623ec07fd3571a1f096c78e58 to your computer and use it in GitHub Desktop.
Learning systemd and how to manage it with ansible
# Use with Vagrantfile liks this:
#
# # -*- mode: ruby -*-
# # vi: set ft=ruby :
#
# Vagrant.configure("2") do |config|
# config.vm.box = "ubuntu/xenial64"
# config.vm.provision "ansible" do |ans|
# ans.playbook = "ansible-systemd-test.yml"
# end
# end
- name: Bootstrap python if missing (required by Ansible to work)
hosts: all
gather_facts: False
become: yes
tags: sudo
tasks:
- name: install python2
raw: >
test -e /usr/bin/python ||
(apt-get -y update && apt-get install -y python-minimal)
- hosts: all
become: yes
tasks:
- name: install useful admin packages
apt:
name: "{{ item }}"
loop:
- tree
- name: template script
copy:
content: |
#!/bin/bash
while true; do
ls /vagrant
echo "-----------"
echo `date` >> /root/omg.log
sleep 10
done
dest: /root/spammer.sh
mode: 0755
- name: template service unit
copy:
content: |
[Unit]
Description = Spamming service
After = network.target
[Service]
WorkingDirectory = /vagrant
ExecStart = /root/spammer.sh
[Install]
WantedBy = multi-user.target
dest: /etc/systemd/system/foo.service
- name: template path unit
copy:
content: |
[Unit]
Description=Monitor a path for foo.service
[Path]
PathExists=/vagrant
[Install]
WantedBy=multi-user.target
dest: /etc/systemd/system/foo.path
- name: manage foo.service unit
systemd:
name: foo.service
state: started
daemon_reload: yes
enabled: no
- name: manage foo.path unit
systemd:
name: foo.path
state: started
daemon_reload: yes
enabled: yes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment