Skip to content

Instantly share code, notes, and snippets.

@mriddle
Last active July 20, 2019 19:09
Show Gist options
  • Save mriddle/4041580 to your computer and use it in GitHub Desktop.
Save mriddle/4041580 to your computer and use it in GitHub Desktop.
Using knife

Using Knife

Basic configuration

How to knife the chef

  • Uploading cookbooks
    knife cookbook upload -a
    Will upload all cookbooks
  • Updating cookbooks from source
    knife cookbook site vendor cookbook_name # e.g. jenkins
  • Adding roles
    knife role from file path/filename.ext #e.g. roles/jenkins_node.json

Checking the diff between chef and local repository

Using the knife-essentials gem enables us to check the diff of cookbooks, roles, definitions, etc.

Usage:
knife diff cookbooks/name

Installing cookbooks from scratch

Managing nodes

  • Delete instance from AWS and node/client from chef
    knife ec2 server delete i-be7e6fc3 -N "Jenkins-Node-030" --purge
  • List instances
    knife ec2 server list
  • add role or recipe to all nodes
    for n in \`knife node list`; do 
      knife node run_list add $n 'role[lpos_default]'
    done
  • Run arbitrary command on boxes with role
    knife ssh -xubuntu "role:jenkins_agent" "sudo chef-client"
  • Run arbitrary command on all nodes
    knife ssh -xubuntu "name:*" "sudo chef-client"
  • Get last number of Jenkins node
    knife node list | grep Jenkins-Node- | tail -n1 | cut -d '-' -f3
  • Delete all the jenkins agents
    for node in $(knife search node "ohai_time:[* TO $(date -v-16H +%s)]" -i); do 
      knife client delete $node
      knife node delete $node
    done
  • delete all nodes
    for node in `knife ec2 server list | grep 'i-' | cut -d ' ' -f 1`; do 
      knife ec2 server delete $node --purge -y
    done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment