diff options
-rw-r--r-- | ansible.cfg | 1 | ||||
-rwxr-xr-x | apply-role.sh | 16 | ||||
-rw-r--r-- | chaos-at-home/vm-install.yml | 2 | ||||
-rw-r--r-- | common/utils.sh | 21 | ||||
-rw-r--r-- | common/vm-install.yml (renamed from vm-install.yml) | 3 | ||||
-rw-r--r-- | dan/vm-install.yml | 2 | ||||
-rw-r--r-- | inventory/group_vars/all/main.yml | 2 | ||||
-rw-r--r-- | inventory/hosts.ini | 10 | ||||
-rw-r--r-- | spreadspace/vm-install.yml | 2 | ||||
-rwxr-xr-x | upgrade.sh | 16 | ||||
-rwxr-xr-x | vm-install.sh | 11 |
11 files changed, 64 insertions, 22 deletions
diff --git a/ansible.cfg b/ansible.cfg index 8d436f20..4248b8ba 100644 --- a/ansible.cfg +++ b/ansible.cfg @@ -20,6 +20,7 @@ vault_id_match = True var_compression_level = 9 +bin_ansible_callbacks = True [ssh_connection] pipelining = True diff --git a/apply-role.sh b/apply-role.sh index a2b0ac4f..5af348d4 100755 --- a/apply-role.sh +++ b/apply-role.sh @@ -1,15 +1,19 @@ #!/bin/bash -if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] ; then - echo "$0 <environment> <host(s)> <role>" +if [ -z "$1" ] || [ -z "$2" ]; then + echo "$0 <host(s)> <role>" exit 1 fi -env="$1" -shift hosts="$1" shift role="$1" shift -echo "######## applying the role '$role' to host(s) '$hosts' in environment '$env' ########" -exec ansible-playbook -e "myhosts=$hosts" -e "myrole=$role" $@ "$env/generic.yaml" +source "${BASH_SOURCE%/*}/common/utils.sh" +get_ansible_variable env_group "$hosts" +if [ $? -ne 0 ]; then + exit 1 +fi + +echo "######## applying the role '$role' to host(s) '$hosts' in environment '$env_group' ########" +exec ansible-playbook -e "myhosts=$hosts" -e "myrole=$role" $@ "$env_group/generic.yaml" diff --git a/chaos-at-home/vm-install.yml b/chaos-at-home/vm-install.yml new file mode 100644 index 00000000..b5d8bf2e --- /dev/null +++ b/chaos-at-home/vm-install.yml @@ -0,0 +1,2 @@ +--- +- import_playbook: ../common/vm-install.yml diff --git a/common/utils.sh b/common/utils.sh new file mode 100644 index 00000000..2749eda6 --- /dev/null +++ b/common/utils.sh @@ -0,0 +1,21 @@ +## this contains several helper functions + +get_ansible_variable() { + local _var_name="$1" + local _hosts="$2" + + local _result=$(env ANSIBLE_STDOUT_CALLBACK="json" ansible "$_hosts" -m debug -a "var=$_var_name" | \ + jq -r ".plays[].tasks[].hosts[].$_var_name" | sort | uniq) + if [ $? -ne 0 ] || [ -z "$_result" ]; then + return 1 + fi + + local _num_results=$(echo "$_result" | wc -l) + if [ $_num_results -ne 1 ]; then + echo "ERROR: hosts may only belong to one environment group but got: $(echo $_result | xargs | sed 's/ /, /g')" + return 1 + fi + + eval "$_var_name"='$(echo "$_result")' + return 0 +} diff --git a/vm-install.yml b/common/vm-install.yml index e0685f9d..a6faf827 100644 --- a/vm-install.yml +++ b/common/vm-install.yml @@ -35,7 +35,8 @@ set_fact: ansible_ssh_extra_args: "" -- import_playbook: "host_playbooks/{{ vmname }}.yml" +### TODO: fix path to host_playbook +##- import_playbook: "host_playbooks/{{ vmname }}.yml" - name: reboot and wait for VM come back hosts: "{{ vmname }}" diff --git a/dan/vm-install.yml b/dan/vm-install.yml new file mode 100644 index 00000000..b5d8bf2e --- /dev/null +++ b/dan/vm-install.yml @@ -0,0 +1,2 @@ +--- +- import_playbook: ../common/vm-install.yml diff --git a/inventory/group_vars/all/main.yml b/inventory/group_vars/all/main.yml index e30c383a..d23e3952 100644 --- a/inventory/group_vars/all/main.yml +++ b/inventory/group_vars/all/main.yml @@ -1,5 +1,5 @@ --- -sshserver_root_keys: "{{ ssh_keys.equinox[environment_group] | join('\n') }}" +sshserver_root_keys: "{{ ssh_keys.equinox[env_group] | join('\n') }}" equinox_user: name: equinox diff --git a/inventory/hosts.ini b/inventory/hosts.ini index 37f8fc9b..5586ab3d 100644 --- a/inventory/hosts.ini +++ b/inventory/hosts.ini @@ -10,7 +10,7 @@ host_name={{ inventory_hostname }} [chaos-at-home:vars] host_domain=chaos-at-home.org -environment_group=chaos-at-home +env_group=chaos-at-home ansible_host={{ host_name }}.{{ host_domain }} ansible_user=root ansible_port=22000 @@ -26,7 +26,7 @@ keyserver [spreadspace:vars] host_domain=spreadspace.org -environment_group=spreadspace +env_group=spreadspace ansible_host={{ host_name }}.{{ host_domain }} ansible_user=root ansible_port=22000 @@ -43,7 +43,7 @@ emc-test [emc:vars] host_domain=spreadspace.org -environment_group=spreadspace +env_group=spreadspace [emc] emc-stats @@ -62,7 +62,7 @@ emc-00 [skillz:vars] host_domain=skillz.biz -environment_group=dan +env_group=dan [skillz] sk2013 host_name=2013 @@ -71,7 +71,7 @@ sk2016 host_name=2016 [elevate:vars] host_domain=elevate.at -environment_group=dan +env_group=dan [elevate] elemedia host_name=media diff --git a/spreadspace/vm-install.yml b/spreadspace/vm-install.yml new file mode 100644 index 00000000..b5d8bf2e --- /dev/null +++ b/spreadspace/vm-install.yml @@ -0,0 +1,2 @@ +--- +- import_playbook: ../common/vm-install.yml @@ -1,13 +1,17 @@ #!/bin/bash -if [ -z "$1" ] || [ -z "$2" ]; then - echo "$0 <environment> <host(s)>" +if [ -z "$1" ]; then + echo "$0 <host(s)>" exit 1 fi -env="$1" -shift hosts="$1" shift -echo "######## upgrading host(s) '$hosts' in environment '$env' ########" -exec ansible-playbook -e "myhosts=$hosts" -e "myrole=upgrade" $@ "$env/generic.yaml" +source "${BASH_SOURCE%/*}/common/utils.sh" +get_ansible_variable env_group "$hosts" +if [ $? -ne 0 ]; then + exit 1 +fi + +echo "######## upgrading host(s) '$hosts' in environment '$env_group' ########" +exec ansible-playbook -e "myhosts=$hosts" -e "myrole=upgrade" $@ "$env_group/generic.yaml" diff --git a/vm-install.sh b/vm-install.sh index 0cc0be48..8d2bc665 100755 --- a/vm-install.sh +++ b/vm-install.sh @@ -4,7 +4,6 @@ if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then echo "$0 <vm> <distro> <codename>" exit 1 fi - name=$1 shift distro=$1 @@ -12,7 +11,13 @@ shift codename=$1 shift -echo "installing vm: $name with $distro/$codename" +source "${BASH_SOURCE%/*}/common/utils.sh" +get_ansible_variable env_group "$name" +if [ $? -ne 0 ]; then + exit 1 +fi + +echo "installing vm: $name with $distro/$codename in environment '$env_group'" echo "" echo "########## clearing old ssh host keys #########" @@ -20,4 +25,4 @@ echo "########## clearing old ssh host keys #########" echo "" echo "######## running the install playbook ########" -exec ansible-playbook -e "vmname=$name" -e "vmdistro=$distro" -e "vmdistcodename=$codename" $@ vm-install.yml +exec ansible-playbook -e "vmname=$name" -e "vmdistro=$distro" -e "vmdistcodename=$codename" $@ "$env_group/vm-install.yml" |