From 251f879b1b39f2cdaeae3f5791a8998b96184a7f Mon Sep 17 00:00:00 2001 From: Beth Date: Sat, 5 Oct 2024 17:38:38 -0500 Subject: [PATCH 1/9] added initial test files --- README.md | 10 +- build_ansible.sh | 120 ++++++++++++++++++++ playbook_templates/privtorrents.actcur.com | 5 + var_files/nginx-ssl/privtorrents.actcur.com | 7 ++ 4 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 build_ansible.sh create mode 100644 playbook_templates/privtorrents.actcur.com create mode 100644 var_files/nginx-ssl/privtorrents.actcur.com diff --git a/README.md b/README.md index 3e2b691..dbca67a 100644 --- a/README.md +++ b/README.md @@ -1 +1,9 @@ -This repository is used to build playbooks based on files that contain role name and version number. \ No newline at end of file +This repository is used to build playbooks based on files that contain role name and version number. + +Usage: + +First, define a branch to use in /ansible/branch. +Next, download just the file build_ansible.sh with the following command: +```curl -k "https://git.actcur.com/actcur-ansible/playbook-builder/raw/branch/prod/build_ansible.sh" -H "accept: application/raw" -o /ansible/build_ansible.sh``` + Note: the prod version of this script can be used. If there is a branch mismatch, this script will download the correct version and restart +Finally, run build_ansible.sh \ No newline at end of file diff --git a/build_ansible.sh b/build_ansible.sh new file mode 100644 index 0000000..e12e66b --- /dev/null +++ b/build_ansible.sh @@ -0,0 +1,120 @@ +#!/bin/bash +# ensure we are running the latest version of the script from the correct branch +if [ -f /ansible/branch ]; +then + branch=`cat /ansible/branch` +else + branch="prod" +fi + +#determine if we need to download the latest version +regrab=false +if [ -f /ansible/current ]; then + latest=`cat /ansible/current` + echo $latest + l_branch=`echo "$latest" | grep -Po ".+:" | grep -Po "[^:]+"` + l_time=`echo "$latest" | grep -Po ":.+" | grep -Po "[^:]+"` + if [ ! "$l_branch" = "$branch" ]; then + regrab=true + else + c_time=`date +%s` + if [ `expr $c_time - $l_time` -gt 120 ]; then + regrab=true + fi + fi +else + regrab=true +fi + +if $regrab;then + # download latest version + curl -k "https://git.actcur.com/actcur-ansible/playbook-builder/raw/branch/$branch/build_ansible.sh" -H "accept: application/raw" -o /tmp/build_ansible.sh + + # verify that download was sucessful + count=`grep -P "(#!/bin/bash)|(end of build_ansible.sh)" /tmp/build_ansible.sh | wc -l` + if [ "$count" != "2" ];then + echo "failed to grab latest build_ansible.sh file, exiting" + exit + fi + + # update build_ansible file and update branch/timestamp info in current + cp /tmp/build_ansible.sh /ansible/build_ansible.sh + echo ${branch}:$c_time > /ansible/current + + # verify the parent script is not build_ansible.sh (to prevent the possibility of recurring until the system crashes in the case of an issue) + me=$(basename "$0") + parent=$(ps -o args= $PPID) + if [[ "$PARENT" != *"$ME"* ]];then + /bin/bash /ansible/build_ansible.sh + fi + exit +fi + +fqdn=`hostnamectl hostname` +curl -k "https://git.actcur.com/actcur-ansible/playbook-builder/raw/branch/$branch/playbook_templates/$fqdn" -H "accept: application/raw" -o /tmp/$fqdn + +# verify file was downloaded successfully +count=`grep -P "end of file" /tmp/$fqdn | wc -l` +if [ "$count" != "2" ];then +echo "failed to grab current playbook build file for $fqdn, exiting" +exit +fi + +# build playbook, retrieve roles and add variables to roles +mkdir /ansible/roles/ +mkdir -p /ansible/new/roles +mkdir -p /ansible/prev/roles +# delete roles from previous attempt to update them +rm -r /ansible/new/roles/* + +# create base playbook +echo "- hosts: $fqdn + become: true + + roles:" > /ansible/new/$fqdn.yml + +lines=`cat /tmp/$fqdn` +for line in $lines +do + elements=() + for element in $(echo $line | tr ";" "\n"); do elements+=("$e") + case ${elements[0]} in + "role") + # format role:role name:branch/tag to use + # clone role repo from specific tag/branch w/o history + git clone -b ${elements[2]} --depth 1 https://git.actcur.com/actcur-ansible/role-${element[1]}.git:/ansible/new/${element[0]} + + # verify branch was cloned successfully + count=`ls /new/$role/ | wc -l` + if [[ $count -lt 9 ]];then + echo "issue grabbing role $role - dir/file count less than 9, exiting" + exit + fi + mv /tmp/roles/${element[0]} /ansible/new/roles + echo " - nginx-ssl" >> /ansible/new/$fqdn.yml + + # retrieve var file if it exists + curl -k "https://git.actcur.com/actcur-ansible/playbook-builder/raw/branch/$branch/var_files/${elements[0]}/$fqdn" -H "accept: application/raw" -o /ansible/new/roles/vars/main.yml + + # verifty file was downloaded successfully + count=`grep -P "(---)|(end of file)" /tmp/build_ansible.sh | wc -l` + if [ "$count" != "2" ];then + echo "issue grabbing vars file for ${element[0]} role on $fqdn - either file is invalid or download filed, exiting" + exit + fi + ;; + "#*") + # this is a comment, ignore it + ;; + *) + echo "skipping invalid entry in playbook build file for $fqdn" + echo "entry: $line" + ;; + esac +done +rm -r /ansible/prev/* +mv /ansible/$fqdn.yml /ansible/prev/ +mv /ansible/roles/ /ansible/prev +mv /ansible/new /ansible + +# end of build_ansible.sh \ No newline at end of file diff --git a/playbook_templates/privtorrents.actcur.com b/playbook_templates/privtorrents.actcur.com new file mode 100644 index 0000000..9c28172 --- /dev/null +++ b/playbook_templates/privtorrents.actcur.com @@ -0,0 +1,5 @@ +role:deluge:branch:testing +role:nginx-ssl:branch:testing +role:certbot:branch:testing + +# end of file \ No newline at end of file diff --git a/var_files/nginx-ssl/privtorrents.actcur.com b/var_files/nginx-ssl/privtorrents.actcur.com new file mode 100644 index 0000000..79a90ff --- /dev/null +++ b/var_files/nginx-ssl/privtorrents.actcur.com @@ -0,0 +1,7 @@ +--- +# vars file for nginx-ssl on privtorrents.actcur.com + +privtorrents.actcur.com: + port: 8112 + +# end of file \ No newline at end of file From 8dc583b44e45570010305a60392e369f14575808 Mon Sep 17 00:00:00 2001 From: Beth Date: Sat, 5 Oct 2024 17:51:31 -0500 Subject: [PATCH 2/9] "fixed a few issues" --- build_ansible.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/build_ansible.sh b/build_ansible.sh index e12e66b..b2e17d4 100644 --- a/build_ansible.sh +++ b/build_ansible.sh @@ -18,7 +18,7 @@ if [ -f /ansible/current ]; then regrab=true else c_time=`date +%s` - if [ `expr $c_time - $l_time` -gt 120 ]; then + if [[ `expr $c_time - $l_time` -gt 120 ]]; then regrab=true fi fi @@ -32,7 +32,7 @@ if $regrab;then # verify that download was sucessful count=`grep -P "(#!/bin/bash)|(end of build_ansible.sh)" /tmp/build_ansible.sh | wc -l` - if [ "$count" != "2" ];then + if [ "$count" != "3" ];then echo "failed to grab latest build_ansible.sh file, exiting" exit fi @@ -44,7 +44,7 @@ if $regrab;then # verify the parent script is not build_ansible.sh (to prevent the possibility of recurring until the system crashes in the case of an issue) me=$(basename "$0") parent=$(ps -o args= $PPID) - if [[ "$PARENT" != *"$ME"* ]];then + if [[ "$parent" != *"$me"* ]];then /bin/bash /ansible/build_ansible.sh fi exit From 8e171bb79d6a9c4e910052d0660768b53122f1db Mon Sep 17 00:00:00 2001 From: Beth Date: Sat, 5 Oct 2024 18:11:45 -0500 Subject: [PATCH 3/9] fixed some issue --- build_ansible.sh | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/build_ansible.sh b/build_ansible.sh index b2e17d4..e51cde9 100644 --- a/build_ansible.sh +++ b/build_ansible.sh @@ -39,7 +39,7 @@ if $regrab;then # update build_ansible file and update branch/timestamp info in current cp /tmp/build_ansible.sh /ansible/build_ansible.sh - echo ${branch}:$c_time > /ansible/current + echo ${branch}:`date +%s` > /ansible/current # verify the parent script is not build_ansible.sh (to prevent the possibility of recurring until the system crashes in the case of an issue) me=$(basename "$0") @@ -55,7 +55,7 @@ curl -k "https://git.actcur.com/actcur-ansible/playbook-builder/raw/branch/$bran # verify file was downloaded successfully count=`grep -P "end of file" /tmp/$fqdn | wc -l` -if [ "$count" != "2" ];then +if [ "$count" != "1" ];then echo "failed to grab current playbook build file for $fqdn, exiting" exit fi @@ -73,11 +73,10 @@ echo "- hosts: $fqdn roles:" > /ansible/new/$fqdn.yml -lines=`cat /tmp/$fqdn` -for line in $lines +while read line do elements=() - for element in $(echo $line | tr ";" "\n"); do elements+=("$e") + for element in $(echo $line | tr ";" "\n"); do elements+=("$e");done case ${elements[0]} in "role") # format role:role name:branch/tag to use @@ -111,7 +110,7 @@ do echo "entry: $line" ;; esac -done +done Date: Sat, 5 Oct 2024 18:21:13 -0500 Subject: [PATCH 4/9] Update --- build_ansible.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build_ansible.sh b/build_ansible.sh index e51cde9..f8cca48 100644 --- a/build_ansible.sh +++ b/build_ansible.sh @@ -76,7 +76,7 @@ echo "- hosts: $fqdn while read line do elements=() - for element in $(echo $line | tr ";" "\n"); do elements+=("$e");done + for element in $(echo $line | tr ":" "\n"); do elements+=("$e");done case ${elements[0]} in "role") # format role:role name:branch/tag to use @@ -114,6 +114,6 @@ done Date: Sat, 5 Oct 2024 18:39:21 -0500 Subject: [PATCH 5/9] testing --- build_ansible.sh | 13 ++++++++----- playbook_templates/privtorrents.actcur.com | 6 +++--- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/build_ansible.sh b/build_ansible.sh index f8cca48..b58dd0f 100644 --- a/build_ansible.sh +++ b/build_ansible.sh @@ -50,6 +50,9 @@ if $regrab;then exit fi +# install/update dependencies +pacman -Sy git --noconfirm + fqdn=`hostnamectl hostname` curl -k "https://git.actcur.com/actcur-ansible/playbook-builder/raw/branch/$branch/playbook_templates/$fqdn" -H "accept: application/raw" -o /tmp/$fqdn @@ -61,7 +64,7 @@ exit fi # build playbook, retrieve roles and add variables to roles -mkdir /ansible/roles/ +mkdir -p /ansible/roles/ mkdir -p /ansible/new/roles mkdir -p /ansible/prev/roles # delete roles from previous attempt to update them @@ -76,12 +79,12 @@ echo "- hosts: $fqdn while read line do elements=() - for element in $(echo $line | tr ":" "\n"); do elements+=("$e");done + for e in $(echo $line | tr ":" "\n"); do elements+=("$e");done case ${elements[0]} in "role") # format role:role name:branch/tag to use # clone role repo from specific tag/branch w/o history - git clone -b ${elements[2]} --depth 1 https://git.actcur.com/actcur-ansible/role-${element[1]}.git:/ansible/new/${element[0]} + git clone -b ${elements[2]} --depth 1 https://git.actcur.com/actcur-ansible/role-${elements[1]}.git /ansible/new/roles/${elements[1]} # verify branch was cloned successfully count=`ls /new/$role/ | wc -l` @@ -89,7 +92,7 @@ do echo "issue grabbing role $role - dir/file count less than 9, exiting" exit fi - mv /tmp/roles/${element[0]} /ansible/new/roles + mv /tmp/roles/${elements[0]} /ansible/new/roles echo " - nginx-ssl" >> /ansible/new/$fqdn.yml # retrieve var file if it exists @@ -98,7 +101,7 @@ do # verifty file was downloaded successfully count=`grep -P "(---)|(end of file)" /tmp/build_ansible.sh | wc -l` if [ "$count" != "2" ];then - echo "issue grabbing vars file for ${element[0]} role on $fqdn - either file is invalid or download filed, exiting" + echo "issue grabbing vars file for ${elements[0]} role on $fqdn - either file is invalid or download filed, exiting" exit fi ;; diff --git a/playbook_templates/privtorrents.actcur.com b/playbook_templates/privtorrents.actcur.com index 9c28172..e343d7e 100644 --- a/playbook_templates/privtorrents.actcur.com +++ b/playbook_templates/privtorrents.actcur.com @@ -1,5 +1,5 @@ -role:deluge:branch:testing -role:nginx-ssl:branch:testing -role:certbot:branch:testing +role:deluge:testing +role:nginx-ssl:testing +role:certbot:testing # end of file \ No newline at end of file From 1a6356a44a019fd25fe45f4864e7a84d33a58f7c Mon Sep 17 00:00:00 2001 From: Beth Date: Sat, 5 Oct 2024 18:50:14 -0500 Subject: [PATCH 6/9] update --- build_ansible.sh | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/build_ansible.sh b/build_ansible.sh index b58dd0f..292a363 100644 --- a/build_ansible.sh +++ b/build_ansible.sh @@ -87,16 +87,15 @@ do git clone -b ${elements[2]} --depth 1 https://git.actcur.com/actcur-ansible/role-${elements[1]}.git /ansible/new/roles/${elements[1]} # verify branch was cloned successfully - count=`ls /new/$role/ | wc -l` - if [[ $count -lt 9 ]];then + count=`ls /ansible/new/roles/${elements[1]}/ | wc -l` + if [[ $count -lt 5 ]];then echo "issue grabbing role $role - dir/file count less than 9, exiting" exit fi - mv /tmp/roles/${elements[0]} /ansible/new/roles echo " - nginx-ssl" >> /ansible/new/$fqdn.yml # retrieve var file if it exists - curl -k "https://git.actcur.com/actcur-ansible/playbook-builder/raw/branch/$branch/var_files/${elements[0]}/$fqdn" -H "accept: application/raw" -o /ansible/new/roles/vars/main.yml + curl -k "https://git.actcur.com/actcur-ansible/playbook-builder/raw/branch/$branch/var_files/${elements[1]}/$fqdn" -H "accept: application/raw" -o /ansible/new/roles/${element[1]/vars/main.yml # verifty file was downloaded successfully count=`grep -P "(---)|(end of file)" /tmp/build_ansible.sh | wc -l` From 6526d7bf2b90a96e85ad6d03b12beb7c31ae0b30 Mon Sep 17 00:00:00 2001 From: Beth Date: Sat, 5 Oct 2024 19:17:01 -0500 Subject: [PATCH 7/9] update --- build_ansible.sh | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/build_ansible.sh b/build_ansible.sh index 292a363..d116e18 100644 --- a/build_ansible.sh +++ b/build_ansible.sh @@ -50,9 +50,6 @@ if $regrab;then exit fi -# install/update dependencies -pacman -Sy git --noconfirm - fqdn=`hostnamectl hostname` curl -k "https://git.actcur.com/actcur-ansible/playbook-builder/raw/branch/$branch/playbook_templates/$fqdn" -H "accept: application/raw" -o /tmp/$fqdn @@ -71,7 +68,7 @@ mkdir -p /ansible/prev/roles rm -r /ansible/new/roles/* # create base playbook -echo "- hosts: $fqdn +echo "- hosts: localhost become: true roles:" > /ansible/new/$fqdn.yml @@ -92,16 +89,15 @@ do echo "issue grabbing role $role - dir/file count less than 9, exiting" exit fi - echo " - nginx-ssl" >> /ansible/new/$fqdn.yml + echo " - ${elements[1]}" >> /ansible/new/$fqdn.yml # retrieve var file if it exists - curl -k "https://git.actcur.com/actcur-ansible/playbook-builder/raw/branch/$branch/var_files/${elements[1]}/$fqdn" -H "accept: application/raw" -o /ansible/new/roles/${element[1]/vars/main.yml - + curl -k "https://git.actcur.com/actcur-ansible/playbook-builder/raw/branch/$branch/var_files/${elements[1]}/$fqdn" -H "accept: application/raw" -o /tmp/main.yml + # /ansible/new/roles/${elements[1]}/vars/main.yml # verifty file was downloaded successfully - count=`grep -P "(---)|(end of file)" /tmp/build_ansible.sh | wc -l` - if [ "$count" != "2" ];then - echo "issue grabbing vars file for ${elements[0]} role on $fqdn - either file is invalid or download filed, exiting" - exit + count=`grep -P "(---)|(end of file)" /tmp/main.yml | wc -l` + if [ "$count" == "2" ];then + mv /tmp/main.yml /ansible/new/roles/${elements[1]}/vars/main.yml fi ;; "#*") @@ -118,4 +114,7 @@ mv /ansible/$fqdn.yml /ansible/prev/ mv /ansible/roles/ /ansible/prev mv /ansible/new/* /ansible/ +cd /ansible +ansible-playbook ${fqdn}.yml + # end of build_ansible.sh \ No newline at end of file From d1f0aa8848d6ba4ff1a3b84f85074b5a7bc93d68 Mon Sep 17 00:00:00 2001 From: Beth Date: Mon, 7 Oct 2024 22:00:16 -0500 Subject: [PATCH 8/9] update --- playbook_templates/privtorrents.actcur.com | 2 ++ var_files/certbot/privtorrents.actcur.com | 7 +++++++ var_files/mount/privtorrents.actcur.com | 12 ++++++++++++ var_files/nginx-ssl/privtorrents.actcur.com | 5 +++-- 4 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 var_files/certbot/privtorrents.actcur.com create mode 100644 var_files/mount/privtorrents.actcur.com diff --git a/playbook_templates/privtorrents.actcur.com b/playbook_templates/privtorrents.actcur.com index e343d7e..54c2a17 100644 --- a/playbook_templates/privtorrents.actcur.com +++ b/playbook_templates/privtorrents.actcur.com @@ -1,3 +1,5 @@ +role:core:testing +role:mount:testing role:deluge:testing role:nginx-ssl:testing role:certbot:testing diff --git a/var_files/certbot/privtorrents.actcur.com b/var_files/certbot/privtorrents.actcur.com new file mode 100644 index 0000000..a0c114a --- /dev/null +++ b/var_files/certbot/privtorrents.actcur.com @@ -0,0 +1,7 @@ +--- +# vars file for certbot on privtorrents.actcur.com + +domains: + - domain: privtorrents.actcur.com + +# end of file \ No newline at end of file diff --git a/var_files/mount/privtorrents.actcur.com b/var_files/mount/privtorrents.actcur.com new file mode 100644 index 0000000..d43e09c --- /dev/null +++ b/var_files/mount/privtorrents.actcur.com @@ -0,0 +1,12 @@ +--- +# vars file for mount on privtorrents.actcur.com + +mounts: + nfs: + - name: deluge + host: host.actcur.com + remote: /mnt/butter/video/deluge + local: /mnt/deluge + type: nfs + +# end of file \ No newline at end of file diff --git a/var_files/nginx-ssl/privtorrents.actcur.com b/var_files/nginx-ssl/privtorrents.actcur.com index 79a90ff..5953848 100644 --- a/var_files/nginx-ssl/privtorrents.actcur.com +++ b/var_files/nginx-ssl/privtorrents.actcur.com @@ -1,7 +1,8 @@ --- # vars file for nginx-ssl on privtorrents.actcur.com -privtorrents.actcur.com: - port: 8112 +domains: + - domain: privtorrents.actcur.com + port: 8112 # end of file \ No newline at end of file From 88f7f7ed551d2fdb769e1e71461c8333eff91eee Mon Sep 17 00:00:00 2001 From: Beth Parker Date: Wed, 25 Dec 2024 22:42:19 -0600 Subject: [PATCH 9/9] updated build_ansible.sh and privtorrents template to include ansible tags --- build_ansible.sh | 10 +++++++++- playbook_templates/privtorrents.actcur.com | 10 +++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/build_ansible.sh b/build_ansible.sh index d116e18..407c93f 100644 --- a/build_ansible.sh +++ b/build_ansible.sh @@ -89,7 +89,15 @@ do echo "issue grabbing role $role - dir/file count less than 9, exiting" exit fi - echo " - ${elements[1]}" >> /ansible/new/$fqdn.yml + + #get tags and add role + if [ -n "${elements[3]}" ];then + tags="\"${elements[3]//,/\",\"}\"" + echo " - { role: ${elements[1]}, tags: [${tags}] }" >> /ansible/new/$fqdn.yml + else + echo " - ${elements[1]}" >> /ansible/new/$fqdn.yml + fi + # retrieve var file if it exists curl -k "https://git.actcur.com/actcur-ansible/playbook-builder/raw/branch/$branch/var_files/${elements[1]}/$fqdn" -H "accept: application/raw" -o /tmp/main.yml diff --git a/playbook_templates/privtorrents.actcur.com b/playbook_templates/privtorrents.actcur.com index 54c2a17..bad8373 100644 --- a/playbook_templates/privtorrents.actcur.com +++ b/playbook_templates/privtorrents.actcur.com @@ -1,7 +1,7 @@ -role:core:testing -role:mount:testing -role:deluge:testing -role:nginx-ssl:testing -role:certbot:testing +role:core:v1.0:core +role:mount:v1.0:core,mount +role:deluge:v1.0:workload,deluge +role:nginx-ssl:v1.0:common,nginx-ssl +role:certbot:v1.0:common,certbot # end of file \ No newline at end of file