Manage multiple server & client computers with SaltStack (finnish)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
3.3 KiB

  1. #!/bin/sh
  2. # Author: Pekka Helenius (~Fincer), 2018
  3. #
  4. # - This script creates two different apache configurations for two minions: minion_1 & minion_2
  5. # - This script is meant to be run on Salt master
  6. #
  7. # Default site contents for all minions is: Nothing interesting here
  8. # minion_2 gets contents of 'uname -a' command into the index.html page
  9. #
  10. if [ $(id -u) -eq 0 ]; then
  11. mkdir -p /srv/{pillar,salt/apache}
  12. if [ ! -f /srv/pillar/top.sls ]; then
  13. tee /srv/pillar/top.sls <<PILLAR_TOP
  14. base:
  15. PILLAR_TOP
  16. fi
  17. if [ $(grep "\- apache" /srv/pillar/top.sls | wc -l) -eq 0 ]; then
  18. tee -a /srv/pillar/top.sls <<PILLAR_TOP_ADD
  19. '*':
  20. - apache_site_data
  21. PILLAR_TOP_ADD
  22. fi
  23. tee /srv/pillar/apache_site_data.sls <<SITE_DATA
  24. {% if grains['id'] == 'minion_2' %}
  25. site_data: '{{ salt['cmd.run']('uname -a') }}'
  26. {% endif %}
  27. SITE_DATA
  28. tee /srv/salt/apache/samplesite.conf <<SAMPLE_SITE
  29. <VirtualHost *:80>
  30. ServerName {{ servername }}
  31. ServerAlias {{ serveralias }}
  32. ServerAdmin webmaster@localhost
  33. DocumentRoot {{ ('/var/www/html/' + grains['id'] + '/') }}
  34. ErrorLog ${APACHE_LOG_DIR}/error.log
  35. CustomLog ${APACHE_LOG_DIR}/access.log combined
  36. </VirtualHost>
  37. SAMPLE_SITE
  38. tee /srv/salt/apache/sampleindex.html <<SAMPLE_HTML
  39. {{ pillar.get('site_data','Nothing interesting here') }}
  40. SAMPLE_HTML
  41. tee /srv/salt/apache/init.sls <<APACHE_DATA
  42. {% set servername = grains['os'].lower() + '.' + grains['id'] + '.com' %}
  43. {% set serveralias = 'www.' + grains['os'].lower() + '.' + grains['id'] + '.com' %}
  44. apache_install:
  45. pkg.installed:
  46. - pkgs:
  47. - apache2
  48. - curl
  49. sample_page_conf:
  50. file.managed:
  51. - name: /etc/apache2/sites-available/{{ grains['id'] }}.conf
  52. - source: salt://apache/samplesite.conf
  53. - mode: 0644
  54. - user: root
  55. - group: root
  56. - template: jinja
  57. - context:
  58. servername: {{ servername }}
  59. serveralias: {{ serveralias }}
  60. - require:
  61. - pkg: apache_install
  62. enable_sample_page:
  63. cmd.run:
  64. - name: 'a2ensite {{ grains['id'] }}.conf'
  65. - require:
  66. - file: sample_page_conf
  67. sample_page_content:
  68. file.managed:
  69. - mode: 0644
  70. - user: root
  71. - group: root
  72. - makedirs: True
  73. - template: jinja
  74. - name: {{ ('/var/www/html/' + grains['id'] + '/index.html') }}
  75. - source: salt://apache/sampleindex.html
  76. - require:
  77. - cmd: enable_sample_page
  78. add_vhost_domain:
  79. file.append:
  80. - name: /etc/hosts
  81. - text: 127.0.0.1 {{ servername }}
  82. - require:
  83. - file: sample_page_content
  84. restart_httpd:
  85. service.running:
  86. - name: apache2.service
  87. - watch:
  88. - file: add_vhost_domain
  89. - cmd: enable_sample_page
  90. test_page:
  91. cmd.run:
  92. - name: 'curl -s {{ servername }}'
  93. - require:
  94. - service: restart_httpd
  95. APACHE_DATA
  96. if [ ! -f /srv/pillar/apache_site_data.sls ] || \
  97. [ ! -f /srv/salt/apache/init.sls ] || \
  98. [ ! -f /srv/pillar/top.sls ] || \
  99. [ ! -f /srv/salt/apache/samplesite.conf ] || \
  100. [ ! -f /srv/salt/apache/sampleindex.html ]; then
  101. echo "Salt files missing. Aborting."
  102. exit 1
  103. else
  104. echo -e "\e[1m\n**Salt -- pillar.items output**\n\e[0m"
  105. salt '*' pillar.items
  106. echo -e "\e[1m\n**Salt -- state.apply output**\n\e[0m"
  107. salt '*' state.apply apache
  108. fi
  109. else
  110. echo "Run this script as root (or with sudo)"
  111. fi