Ansible Playbook: Deploy Apache, PhP, MySQL and copy files

Ansible_Logo

Ansible Playbook comprises of one or more plays, which in turn consists tasks. The Playbook consists of Modules, APIs and Plugins. Modules consists of the Tasks to be executed. APIs are used for CLI access to the Playbook. Plugins are pieces of code used for carrying our certain task. For example a Connection Plugin can be used to integrate Ansible with Kerberos authentication.

The playbook written in Yaml language. This document starts with three hyphens on top, then contains Hosts list, Variables, Tasks to be executed, in the same order it is listed, and Handlers to execute tasks.

 

 

*************** *************

Posted below is a Playbook to install httpd and mysql in a node via Ansible in a CentOS 7 client node.

In the Ansible server add the destination node’s IP/hostname under a newly created group names ‘test-server’ in the end of the file “/etc/ansible/hosts

[‘test-servers’]
192.168.1.6

To make life simple ensure you can SSH to 192.168.1.6 from the server without a password. Tutorial to enable password less authentication can be found here.

  • Create a file names /etc/ansible/apache_php.yml with the following content starting with three hyphens as below (IMP: yml scripts are very particular about white-spaces. You may encounter issues related to this):

– name: install apache & php & mysql
   hosts: test-servers
   become: true
   become_user: root
    gather_facts: true

    tasks:
        – name: “Install apache”
          package: name=httpd state=present
        – name: “Install apache2-php5”
           package: name=php state=present

  • Now let us run the playbook by using the command “ansible-playbook /etc/ansible/apache_php.yml
  • The execution of each task and the results will be displayed on screen. The result “unreachable=0” means all hosts were reachable
  • Let us run a playbook to create mySQL users and database by using a playbook stored in file “/etc/ansible/create_db.yml” with the following content:

     – hosts: all
        remote_user: root

       tasks:
        – name: “Install mySql”
          package: name=mariadb-server state=present
        – name: “Install MySQL-python”
          package: name=MySQL-python state=present

        – name: Create database user shiju
          mysql_user: user=shiju password=shiju123 priv=*.*:ALL state=present

        – name: Create database edu
          mysql_db: db=shiju state=present

        – name: Create a Table names thenames
         command: mysql -u shiju -pshiju123 -e ‘CREATE TABLE thenames (name varchar(3), mailid varchar(30));’ shiju

 

  • Once complete, access the client node and see if you can see the database, table and the user
  • Let us run a playbook to copy a file from the server to a client. The playbook “/etc/ansible/copyfiles.yml” will have the following content:

     – name: copy
       hosts: test-servers
       become: true
       become_user: root
       gather_facts: true

       tasks:
         – name: “copy file”
            copy: src=/root/index.html dest=/var/www/html/index.html