forked from weareinteractive/ansible-environment
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(multiple-files): Add ability to specify multiple environment fil…
…es (#1)
- Loading branch information
Showing
10 changed files
with
173 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,11 @@ | ||
PWD=$(shell pwd) | ||
ROLE_NAME=franklinkim.environment | ||
ROLE_NAME=ndench.environment | ||
ROLE_PATH=/etc/ansible/roles/$(ROLE_NAME) | ||
TEST_VERSION=ansible --version | ||
TEST_DEFAULT_SYNTAX=ansible-playbook -vv -i 'localhost,' -c local $(ROLE_PATH)/tests/test_default.yml --syntax-check | ||
TEST_DEFAULT_PLAYBOOK=ansible-playbook -vv -i 'localhost,' -c local $(ROLE_PATH)/tests/test_default.yml | ||
TEST_MAIN_SYNTAX=ansible-playbook -vv -i 'localhost,' -c local $(ROLE_PATH)/tests/main.yml --syntax-check | ||
TEST_MAIN_PLAYBOOK=ansible-playbook -vv -i 'localhost,' -c local $(ROLE_PATH)/tests/main.yml | ||
TEST_DEFAULT_CMD=$(TEST_VERSION); $(TEST_DEFAULT_SYNTAX); $(TEST_DEFAULT_PLAYBOOK) | ||
TEST_MAIN_CMD=$(TEST_VERSION); $(TEST_MAIN_SYNTAX); $(TEST_MAIN_PLAYBOOK) | ||
TEST_SYNTAX=ansible-playbook -vv -i 'localhost,' -c local $(ROLE_PATH)/tests/main.yml --syntax-check | ||
TEST_PLAYBOOK=ansible-playbook -vv -i 'localhost,' -c local $(ROLE_PATH)/tests/main.yml | ||
TEST_CMD=$(TEST_VERSION); $(TEST_SYNTAX); $(TEST_PLAYBOOK) | ||
|
||
.PHONY: test | ||
test: | ||
docker run -it --rm -e "ROLE_NAME=$(ROLE_NAME)" -v $(PWD):$(ROLE_PATH) williamyeh/ansible:ubuntu14.04 /bin/bash -c "$(TEST_DEFAULT_CMD)" | ||
docker run -it --rm -e "ROLE_NAME=$(ROLE_NAME)" -v $(PWD):$(ROLE_PATH) williamyeh/ansible:ubuntu14.04 /bin/bash -c "$(TEST_MAIN_CMD)" | ||
docker run -it --rm -e "ROLE_NAME=$(ROLE_NAME)" -v $(PWD):$(ROLE_PATH) williamyeh/ansible:ubuntu14.04 /bin/bash -c "$(TEST_CMD)" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,35 @@ | ||
--- | ||
|
||
# Path to the environment files | ||
system_environment_file: /etc/environment | ||
|
||
# Set this to ennable php-fpm environment variables | ||
php_fpm_environment_file: '' | ||
# Set this if you want the php-fpm service restarted | ||
php_fpm_service_name: '' | ||
# A dictionary of environment files i.e | ||
# environment_files: | ||
# # Keys can be anything, as long as they're unique | ||
# # They're used in the files section of each environment_config | ||
# env: | ||
# path: /etc/environment | ||
# format: system | ||
# owner: root | ||
# group: root | ||
# | ||
# www.conf: | ||
# path: /etc/php/7.1/fpm/pool.d/www.conf | ||
# format: php.ini | ||
# service: php7.1-fpm # Service to restart once changes are made | ||
environment_files: | ||
system: | ||
path: /etc/environment | ||
format: system | ||
|
||
# The environment file owner | ||
environment_file_owner: root | ||
# The environment file group | ||
environment_file_group: root | ||
# A dictionary of config parameters i.e | ||
# | ||
# environment_config: | ||
# LC_ALL: en_US.UTF-8 # Added to system environment | ||
# LC_ALL: | ||
# value: en_US.UTF-8 | ||
# environment_files: [env] # Added to env environment_file | ||
# APP_ENV: | ||
# value: prod | ||
# environments: [system, php-fpm] # Added to system and php-fpm environments | ||
# environment_files: [env, www.conf] # Added to env and www.conf environment_files | ||
# APP_PASSWORD: | ||
# value: security | ||
# environments: [php-fpm] # Added to php-fpm environment | ||
# environment_files: [www.conf] # Added to www.conf environment | ||
environment_config: {} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,58 @@ | ||
--- | ||
|
||
- name: Ensure system file exists | ||
- name: Ensure each environment_file exists | ||
file: | ||
path: "{{ system_environment_file }}" | ||
owner: "{{ environment_file_owner }}" | ||
group: "{{ environment_file_group }}" | ||
path: "{{ item.value.path }}" | ||
owner: "{{ item.value.owner|default(omit) }}" | ||
group: "{{ item.value.owner|default(omit) }}" | ||
state: touch | ||
register: environment_file_result | ||
changed_when: environment_file_result.diff.before.state != "file" | ||
with_dict: "{{ environment_files }}" | ||
|
||
- name: Ensure php-fpm file exists | ||
file: | ||
path: "{{ php_fpm_environment_file }}" | ||
owner: "{{ environment_file_owner }}" | ||
group: "{{ environment_file_group }}" | ||
state: touch | ||
register: environment_file_result | ||
changed_when: environment_file_result.diff.before.state != "file" | ||
when: php_fpm_environment_file != '' | ||
|
||
- name: Remove previous system values | ||
- name: Remove previous system environment values | ||
lineinfile: | ||
dest: "{{ system_environment_file }}" | ||
regexp: '^{{ item.key }}\ ?=' | ||
dest: "{{ environment_files[item.1].path }}" | ||
regexp: '^{{ item.0.key }}\ ?=' | ||
state: absent | ||
with_dict: "{{ environment_config }}" | ||
with_subelements: | ||
- "{{ environment_config }}" | ||
- files | ||
when: environment_files[item.1].format == 'system' | ||
|
||
# The config goes into the system environmnet file if: | ||
# * the value is just a string (the default backwards compatible case) | ||
# * the value is a dict that contains | ||
# * a 'value' string and; | ||
# * an 'environments' list that contains 'system' | ||
- name: Configuring system environment | ||
- name: Add system environment values | ||
lineinfile: | ||
dest: "{{ system_environment_file }}" | ||
line: "{{ item.key }}='{{ item.value if item.value is string else item.value.value }}'" | ||
with_dict: "{{ environment_config }}" | ||
when: item.value is string or (item.value.environments is defined and 'system' in item.value.environments) | ||
|
||
- name: Remove previous php-fpm values | ||
dest: "{{ environment_files[item.1].path }}" | ||
line: "{{ item.0.key }}='{{ item.0.value }}'" | ||
with_subelements: | ||
- "{{ environment_config }}" | ||
- files | ||
when: environment_files[item.1].format == 'system' | ||
|
||
- name: Remove previous php.ini environment values | ||
lineinfile: | ||
dest: "{{ php_fpm_environment_file }}" | ||
regexp: '^env\[{{ item.key }}\]?\ =' | ||
dest: "{{ environment_files[item.1].path }}" | ||
regexp: '^env\[{{ item.0.key }}\]?\ =' | ||
state: absent | ||
with_dict: "{{ environment_config }}" | ||
when: php_fpm_environment_file != '' and (item.value.environments is defined and 'php-fpm' in item.value.environments) | ||
with_subelements: | ||
- "{{ environment_config }}" | ||
- files | ||
when: environment_files[item.1].format == 'php.ini' | ||
|
||
# The config goes into the php-fpm environment file if: | ||
# * the value is a dict that contains | ||
# * a 'value' string and; | ||
# * an 'environments' list that contains 'php-fpm' | ||
- name: Configuring php-fpm environment | ||
- name: Add php.ini environment values | ||
lineinfile: | ||
dest: "{{ php_fpm_environment_file }}" | ||
line: 'env[{{ item.key }}] = "{{ item.value.value }}"' | ||
with_dict: "{{ environment_config }}" | ||
when: php_fpm_environment_file != '' and (item.value.environments is defined and 'php-fpm' in item.value.environments) | ||
|
||
- name: restart php-fpm | ||
systemd: | ||
name: "{{ php_fpm_service_name }}" | ||
dest: "{{ environment_files[item.1].path }}" | ||
line: 'env[{{ item.0.key }}] = "{{ item.0.value }}"' | ||
with_subelements: | ||
- "{{ environment_config }}" | ||
- files | ||
when: environment_files[item.1].format == 'php.ini' | ||
|
||
- name: Restart services | ||
service: | ||
name: "{{ environment_files[item.1].service }}" | ||
state: restarted | ||
when: php_fpm_service_name != '' | ||
|
||
with_subelements: | ||
- "{{ environment_config }}" | ||
- files | ||
when: environment_files[item.1].service is defined |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- | ||
|
||
- name: Validate environment formats | ||
fail: | ||
msg: The only supported environment file formats are 'system' and 'php.ini' | ||
when: item.value.format not in ['system', 'php.ini'] | ||
with_dict: "{{ environment_files }}" | ||
|
||
- name: Validate environment config specifies valid files | ||
fail: | ||
msg: The specified files must exist in the environment_files dict | ||
when: item.files | difference(environment_files.keys()) | ||
with_items: "{{ environment_config }}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.