Advertisement
Guest User

Untitled

a guest
Oct 13th, 2015
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. #!/bin/bash
  2. #===============================================================================
  3. #
  4. # FILE: ansible_template.sh
  5. #
  6. # USAGE: ./ansible_template.sh
  7. #
  8. # DESCRIPTION: generates ansible vars and jinja2 template from a config file
  9. #
  10. # OPTIONS: <ansible variable prefix> <config filename> [option separator]
  11. # AUTHOR: Ryan Schulze (rs), ryan@dopefish.de
  12. # CREATED: 03/03/2015 09:56:04 PM CST
  13. # REVISION: 1.2
  14. #===============================================================================
  15.  
  16. #===============================================================================
  17. # Main
  18. #===============================================================================
  19.  
  20. Prefix="${1:-}"
  21. ConfigOriginal="${2}"
  22. Separator="${3:- :=}"
  23.  
  24. usage() {
  25. echo "Usage: $(basename "${0}") <variable name prefix> <config file> [ <config separator> ]"
  26. echo
  27. echo "This small script takes a config file and splits it into a jinja2 template and a yaml file for usage with ansible"
  28. echo
  29. echo "Example: $(basename "${0}") greylist mail.foo.bar:/etc/greylistd/config"
  30. echo
  31. exit 0
  32. }
  33. if [[ $# -lt 2 ]]; then
  34. usage
  35. fi
  36.  
  37. cleanup() {
  38. rm "${tmpfile}"
  39. exit 0
  40. }
  41.  
  42. Template="${ConfigOriginal##*/}.j2"
  43. scp='scp -o connecttimeout=10 -o stricthostkeychecking=no'
  44. ssh='ssh -o connecttimeout=10 -o stricthostkeychecking=no'
  45. tmpfile=$(mktemp)
  46. trap cleanup TERM EXIT
  47.  
  48. if [[ -f "${ConfigOriginal}" ]] ; then
  49. cp "${ConfigOriginal}" "./${Template}"
  50. cp "${Template}" "${tmpfile}"
  51. elif [[ ${ConfigOriginal} =~ ^.+:.+$ ]] ; then
  52. $scp -q "${ConfigOriginal}" "${Template}"
  53. if [[ $? -gt 0 ]] ; then
  54. echo "Error: '${ConfigOriginal}' could not be accessed ..."
  55. exit 1
  56. fi
  57. cp "${Template}" "${tmpfile}"
  58. echo "- name: ${Prefix^} template"
  59. echo " template: src={{ item.local }} dest={{ item.remote }} owner={{ item.owner }} group={{ item.group }} mode={{ item.mode }}"
  60. echo " with_items:"
  61. $ssh "${ConfigOriginal%%:*}" "stat --format \" - { local: '${Template}', remote: '${ConfigOriginal#*:}', owner: '%U', group: '%G', mode: '%.4a' }\" \"${ConfigOriginal#*:}\""
  62. echo
  63. else
  64. echo "Error: '${ConfigOriginal}' needs to be either an existing local or remote file ..."
  65. exit 1
  66. fi
  67.  
  68. while read line
  69. do
  70. # variable in " " or ' ' (foo = "1")
  71. if [[ ${line} =~ ^([^#][^\ ]+)[\ ]*[${Separator}][\ ]*[\"\'](.+)[\'\"]$ ]]; then
  72. VariableName="${Prefix}_${BASH_REMATCH[1]//-/_}"
  73. VariableName="${VariableName,,}"
  74. sed -ri "s/^(${BASH_REMATCH[1]}[\ ]*[${Separator}][\ ]*[\"\']).+([\"\'])$/\1{{ ${VariableName} }}\2/" "${Template}"
  75. printf "%-40s %s\n" "${VariableName}:" "'${BASH_REMATCH[2]}'"
  76. # 'naked' variable (foo = 1)
  77. elif [[ ${line} =~ ^([^#][^\ ]+)[\ ]*[${Separator}][\ ]*([^\ ]+)$ ]] ; then
  78. VariableName="${Prefix}_${BASH_REMATCH[1]//-/_}"
  79. VariableName="${VariableName,,}"
  80. sed -ri "s/^(${BASH_REMATCH[1]}[\ ]*[${Separator}][\ ]*).+$/\1{{ ${VariableName} }}/" "${Template}"
  81. printf "%-40s %s\n" "${VariableName}:" "'${BASH_REMATCH[2]}'"
  82. fi
  83. done < "${tmpfile}"
  84. echo
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement