Skip to content

Instantly share code, notes, and snippets.

@irvingpop
Created August 6, 2017 17:25
Show Gist options
  • Save irvingpop/801593c2a500f71f444c3617cb3eaff1 to your computer and use it in GitHub Desktop.
Save irvingpop/801593c2a500f71f444c3617cb3eaff1 to your computer and use it in GitHub Desktop.
Bit of systemd chef code to have clients automatically remove themselves on shutdown
# Create a systemd service that runs at shutdown to remove one's node and client from the chef server,
# useful in AutoScale and Terraform-managed scenarios
# not useful in cases where you may want to shutdown (but not terminate) a node
systemd_unit 'chef-cleanup.service' do
content('Unit' => {
'Description' => 'Delete my own Chef node and client objects at shutdown/termination',
'DefaultDependencies' => 'no',
'Before' => 'shutdown.target halt.target',
},
'Service' => {
'Type' => 'oneshot',
'ExecStart' => "/opt/chef/bin/knife node delete #{node.name} -y -c /etc/chef/client.rb && /opt/chef/bin/knife client delete #{node.name} -y -c /etc/chef/client.rb",
},
'Install' => {
'WantedBy' => 'halt.target shutdown.target',
})
action [:create, :enable]
end
@obazoud
Copy link

obazoud commented Sep 5, 2018

I try this code with Ubuntu 18.04 LTS on AWS, and my node is not unregister.
Do you have an idea ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment