Skip to content

Instantly share code, notes, and snippets.

@djaboxx
djaboxx / ssh_cmd.py
Last active June 8, 2016 05:17
Using Paramiko to ssh to a node and run a command...
def run_cmd(host, cmd, private_key=SSH_KEY, username=SSH_USER, password=None):
k = paramiko.RSAKey.from_private_key_file(private_key, password)
c = paramiko.SSHClient()
c.load_system_host_keys()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(hostname=host, username=username, pkey=k)
stdin, stdout, stderr = c.exec_command(cmd)
return dict(host=host, stdout=stdout.read(), stderr=stderr.read(), exit=stdout.channel.recv_exit_status())
# sample plugin
class AppPlugin:
def __init__(self, *args, **kwargs):
'''
process stuff, compile self.json_body
'''
def run():
return self.json_body
@djaboxx
djaboxx / lock
Last active July 28, 2022 00:05
Ansible Host Lock
#!/usr/bin/env python
import os
def sanitize_path(path):
path = os.path.expandvars(path)
path = os.path.expanduser(path)
path = os.path.abspath(path)
return path
def main():

Keybase proof

I hereby claim:

To claim this, I am signing this object:

@djaboxx
djaboxx / main.tf
Created June 28, 2018 04:19
advanced counting in Terraform
provider "aws" {
region = "us-east-1"
}
variable "instance_per_az" {
default = 1
description = "number of instances per Availability Zone"
}
variable "max_instances" {
@djaboxx
djaboxx / push_vault_env
Created July 16, 2018 04:40
Bash Function for push Vault Environment Variables to Terraform Enterprise Workspace
function push_vault_env {
WS=${2}
tfe_org=${1}
source ~/.tfe/${tfe_org}
tfe pushvars -name ${tfe_org}/${WS} \
-senv-var "VAULT_TOKEN=${VAULT_TOKEN}" \
-env-var "VAULT_ADDR=${VAULT_ADDR}"
}
@djaboxx
djaboxx / push_aws
Created July 16, 2018 04:42
Bash Function for pushing AWS Environment Variables to Terraform Enterprise Workspace
function push_aws {
WS=${2}
tfe_org=${1}
source ~/.tfe/${tfe_org}
tfe pushvars -name ${tfe_org}/${WS} \
-senv-var "AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}" \
-senv-var "AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}" \
-env-var AWS_DEFAULT_REGION=us-east-1
}
@djaboxx
djaboxx / app_role
Created July 16, 2018 04:51
Bash Function for creating new App Role Login in Hashicorp Vault
function app_role {
role=${1}
policy=${2}
vault write auth/approle/role/${role} \
secret_id_ttl=10m \
token_num_uses=10 \
token_ttl=20m \
token_max_ttl=30m \
secret_id_num_uses=40 \
policies=${policy}
@djaboxx
djaboxx / oauth_tokens
Created August 24, 2018 17:47
Find your Oauth Tokens in TFE
#!/usr/local/bin/python
import requests
import os
import json
import sys
import hcl
def hcl_config(api, config):
with open(os.path.abspath(config), 'r') as fp:
obj = hcl.load(fp)
@djaboxx
djaboxx / vault_fact.py
Created August 24, 2018 19:46
Looking up Vault Facts in Ansible
#!/usr/bin/env python
import json
import subprocess, shlex
import os
from tempfile import NamedTemporaryFile as ntf
from collections import defaultdict
def read_all_with_name(path):
d = list_path(path)
_data = defaultdict(list)