diff --git a/README.md b/README.md index e0c0213..404efd8 100644 --- a/README.md +++ b/README.md @@ -379,7 +379,8 @@ This is a sample playbook file for deploying the Ansible Galaxy NGINX role in a nginx_type: plus ``` -This is a sample playbook file for deploying the Ansible Galaxy NGINX role in a localhost to install NGINX Plus and the NGINX Controller agent. +This is a sample playbook file for deploying the Ansible Galaxy NGINX role in a localhost to install NGINX Plus and the NGINX Controller agent. Commented out +are sample variables to install the NGINX Controller agent from your NGINX Controller instance instead of the NGINX repository. ```yaml - hosts: localhost @@ -392,8 +393,18 @@ This is a sample playbook file for deploying the Ansible Galaxy NGINX role in a nginx_rest_api_port: 80 nginx_rest_api_write: true nginx_controller_enable: true + nginx_controller_source: repository nginx_controller_api_key: - nginx_controller_api_endpoint: https:///1.4 + nginx_controller_endpoint: # e.g. controller.nginx.com + # nginx_type: plus + # nginx_rest_api_enable: true + # nginx_rest_api_port: 80 + # nginx_rest_api_write: true + # nginx_controller_enable: true + # nginx_controller_source: instance + # nginx_controller_endpoint: controller.nginx.com + # nginx_controller_user_email: john_doe@nginx.com + # nginx_controller_password: password ``` This is a sample playbook file for deploying the Ansible Galaxy NGINX role in a localhost to install NGINX Unit and the PHP/Perl NGINX Unit language modules. diff --git a/defaults/main/controller.yml b/defaults/main/controller.yml index 461df96..0e75768 100644 --- a/defaults/main/controller.yml +++ b/defaults/main/controller.yml @@ -1,8 +1,25 @@ --- -# Install NGINX Controller. -# Use your NGINX Controller API key and NGINX Controller API endpoint. +# Install NGINX Controller Agent. # Requires NGINX Plus and write access to the NGINX Plus REST API. -# Default is null. nginx_controller_enable: false + +# Set the source from where to pull the NGINX Controller Agent. +# Options are 'instance', to pull the NGINX Controller Agent install script +# from your NGINX Controller instance, or 'repository', to pull the NGINX +# Controller Agent from the NGINX repository. +# Default is instance. +nginx_controller_source: instance + +# Set your NGINX Controller Endpoint. Required for both types of NGINX Controller +# installation. +nginx_controller_endpoint: null + +# Set your NGINX Controller API key. +# Required when 'nginx_controller_source' is set to 'repository'. +# Default is null. nginx_controller_api_key: null -nginx_controller_api_endpoint: null + +# Set your NGINX Controller Admin Email and Password. +# Required when 'nginx_controller_source' is set to 'instance'. +nginx_controller_user_email: null +nginx_controller_password: null diff --git a/tasks/controller/install-controller.yml b/tasks/controller/install-controller.yml index 2e818c5..1ff172a 100644 --- a/tasks/controller/install-controller.yml +++ b/tasks/controller/install-controller.yml @@ -1,42 +1,15 @@ --- -- import_tasks: setup-debian.yml - when: ansible_os_family == "Debian" +- import_tasks: setup-controller-instance.yml + when: + - nginx_controller_source == "instance" + - nginx_controller_user_email is defined + - nginx_controller_user_email | length > 0 + - nginx_controller_password is defined + - nginx_controller_password | length > 0 -- import_tasks: setup-redhat.yml - when: ansible_os_family == "RedHat" -- name: "(Install: All OSs) Install NGINX Controller Agent" - package: - name: nginx-controller-agent - state: present - -- name: "(Setup: All OSs) Copy NGINX Controller Agent Configuration Template" - copy: - remote_src: yes - src: /etc/controller-agent/agent.controller.conf.default - dest: /etc/controller-agent/agent.conf - -- name: "(Setup: All OSs) Copy NGINX Configurator Agent Configuration Template" - copy: - remote_src: yes - src: /etc/controller-agent/agent.configurator.conf.default - dest: /etc/controller-agent/agent.configurator.conf - -- name: "(Setup: All OSs) Configure NGINX Controller Agent API Key" - lineinfile: - dest: /etc/controller-agent/agent.conf - regexp: api_key =.* - line: "api_key = {{ nginx_controller_api_key }}" - -- name: "(Setup: All OSs) Configure NGINX Controller Agent API URL" - lineinfile: - dest: /etc/controller-agent/agent.conf - regexp: api_url =.* - line: "api_url = {{ nginx_controller_api_endpoint }}" - -- name: "(Setup: All OSs) Configure NGINX Controller Agent API Hostname" - lineinfile: - dest: /etc/controller-agent/agent.conf - regexp: hostname =.* - line: "hostname = {{ ansible_hostname }}" - notify: "(Handler: All OSs) Start NGINX Controller Agent" +- import_tasks: setup-controller-repository.yml + when: + - nginx_controller_source == "repository" + - nginx_controller_api_key is defined + - nginx_controller_api_key | length > 0 diff --git a/tasks/controller/setup-controller-instance.yml b/tasks/controller/setup-controller-instance.yml new file mode 100644 index 0000000..3156448 --- /dev/null +++ b/tasks/controller/setup-controller-instance.yml @@ -0,0 +1,28 @@ +--- +- name: "(Install: All OSs) Fetch NGINX Controller API Key" + uri: + url: "https://{{ nginx_controller_endpoint }}/sapi/auth/login/" + method: "POST" + body: + email: "{{ nginx_controller_user_email }}" + password: "{{ nginx_controller_password }}" + body_format: json + return_content: yes + status_code: 200 + validate_certs: false + register: controller_return + +- name: "(Install: All OSs) Download the NGINX Controller Agent Installer Script" + get_url: + url: "https://{{ nginx_controller_endpoint }}:8443/1.4/install/controller/" + dest: /tmp/install.sh + validate_certs: no + force: yes + +- name: "(Install: All OSs) Run the NGINX Controller Agent Installer Script" + command: "sh ./install.sh -y" + args: + chdir: /tmp + creates: /var/log/nginx-controller/agent.log + environment: + API_KEY: "{{ controller_return.json.api_key }}" diff --git a/tasks/controller/setup-controller-repository.yml b/tasks/controller/setup-controller-repository.yml new file mode 100644 index 0000000..6ab5d2a --- /dev/null +++ b/tasks/controller/setup-controller-repository.yml @@ -0,0 +1,48 @@ +--- +- name: "(Install: Debian/Ubuntu) Add NGINX Controller Agent Repository" + apt_repository: + filename: nginx-controller + repo: deb https://packages.nginx.org/controller/{{ ansible_distribution | lower }}/ {{ ansible_distribution_release | lower }} controller + +- name: "(Install: CentOS/RedHat) Add NGINX Controller Agent Repository" + yum_repository: + name: nginx-controller + baseurl: https://packages.nginx.org/controller/centos/$releasever/$basearch/ + description: NGINX Controller Agent + gpgcheck: yes + +- name: "(Install: All OSs) Install NGINX Controller Agent" + package: + name: nginx-controller-agent + state: present + +- name: "(Setup: All OSs) Copy NGINX Controller Agent Configuration Template" + copy: + remote_src: yes + src: /etc/controller-agent/agent.controller.conf.default + dest: /etc/controller-agent/agent.conf + +- name: "(Setup: All OSs) Copy NGINX Configurator Agent Configuration Template" + copy: + remote_src: yes + src: /etc/controller-agent/agent.configurator.conf.default + dest: /etc/controller-agent/agent.configurator.conf + +- name: "(Setup: All OSs) Configure NGINX Controller Agent API Key" + lineinfile: + dest: /etc/controller-agent/agent.conf + regexp: api_key =.* + line: "api_key = {{ nginx_controller_api_key }}" + +- name: "(Setup: All OSs) Configure NGINX Controller Agent API URL" + lineinfile: + dest: /etc/controller-agent/agent.conf + regexp: api_url =.* + line: "api_url = https://{{ nginx_controller_endpoint }}/1.4" + +- name: "(Setup: All OSs) Configure NGINX Controller Agent API Hostname" + lineinfile: + dest: /etc/controller-agent/agent.conf + regexp: hostname =.* + line: "hostname = {{ ansible_hostname }}" + notify: "(Handler: All OSs) Start NGINX Controller Agent" diff --git a/tasks/controller/setup-debian.yml b/tasks/controller/setup-debian.yml deleted file mode 100644 index c22bd72..0000000 --- a/tasks/controller/setup-debian.yml +++ /dev/null @@ -1,5 +0,0 @@ ---- -- name: "(Install: Debian/Ubuntu) Add NGINX Controller Agent Repository" - apt_repository: - filename: nginx-controller - repo: deb http://packages.nginx.org/controller/{{ ansible_distribution | lower }}/ {{ ansible_distribution_release | lower }} controller diff --git a/tasks/controller/setup-redhat.yml b/tasks/controller/setup-redhat.yml deleted file mode 100644 index 59e696e..0000000 --- a/tasks/controller/setup-redhat.yml +++ /dev/null @@ -1,7 +0,0 @@ ---- -- name: "(Install: CentOS/RedHat) Add NGINX Controller Agent Repository" - yum_repository: - name: nginx-controller - baseurl: http://packages.nginx.org/controller/centos/$releasever/$basearch/ - description: NGINX Controller Agent - gpgcheck: yes diff --git a/tasks/main.yml b/tasks/main.yml index 0b6cf3e..e30bafd 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -85,10 +85,8 @@ - import_tasks: controller/install-controller.yml when: - nginx_controller_enable | bool - - nginx_controller_api_key is defined - - nginx_controller_api_key | length > 0 - - nginx_controller_api_endpoint is defined - - nginx_controller_api_endpoint | length > 0 + - nginx_controller_endpoint is defined + - nginx_controller_endpoint | length > 0 tags: nginx_install_controller - import_tasks: unit/install-unit.yml