├── .travis.yml ├── README.md ├── Vagrantfile ├── defaults └── main.yml ├── handlers └── main.yml ├── meta └── main.yml ├── tasks ├── 1-install-Debian.yml ├── 1-install.yml ├── 2-configure.yml ├── 3-pools.yml └── main.yml ├── templates ├── php-fpm.conf.j2 └── pool.conf.j2 ├── tests ├── Dockerfile ├── inventory ├── test.sh ├── test.yml ├── vagrant.yml └── vars.yml └── vars ├── Debian.yml └── main.yml /.travis.yml: -------------------------------------------------------------------------------- 1 | # .travis.yml http://bertvv.github.io/notes-to-self/2015/12/13/testing-ansible-roles-with-travis-ci-part-2-multi-platform-tests/ 2 | --- 3 | sudo: required 4 | env: 5 | - > 6 | distribution=ubuntu 7 | version=14.04 8 | extra_vars="php_fpm_version=5.6" 9 | container_id=$(mktemp) 10 | init=/sbin/init 11 | build_args="" 12 | run_opts="" 13 | - > 14 | distribution=ubuntu 15 | version=14.04 16 | extra_vars="php_fpm_version=7.0" 17 | container_id=$(mktemp) 18 | init=/sbin/init 19 | build_args="" 20 | run_opts="" 21 | - > 22 | distribution=ubuntu 23 | version=14.04 24 | extra_vars="php_fpm_version=7.1" 25 | container_id=$(mktemp) 26 | init=/sbin/init 27 | build_args="" 28 | run_opts="" 29 | - > 30 | distribution=ubuntu 31 | version=16.04 32 | extra_vars="php_fpm_version=5.6" 33 | container_id=$(mktemp) 34 | init=/sbin/init 35 | build_args="" 36 | run_opts="--privileged" 37 | - > 38 | distribution=ubuntu 39 | version=16.04 40 | extra_vars="php_fpm_version=7.0" 41 | container_id=$(mktemp) 42 | init=/sbin/init 43 | build_args="" 44 | run_opts="--privileged" 45 | - > 46 | distribution=ubuntu 47 | version=16.04 48 | extra_vars="php_fpm_version=7.1" 49 | container_id=$(mktemp) 50 | init=/sbin/init 51 | build_args="" 52 | run_opts="--privileged" 53 | - > 54 | distribution=debian 55 | version=8 56 | extra_vars="php_fpm_version=5.6" 57 | container_id=$(mktemp) 58 | init=/sbin/init 59 | build_args="" 60 | run_opts="--privileged" 61 | - > 62 | distribution=debian 63 | version=8 64 | extra_vars="php_fpm_version=7.0" 65 | container_id=$(mktemp) 66 | init=/sbin/init 67 | build_args="" 68 | run_opts="--privileged" 69 | - > 70 | distribution=debian 71 | version=8 72 | extra_vars="php_fpm_version=7.1" 73 | container_id=$(mktemp) 74 | init=/sbin/init 75 | build_args="" 76 | run_opts="--privileged" 77 | - > 78 | distribution=debian 79 | version=9 80 | extra_vars="php_fpm_version=5.6" 81 | container_id=$(mktemp) 82 | init=/sbin/init 83 | build_args="--build-arg additional_packages=systemd-sysv" 84 | run_opts="--privileged" 85 | - > 86 | distribution=debian 87 | version=9 88 | extra_vars="php_fpm_version=7.0" 89 | container_id=$(mktemp) 90 | init=/sbin/init 91 | build_args="--build-arg additional_packages=systemd-sysv" 92 | run_opts="--privileged" 93 | - > 94 | distribution=debian 95 | version=9 96 | extra_vars="php_fpm_version=7.1" 97 | container_id=$(mktemp) 98 | init=/sbin/init 99 | build_args="--build-arg additional_packages=systemd-sysv" 100 | run_opts="--privileged" 101 | 102 | services: 103 | - docker 104 | 105 | before_install: 106 | - sudo apt-get update 107 | # Install latest Docker version to support ARG before FROM 108 | - sudo apt-get -y -o Dpkg::Options::="--force-confnew" install docker-ce 109 | # Pull container 110 | - sudo docker pull ${distribution}:${version} 111 | # Customize container 112 | - docker version 113 | - sudo docker build --rm=true --file=tests/Dockerfile --build-arg image=${distribution}:${version} ${build_args} --tag=${distribution}-${version}:ansible tests 114 | 115 | script: 116 | # Run container in detached state 117 | - sudo docker run --detach --volume="${PWD}":/etc/ansible/roles/role_under_test:ro ${run_opts} ${distribution}-${version}:ansible "${init}" >> "${container_id}" 118 | 119 | # Syntax check 120 | - sudo docker exec --tty "$(cat ${container_id})" env TERM=xterm ansible-playbook /etc/ansible/roles/role_under_test/tests/test.yml --extra-vars "${extra_vars}" --syntax-check 121 | # Test role 122 | - sudo docker exec --tty "$(cat ${container_id})" env TERM=xterm ansible-playbook /etc/ansible/roles/role_under_test/tests/test.yml --extra-vars "${extra_vars}" 123 | # Idempotence test 124 | - > 125 | sudo docker exec "$(cat ${container_id})" ansible-playbook "/etc/ansible/roles/role_under_test/tests/test.yml" --extra-vars "${extra_vars}" 126 | | grep -q 'changed=0.*failed=0' 127 | && (echo 'Idempotence test: pass' && exit 0) 128 | || (echo 'Idempotence test: fail' && exit 1) 129 | 130 | # Clean up 131 | - sudo docker stop "$(cat ${container_id})" 132 | 133 | notifications: 134 | webhooks: https://galaxy.ansible.com/api/v1/notifications/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](http://img.shields.io/travis/NBZ4live/ansible-php-fpm.svg?style=flat)](https://travis-ci.org/NBZ4live/ansible-php-fpm) [![Ansible Galaxy](http://img.shields.io/badge/ansible--galaxy-php--fpm-blue.svg?style=flat)](https://galaxy.ansible.com/list#/roles/304) 2 | 3 | php-fpm 4 | ======== 5 | 6 | This role installs and configures the php-fpm interpreter. 7 | 8 | Attention 9 | --------- 10 | RedHat support is discontinued in version 2.0.0. 11 | Pull requests are welcome. 12 | 13 | Requirements 14 | ------------ 15 | 16 | This role requires Ansible 1.4 or higher and tested platforms are listed in the metadata file. 17 | 18 | Role Variables 19 | -------------- 20 | 21 | The role uses the following variables: 22 | 23 | - **php_fpm_version**: PHP version which should be installed. 24 | Available: 5.5, 5.6, 7.0, 7.1. Default: 5.6. Attention: PHP 5.5 can 25 | be used, but it has reached it's end of life and should be avoided! 26 | - **php_fpm_pools**: The list a pools for php-fpm, each pools is a hash with 27 | a name entry (used for filename), all the other entries in the hash are pool 28 | directives (see http://php.net/manual/en/install.fpm.configuration.php). One 29 | level dictionaries nesting is supported, to allow passing environment 30 | variables and PHP settings as dictionaries. 31 | - **php_fpm_pool_defaults**: A list of default directives used for all php-fpm pools 32 | (see http://php.net/manual/en/install.fpm.configuration.php). 33 | - **php_fpm_apt_packages**: The list of packages to be installed by the 34 | ```apt``` module, defaults to ```[php5-fpm]```. 35 | module. 36 | - **php_fpm_yum_packages**: The list of packages to be installed by the 37 | ```yum``` module, defaults to ```[php-fpm]```. 38 | - **php_fpm_ini**: Customization for php-fpm's php.ini as a list of options, 39 | each option is a hash using the following structure: 40 | - **option**: The name of the option. 41 | - **value**: The string value to be associated with the option. 42 | - **section**: Section name in INI file. 43 | - **php_fpm_config**: Customization for php-fpm's configuration file as a list 44 | of options. 45 | - **php_fpm_default_pool**: 46 | - **delete**: Set to a ```True``` value to delete the default pool. 47 | - **name**: The filename the default pool configuration file. 48 | - **php_fpm_apt_latest**: If set to ```yes``` will update the packages to the latest version 49 | 50 | Example configuration 51 | -------------- 52 | 53 | - role: php-fpm 54 | php_fpm_pool_defaults: 55 | pm: dynamic 56 | pm.max_children: 5 57 | pm.start_servers: 2 58 | pm.min_spare_servers: 1 59 | pm.max_spare_servers: 3 60 | php_fpm_pools: 61 | - name: foo 62 | user: www-data 63 | group: www-data 64 | listen: 8000 65 | chdir: / 66 | - name: bar 67 | user: www-data 68 | group: www-data 69 | # Add the host and port in separate variables. 70 | listen_host: 127.0.0.1 71 | # Attention: One of listen_port or listen is required! 72 | listen_port: 8001 73 | env: 74 | PATH: "/usr/local/bin:/usr/bin:/bin" 75 | TMPDIR: "/tmp" 76 | php_admin_value: 77 | sendmail_path: "/usr/sbin/sendmail -t -i -f www@my.domain.com" 78 | error_log: "/var/log/fpm-bar.www.log" 79 | php_fpm_ini: 80 | # PHP section directives 81 | - option: "engine" 82 | section: "PHP" 83 | value: "1" 84 | - option: "error_reporting" 85 | section: "PHP" 86 | value: "E_ALL & ~E_DEPRECATED & ~E_STRICT" 87 | - option: "date.timezone" 88 | section: "PHP" 89 | value: "Europe/Berlin" 90 | # soap section directives 91 | - option: "soap.wsdl_cache_dir" 92 | section: "soap" 93 | value: "/tmp" 94 | # Pdo_mysql section directives 95 | - option: "pdo_mysql.cache_size" 96 | section: "Pdo_mysql" 97 | value: "2000" 98 | php_fpm_config: 99 | - option: "log_level" 100 | section: "global" 101 | value: "notice" 102 | - option: "syslog.facility" 103 | section: "global" 104 | value: "daemon" 105 | 106 | Example usage 107 | ------- 108 | 109 | --- 110 | # file: task.yml 111 | - hosts: all 112 | roles: 113 | - nbz4live.php-fpm 114 | - { 115 | role: nbz4live.php-fpm, 116 | php_fpm_pools:[ 117 | {name: foo, user: www-data, group: www-data, listen: 8000, chdir: /} 118 | ] 119 | } 120 | - role: php-fpm 121 | php_fpm_pools: 122 | - name: bar 123 | user: www-data 124 | group: www-data 125 | listen: 9000 126 | chdir: / 127 | 128 | Attention 129 | ------- 130 | The process manager configuration (pm, pm.max_children, pm.start_servers, pm.min_spare_servers, pm.max_spare_servers), 131 | in the defaults, is only for testing. This values should always be calculated based on the used server resources 132 | (hardware, number of pools, other software on the server). 133 | Please read the [documentation](http://php.net/manual/en/install.fpm.configuration.php#pm) for more information 134 | about this directives or follow [this guide](http://myshell.co.uk/blog/2012/07/adjusting-child-processes-for-php-fpm-nginx/) 135 | to calculate best values for your case. 136 | 137 | License 138 | ------- 139 | 140 | BSD 141 | 142 | Author Information 143 | ------------------ 144 | 145 | - Sergey Fayngold 146 | - Pierre Buyle 147 | -------------------------------------------------------------------------------- /Vagrantfile: -------------------------------------------------------------------------------- 1 | # -*- mode: ruby -*- 2 | # vi: set ft=ruby : 3 | 4 | # All Vagrant configuration is done below. The "2" in Vagrant.configure 5 | # configures the configuration version (we support older styles for 6 | # backwards compatibility). Please don't change it unless you know what 7 | # you're doing. 8 | Vagrant.configure("2") do |config| 9 | # config.vm.box = "ubuntu/trusty64" # Ubuntu 14.04 10 | config.vm.box = "ubuntu/xenial64" # Ubuntu 16.04 11 | # config.vm.box = "debian/jessie64" # Debian 8 12 | 13 | config.vm.network "public_network" 14 | 15 | config.vm.provision "ansible" do |ansible| 16 | ansible.verbose = true 17 | ansible.playbook = "tests/vagrant.yml" 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /defaults/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | apt_cache_valid_time: 86400 3 | 4 | php_fpm_version: 7.1 5 | 6 | php_fpm_default_pool: 7 | delete: yes 8 | name: www.conf 9 | 10 | php_fpm_pools: 11 | - name: foo 12 | user: www-data 13 | group: www-data 14 | listen: 8000 15 | chdir: / 16 | 17 | php_fpm_pool_defaults: 18 | pm: dynamic 19 | pm.max_children: 5 20 | pm.start_servers: 2 21 | pm.min_spare_servers: 1 22 | pm.max_spare_servers: 3 23 | 24 | php_fpm_apt_packages: 25 | - php{{php_fpm_version}}-fpm 26 | 27 | php_fpm_ini: 28 | - option: "engine" 29 | section: "PHP" 30 | value: "On" 31 | - option: "error_reporting" 32 | section: "PHP" 33 | value: "E_ALL & ~E_DEPRECATED & ~E_STRICT" 34 | - option: "ldap.max_links" 35 | section: "ldap" 36 | value: "-1" 37 | 38 | php_fpm_config: [] 39 | -------------------------------------------------------------------------------- /handlers/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: restart php-fpm 3 | service: name={{php_fpm_service_name}} state=restarted 4 | 5 | - name: reload php-fpm 6 | service: name={{php_fpm_service_name}} state=reloaded 7 | -------------------------------------------------------------------------------- /meta/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | galaxy_info: 3 | author: Sergey Fayngold 4 | company: My.com B.V. 5 | license: BSD 6 | min_ansible_version: 1.8 7 | platforms: 8 | - name: Ubuntu 9 | versions: 10 | - trusty 11 | - xenial 12 | - name: Debian 13 | versions: 14 | - jessie 15 | - stretch 16 | categories: 17 | - development 18 | - web 19 | dependencies: [] 20 | 21 | -------------------------------------------------------------------------------- /tasks/1-install-Debian.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | - name: Make sure sury.org repository can be used (Debian) 4 | apt: > 5 | name={{ item }} 6 | state=present 7 | update_cache=yes 8 | cache_valid_time={{ apt_cache_valid_time }} 9 | with_items: 10 | - apt-transport-https 11 | 12 | - name: Add apt reporitory key (Debian) 13 | apt_key: 14 | url: "https://packages.sury.org/php/apt.gpg" 15 | state: present 16 | keyring: /etc/apt/trusted.gpg.d/php.gpg 17 | 18 | - name: Add DEB repository (Debian) 19 | apt_repository: 20 | repo: deb https://packages.sury.org/php/ {{ ansible_distribution_release }} main 21 | state: present 22 | update_cache: yes -------------------------------------------------------------------------------- /tasks/1-install.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | - name: Add PPA repository (Ubuntu) 4 | apt_repository: 5 | repo: ppa:ondrej/php 6 | update_cache: true 7 | when: ansible_distribution == "Ubuntu" 8 | 9 | - include: 1-install-Debian.yml 10 | when: 11 | - ansible_distribution == "Debian" 12 | - ansible_distribution_version|int >= 8 13 | 14 | - name: Install the php packages (APT) 15 | apt: > 16 | name={{ item }} 17 | state={% if php_fpm_apt_latest is defined and php_fpm_apt_latest == 'yes' %}latest{% else %}present{% endif %} 18 | update_cache=yes 19 | cache_valid_time={{ apt_cache_valid_time }} 20 | with_items: "{{ php_fpm_apt_packages }}" 21 | environment: "{{ env }}" 22 | when: ansible_os_family == "Debian" 23 | notify: 24 | - restart php-fpm 25 | tags: [packages,php,fpm] 26 | -------------------------------------------------------------------------------- /tasks/2-configure.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | - name: Copy the PHP configuration file 4 | ini_file: > 5 | dest={{ php_fpm_ini_path }} 6 | section="{{ item.section }}" 7 | option="{{ item.option }}" 8 | value="{{ item.value }}" 9 | backup=yes 10 | with_items: "{{ php_fpm_ini }}" 11 | when: php_fpm_ini|length > 0 12 | notify: 13 | - restart php-fpm 14 | tags: [configuration,php,fpm] 15 | 16 | - name: Copy the FPM configuration 17 | ini_file: > 18 | dest={{ php_fpm_config_path }} 19 | section="{{ item.section }}" 20 | option="{{ item.option }}" 21 | value="{{ item.value }}" 22 | backup=yes 23 | with_items: "{{ php_fpm_config }}" 24 | when: php_fpm_config|length > 0 25 | register: copy_fpm_config 26 | notify: 27 | - restart php-fpm 28 | tags: [configuration,php,fpm] 29 | -------------------------------------------------------------------------------- /tasks/3-pools.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | - name: Delete the default POOL configuration file 4 | file: > 5 | path={{ php_fpm_pool_d }}/{{ php_fpm_default_pool.name }} 6 | state=absent 7 | when: php_fpm_default_pool.delete 8 | tags: [configuration,php,fpm] 9 | 10 | - name: Copy the POOL configurations 11 | template: > 12 | src=pool.conf.j2 13 | dest={{ php_fpm_pool_d }}/{{ item['name'] }}.conf 14 | backup=yes 15 | with_items: "{{ php_fpm_pools }}" 16 | when: php_fpm_pools|length > 0 17 | notify: 18 | - restart php-fpm 19 | tags: [configuration,php,fpm] -------------------------------------------------------------------------------- /tasks/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | - name: Add the OS specific varibles 4 | include_vars: "{{role_path}}/vars/{{ ansible_os_family }}.yml" 5 | 6 | - include: 1-install.yml 7 | - include: 2-configure.yml 8 | - include: 3-pools.yml 9 | 10 | - name: Check php-fpm syntax of configuration files 11 | shell: "{{ php_fpm_binary_name }} -t" 12 | register: result 13 | when: php_fpm_pools|length > 0 14 | changed_when: "result.rc != 0" 15 | tags: [configuration,php,fpm] 16 | 17 | - name: Start the {{ php_fpm_service_name }} service 18 | service: name={{ php_fpm_service_name }} state=started enabled=yes 19 | when: php_fpm_pools|length > 0 20 | tags: [service,php,fpm] -------------------------------------------------------------------------------- /templates/php-fpm.conf.j2: -------------------------------------------------------------------------------- 1 | ;{{ ansible_managed }} 2 | 3 | ;;;;;;;;;;;;;;;;;;;;; 4 | ; FPM Configuration ; 5 | ;;;;;;;;;;;;;;;;;;;;; 6 | 7 | ; All relative paths in this configuration file are relative to PHP's install 8 | ; prefix (/usr). This prefix can be dynamically changed by using the 9 | ; '-p' argument from the command line. 10 | 11 | ; Include one or more files. If glob(3) exists, it is used to include a bunch of 12 | ; files from a glob(3) pattern. This directive can be used everywhere in the 13 | ; file. 14 | ; Relative path can also be used. They will be prefixed by: 15 | ; - the global prefix if it's been set (-p argument) 16 | ; - /usr otherwise 17 | ;include=/etc/php5/fpm/*.conf 18 | 19 | ;;;;;;;;;;;;;;;;;; 20 | ; Global Options ; 21 | ;;;;;;;;;;;;;;;;;; 22 | 23 | [global] 24 | ; Pid file 25 | ; Note: the default prefix is /var 26 | ; Default Value: none 27 | pid = {{ php_fpm_pid_file }} 28 | 29 | ; Error log file 30 | ; If it's set to "syslog", log is sent to syslogd instead of being written 31 | ; in a local file. 32 | ; Note: the default prefix is /var 33 | ; Default Value: log/php-fpm.log 34 | error_log = /var/log/php5-fpm.log 35 | 36 | ; syslog_facility is used to specify what type of program is logging the 37 | ; message. This lets syslogd specify that messages from different facilities 38 | ; will be handled differently. 39 | ; See syslog(3) for possible values (ex daemon equiv LOG_DAEMON) 40 | ; Default Value: daemon 41 | ;syslog.facility = daemon 42 | 43 | ; syslog_ident is prepended to every message. If you have multiple FPM 44 | ; instances running on the same server, you can change the default value 45 | ; which must suit common needs. 46 | ; Default Value: php-fpm 47 | ;syslog.ident = php-fpm 48 | 49 | ; Log level 50 | ; Possible Values: alert, error, warning, notice, debug 51 | ; Default Value: notice 52 | ;log_level = notice 53 | 54 | ; If this number of child processes exit with SIGSEGV or SIGBUS within the time 55 | ; interval set by emergency_restart_interval then FPM will restart. A value 56 | ; of '0' means 'Off'. 57 | ; Default Value: 0 58 | ;emergency_restart_threshold = 0 59 | 60 | ; Interval of time used by emergency_restart_interval to determine when 61 | ; a graceful restart will be initiated. This can be useful to work around 62 | ; accidental corruptions in an accelerator's shared memory. 63 | ; Available Units: s(econds), m(inutes), h(ours), or d(ays) 64 | ; Default Unit: seconds 65 | ; Default Value: 0 66 | ;emergency_restart_interval = 0 67 | 68 | ; Time limit for child processes to wait for a reaction on signals from master. 69 | ; Available units: s(econds), m(inutes), h(ours), or d(ays) 70 | ; Default Unit: seconds 71 | ; Default Value: 0 72 | ;process_control_timeout = 0 73 | 74 | ; The maximum number of processes FPM will fork. This has been design to control 75 | ; the global number of processes when using dynamic PM within a lot of pools. 76 | ; Use it with caution. 77 | ; Note: A value of 0 indicates no limit 78 | ; Default Value: 0 79 | ; process.max = 128 80 | 81 | ; Specify the nice(2) priority to apply to the master process (only if set) 82 | ; The value can vary from -19 (highest priority) to 20 (lower priority) 83 | ; Note: - It will only work if the FPM master process is launched as root 84 | ; - The pool process will inherit the master process priority 85 | ; unless it specified otherwise 86 | ; Default Value: no set 87 | ; process.priority = -19 88 | 89 | ; Send FPM to background. Set to 'no' to keep FPM in foreground for debugging. 90 | ; Default Value: yes 91 | ;daemonize = yes 92 | 93 | ; Set open file descriptor rlimit for the master process. 94 | ; Default Value: system defined value 95 | ;rlimit_files = 1024 96 | 97 | ; Set max core size rlimit for the master process. 98 | ; Possible Values: 'unlimited' or an integer greater or equal to 0 99 | ; Default Value: system defined value 100 | ;rlimit_core = 0 101 | 102 | ; Specify the event mechanism FPM will use. The following is available: 103 | ; - select (any POSIX os) 104 | ; - poll (any POSIX os) 105 | ; - epoll (linux >= 2.5.44) 106 | ; - kqueue (FreeBSD >= 4.1, OpenBSD >= 2.9, NetBSD >= 2.0) 107 | ; - /dev/poll (Solaris >= 7) 108 | ; - port (Solaris >= 10) 109 | ; Default Value: not set (auto detection) 110 | ;events.mechanism = epoll 111 | 112 | ; When FPM is build with systemd integration, specify the interval, 113 | ; in second, between health report notification to systemd. 114 | ; Set to 0 to disable. 115 | ; Available Units: s(econds), m(inutes), h(ours) 116 | ; Default Unit: seconds 117 | ; Default value: 10 118 | ;systemd_interval = 10 119 | 120 | ;;;;;;;;;;;;;;;;;;;; 121 | ; Pool Definitions ; 122 | ;;;;;;;;;;;;;;;;;;;; 123 | 124 | ; Multiple pools of child processes may be started with different listening 125 | ; ports and different management options. The name of the pool will be 126 | ; used in logs and stats. There is no limitation on the number of pools which 127 | ; FPM can handle. Your system will tell you anyway :) 128 | 129 | ; To configure the pools it is recommended to have one .conf file per 130 | ; pool in the following directory: 131 | include={{php_fpm_pool_d}}/*.conf -------------------------------------------------------------------------------- /templates/pool.conf.j2: -------------------------------------------------------------------------------- 1 | ;{{ ansible_managed }} 2 | 3 | [{{ item.name }}] 4 | {% set pools_directives = php_fpm_pool_defaults | default(dict()) | combine(item) -%} 5 | 6 | {% set listen = '' -%} 7 | {% if 'listen_port' in pools_directives -%} 8 | {% if 'listen_host' in pools_directives -%} 9 | {% set listen = listen ~ pools_directives['listen_host'] + ':' %} 10 | {%- endif %} 11 | {% set listen = listen ~ pools_directives['listen_port'] %} 12 | {% elif 'listen' in pools_directives -%} 13 | {% set listen = pools_directives['listen'] %} 14 | {%- endif %} 15 | 16 | {% if listen -%} 17 | listen={{ listen }} 18 | {%- endif %} 19 | 20 | {% for directive, value in pools_directives.items() if directive not in ("name", "listen", "listen_host", "listen_port") -%} 21 | {% if value is mapping %} 22 | {% for key, value2 in value.items() %} 23 | {{ directive }}[{{ key }}] = {{ value2 }} 24 | {% endfor %} 25 | {% else %} 26 | {{ directive }} = {{ value }} 27 | {% endif %} 28 | {%- endfor %} 29 | -------------------------------------------------------------------------------- /tests/Dockerfile: -------------------------------------------------------------------------------- 1 | # Dockerfile 2 | ARG image 3 | FROM $image 4 | # Install Ansible 5 | RUN apt-get update 6 | ARG additional_packages 7 | RUN apt-get install -y software-properties-common git gnupg 8 | RUN echo apt-get install -y wget $additional_packages 9 | RUN apt-get install -y wget $additional_packages 10 | RUN echo "deb http://ppa.launchpad.net/ansible/ansible/ubuntu trusty main" > /etc/apt/sources.list.d/ansible.list 11 | RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 93C4A3FD7BB9C367 12 | RUN apt-get update 13 | RUN apt-get install -y ansible 14 | # Install Ansible inventory file 15 | RUN echo "[local]\nlocalhost ansible_connection=local" > /etc/ansible/hosts 16 | RUN echo "retry_files_enabled = False" >> /etc/ansible/ansible.cfg -------------------------------------------------------------------------------- /tests/inventory: -------------------------------------------------------------------------------- 1 | localhost 2 | -------------------------------------------------------------------------------- /tests/test.sh: -------------------------------------------------------------------------------- 1 | #!/usr/local/bin/bash 2 | 3 | COLOR='\e[44m' 4 | NC='\e[49m' # No Color 5 | 6 | PWD="$( cd ${0%/*}/.. && pwd -P )" 7 | 8 | cd $PWD; 9 | 10 | # Declare the variables 11 | distributions="ubuntu debian" 12 | ubuntu_versions="14.04 16.04" 13 | debian_versions="8 9" 14 | php_versions="5.6 7.0 7.1" 15 | #php_versions="7.0" # For testing 16 | force_build="false" 17 | dry_run="false" 18 | no_cache="false" 19 | keep_container="false" 20 | interactive="false" 21 | 22 | # Check the options and override variables 23 | while getopts d:v:p:tfcki option 24 | do 25 | case "${option}" 26 | in 27 | d) distributions=${OPTARG};; 28 | v) VERSION=${OPTARG};; 29 | p) php_versions=${OPTARG};; 30 | t) dry_run="true";; 31 | f) force_build="true";; 32 | c) no_cache="true";; 33 | k) keep_container="true";; 34 | i) interactive="true";; 35 | esac 36 | done 37 | 38 | # Function that runs the test inside Docker 39 | run_test() { 40 | # Declare function arguments 41 | local container_id=$(mktemp) 42 | local distribution=$1 43 | local version=$2 44 | local php_version=$3 45 | 46 | # Declare extra variables 47 | local extra_vars="php_fpm_version=${php_version}" 48 | local run_opts="--privileged" 49 | local init=/sbin/init 50 | local additional_packages="" 51 | 52 | local image=${distribution}:${version} 53 | local tag=${distribution}-${version}:ansible 54 | local role_path="/etc/ansible/roles/role_under_test/tests/test.yml" 55 | 56 | # Declare Docker build arguments 57 | local build_args="--build-arg image=${image}" 58 | 59 | # We need to install systemd-sysv on Debian 9 for Ansible service module to work 60 | if [ ${image} = "debian:9" ]; 61 | then 62 | build_args="${build_args} --build-arg additional_packages=systemd-sysv" 63 | fi 64 | 65 | if [ ${no_cache} = "true" ]; 66 | then 67 | build_args="--no-cache ${build_args}" 68 | fi 69 | 70 | printf "${COLOR}Start testing PHP ${php_version} on ${image}${NC}\n" 71 | 72 | # Check if we already have a built image or forced option 73 | local require_build=$(docker images ${tag} -q) 74 | if [ -z "${require_build}" ] || [ ${force_build} = "true" ]; then 75 | printf "${COLOR}Pull container ${image}${NC}\n" 76 | docker pull ${image} 77 | 78 | printf "${COLOR}Customize container${NC}\n" 79 | 80 | docker build --rm=true --file=tests/Dockerfile ${build_args} --tag=${tag} tests 81 | fi 82 | 83 | # Check if it is a dry run only for building the image 84 | if [ ${dry_run} = "true" ]; then 85 | return 86 | fi 87 | 88 | printf "${COLOR}Run container in detached state (${image})${NC}\n" 89 | docker run --detach --volume="${PWD}":/etc/ansible/roles/role_under_test:rw ${run_opts} ${tag} "${init}" >> "${container_id}" 90 | 91 | printf "${COLOR}Syntax check${NC}\n" 92 | docker exec --tty "$(cat ${container_id})" env TERM=xterm ansible-playbook ${role_path} --extra-vars "${extra_vars}" --syntax-check 93 | 94 | printf "${COLOR}Test role (${image})${NC}\n" 95 | docker exec --tty "$(cat ${container_id})" env TERM=xterm ansible-playbook ${role_path} --extra-vars "${extra_vars}" 96 | 97 | printf "${COLOR}Idempotence test (${image})${NC}\n" 98 | docker exec "$(cat ${container_id})" ansible-playbook ${role_path} --extra-vars "${extra_vars}" \ 99 | | grep -q 'changed=0.*failed=0' && (echo 'Idempotence test: pass' && exit 0) || (echo 'Idempotence test: fail' && exit 1) 100 | 101 | if [ ${interactive} = "true" ]; then 102 | docker exec -it "$(cat ${container_id})" bash 103 | fi 104 | 105 | if [ ${keep_container} = "true" ]; then 106 | cat ${container_id} 107 | else 108 | printf "${COLOR}Clean up (${image})${NC}\n" 109 | docker stop "$(cat ${container_id})" 110 | docker rm "$(cat ${container_id})" 111 | fi 112 | } 113 | 114 | # Loop to run the test on each defined distribution, os version and PHP version 115 | for distribution in $distributions; do 116 | os_version_var_name=${distribution}_versions 117 | 118 | # Check if we define the 119 | if [ -n "${VERSION}" ] 120 | then 121 | os_versions="${VERSION}" 122 | else 123 | os_versions=${!os_version_var_name} 124 | fi 125 | 126 | for os_version in ${os_versions}; do 127 | for php_version in ${php_versions}; do 128 | run_test ${distribution} ${os_version} ${php_version} 129 | done 130 | done 131 | done 132 | -------------------------------------------------------------------------------- /tests/test.yml: -------------------------------------------------------------------------------- 1 | # test.yml 2 | --- 3 | - hosts: all 4 | vars: 5 | php_fpm_pools: 6 | - name: foo 7 | user: www-data 8 | group: www-data 9 | listen: 7000 10 | chdir: / 11 | - name: bar 12 | user: www-data 13 | group: www-data 14 | listen_port: 8000 15 | chdir: / 16 | - name: baz 17 | user: www-data 18 | group: www-data 19 | listen_host: 127.0.0.1 20 | listen_port: 9000 21 | chdir: / 22 | roles: 23 | - role_under_test -------------------------------------------------------------------------------- /tests/vagrant.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - hosts: all 3 | gather_facts: False 4 | pre_tasks: 5 | - name: Install python for Ansible 6 | raw: test -e /usr/bin/python || (sudo apt -y update && sudo apt install -y python-minimal) 7 | changed_when: False 8 | - setup: # aka gather_facts 9 | 10 | - hosts: all 11 | remote_user: root 12 | sudo: true 13 | vars_files: 14 | - '../vars/main.yml' 15 | - '../defaults/main.yml' 16 | - 'vars.yml' 17 | tasks: 18 | - include: '../tasks/main.yml' 19 | handlers: 20 | - include: '../handlers/main.yml' 21 | -------------------------------------------------------------------------------- /tests/vars.yml: -------------------------------------------------------------------------------- 1 | --- 2 | role_path: ".." 3 | 4 | nagios_nrpe_server_config: 5 | server_port: 5666 6 | allowed_hosts: 127.0.0.1 7 | command[check_zombie_procs]: /usr/lib/nagios/plugins/check_procs -w 5 -c 10 -s Z 8 | 9 | nagios_nrpe_server_commands: 10 | - name: check_users 11 | line: /usr/lib/nagios/plugins/check_users -w 5 -c 10 12 | - name: check_load 13 | plugin: check_load 14 | args: -w 15,10,5 -c 30,25,20 15 | - name: check_hda1 16 | plugin: check_disk 17 | args: -w 20% -c 10% -p /dev/hda1 18 | sudo: true 19 | nagios_nrpe_server_sudo_commands: 20 | - /usr/lib/nagios/plugins/check_procs 21 | - check_mysql -------------------------------------------------------------------------------- /vars/Debian.yml: -------------------------------------------------------------------------------- 1 | --- 2 | php_fpm_ini_path: /etc/php/{{php_fpm_version}}/fpm/php.ini 3 | php_fpm_config_path: /etc/php/{{php_fpm_version}}/fpm/php-fpm.conf 4 | php_fpm_pool_d: /etc/php/{{php_fpm_version}}/fpm/pool.d 5 | 6 | php_fpm_binary_name: php-fpm{{php_fpm_version}} 7 | php_fpm_service_name: php{{php_fpm_version}}-fpm 8 | 9 | php_fpm_pid_file: /var/run/php/php{{php_fpm_version}}-fpm.pid -------------------------------------------------------------------------------- /vars/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | env: 3 | RUNLEVEL: 1 --------------------------------------------------------------------------------