Skip to content

Instantly share code, notes, and snippets.

@JSONOrona
Created March 7, 2023 16:36
Show Gist options
  • Save JSONOrona/6a63c08b75fbeb1b15e945cbdc0fea44 to your computer and use it in GitHub Desktop.
Save JSONOrona/6a63c08b75fbeb1b15e945cbdc0fea44 to your computer and use it in GitHub Desktop.
Ruby Chef API example
require 'chef'
require 'chef/config'
require 'chef/server_api'
# Configure the Chef Infra Server API endpoint and credentials
Chef::Config.node_name = 'yourusername'
Chef::Config.client_key = '/path/to/your/client/key.pem'
Chef::Config.chef_server_url = 'https://yourchefserver.com/organizations/yourorg'
# Define the name and password of the user to create
username = 'newuser'
password = 'mypassword'
# Create a new Chef Infra Server user with the specified name and password
user = Chef::ServerAPI.new.create_user(username, password: password, admin: false)
puts "Created user '#{username}'"
# Delete the Chef Infra Server user with the specified name
Chef::ServerAPI.new.delete_user(username)
puts "Deleted user '#{username}'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment