Monday, July 17, 2017

Ansible - Playbook to setup LAMP server


  • Purpose of this playbook is to setup LAMP server using Ansible.
  • To understand ARRAY's use in Ansible please refer to first block.
  • with_item is another example of loops. It can be mainly used where we can't use Array. Service is one of the module for example which does not support array.
  • You need to change "hosts" names only to use this playbook in your environment to setup LAMP server.  

#Copy from here:
---
- name: Setup LAMP server
  hosts: clientmachine ## change the name by your target server
  tasks:

## First of all we need to install all required packages. All packages are listed under YUM module using ARRAY feature instead writing same line of code multiple times for each package.
  - block:
    - name: Install all Packages - firewalld, httpd, php, php-mysql, and mariadb-server
      yum:
        name:
          - firewalld
          - httpd
          - php
          - php-mysql
          - mariadb-server
        state: latest

## Post installation of packages now we need to start services and enable these services to start automatically at next boot.
  - block:
    - name: Start services - firewalld, httpd and mariadb-server
      service:
        name: "{{ item }}"
        state: started
        enabled: yes
      with_items:
        - firewalld
        - httpd
        - mariadb

## Configure firewall to all Web service access from outside
  - block:
    - name: Allow Web service access from outside
      firewalld: service=http permanent=true state=enabled immediate=true

## Deploy test Web page. You can use either above indentation and format or the below one.
  - block:
    - name: Test Web page
      copy:
        content: "This is test page"
        dest: /var/www/html/index.php
        mode: 0644

    - name: Reload Web service
      service: name=httpd state=reloaded

## Test whether the web page is accessible from outside or not
- name: Test Web server access from outside
  hosts: controller ## change the name by your controller server
  tasks:

  - name: Browse default web page
    uri:
      url: http://node11.example.com/index.php
      status_code: 200
...

2 comments:

Unknown said...
This comment has been removed by the author.
Unknown said...

Verify the syntax of above playbook before running it in your environment by using below command:

To check the syntax:
ansible-playbook --syntax-check nameoftheplaybook.yml


To dry run:
ansible-playbook -C nameoftheplaybook.yml