Skip to content

Commit

Permalink
feat(multiple-files): Add ability to specify multiple environment fil…
Browse files Browse the repository at this point in the history
…es (#1)
  • Loading branch information
ndench committed Jan 30, 2018
1 parent d18775c commit a6b26e6
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 136 deletions.
14 changes: 5 additions & 9 deletions Makefile
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)"
99 changes: 54 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,40 @@ Here is a list of all the default variables for this role, which are also availa
```yaml
---

# 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: ''

# The environment file owner
environment_file_owner: root
# The environment file group
environment_file_group: root
# A dictionary of config parameters i.e
# 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

# 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: {}



```


Expand All @@ -79,18 +89,33 @@ This is an example playbook:

- hosts: all
roles:
- franklinkim.environment
- ndench.environment
vars:
php_fpm_environment_file: /etc/php-fpm.www.conf
php_fpm_service_name: '' # Specify this to restart the php-fpm service
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/system.environment
format: system
owner: root
group: root

www.conf:
# Test box doesn't have php installed
path: /etc/php.ini_pool.d_www.conf
format: php.ini
#service: php7.1-fpm # Service to restart once changes are made

environment_config:
LC_ALL: en_US.UTF-8 # Added to system environment
APP_ENV:
- key: LC_ALL
value: en_US.UTF-8
files: [env] # Added to system environment
- key: APP_ENV
value: prod
environments: [system, php-fpm] # Added to system and php-fpm environments
APP_PASSWORD:
files: [env, www.conf] # Added to system and php-fpm environments
- key: APP_PASSWORD
value: security
environments: [php-fpm] # Added to php-fpm environment
files: [www.conf] # Added to php-fpm environment

```

Expand Down Expand Up @@ -119,28 +144,12 @@ $ gem install ansible-role
$ ansible-role docgen
```

## License
Copyright (c) Nathan Dench under the MIT license.

## TODO:

Change the environment name from php-fpm to php.ini
Add ability to specify multiple files:
* Use handlers to stop the services having to restart them multiple times
* Make the tests check the file contents

```yaml
environment_files:
www.conf:
path: /etc/php-fpm.www.conf
format: php.ini
sys:
path: /etc/environment
format: system
environment_config:
LC_ALL: en_US.UTF-8 # Added to system environment
APP_ENV:
value: prod
environment_files: [sys, www.conf] # Added to system and php-fpm environments
APP_PASSWORD:
value: security
environment_files: [www.conf] # Added to php-fpm environment
```

## License
Copyright (c) Nathan Dench under the MIT license.
3 changes: 2 additions & 1 deletion Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ Vagrant.configure("2") do |config|
# View the documentation for the provider you're using for more
# information on available options.
config.vm.provision "ansible" do |ansible|
ansible.compatibility_mode = "2.0"
ansible.playbook = "tests/main.yml"
ansible.verbose = 'vv'
ansible.sudo = true
ansible.become = true
end
end
40 changes: 25 additions & 15 deletions defaults/main.yml
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: {}


2 changes: 1 addition & 1 deletion meta/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
galaxy_info:
author: ndench
company: Nathan Dench
description: Adds /etc/environment and php www.conf variables
description: Adds /etc/environment and php.ini variables
min_ansible_version: 2.4
license: MIT
# Optionally specify the branch Galaxy will use when accessing the GitHub
Expand Down
91 changes: 42 additions & 49 deletions tasks/config.yml
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
8 changes: 8 additions & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
---

- import_tasks: validate.yml
tags:
- validate
- system
- environment
- config
- environment-config

- import_tasks: config.yml
tags:
- system
Expand Down
13 changes: 13 additions & 0 deletions tasks/validate.yml
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 }}"
31 changes: 23 additions & 8 deletions tests/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,30 @@

- hosts: all
roles:
- franklinkim.environment
- ndench.environment
vars:
php_fpm_environment_file: /etc/php-fpm.www.conf
php_fpm_service_name: '' # Specify this to restart the php-fpm service
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/system.environment
format: system
owner: root
group: root

www.conf:
# Test box doesn't have php installed
path: /etc/php.ini_pool.d_www.conf
format: php.ini
#service: php7.1-fpm # Service to restart once changes are made

environment_config:
LC_ALL: en_US.UTF-8 # Added to system environment
APP_ENV:
- key: LC_ALL
value: en_US.UTF-8
files: [env] # Added to system environment
- key: APP_ENV
value: prod
environments: [system, php-fpm] # Added to system and php-fpm environments
APP_PASSWORD:
files: [env, www.conf] # Added to system and php-fpm environments
- key: APP_PASSWORD
value: security
environments: [php-fpm] # Added to php-fpm environment
files: [www.conf] # Added to php-fpm environment

0 comments on commit a6b26e6

Please sign in to comment.