Description: Here I have explained, What is Ansible Role? How to install LAMP in Centos using Ansible Role?
What is Ansible Role?
Ansible role is a set of tasks to configure a host to serve a certain purpose like install and configure services. Roles are defined using YAML files with a predefined directory structure. A role directory structure contains directories: defaults, vars, tasks, files, templates, meta, handlers. Each directory must contain a main.yml file that contains relevant content. Below is the description of each directory
- defaults: contains default variables for the role. Variables in default have the lowest priority so they are easy to override.
- vars: contains variables for the role. Variables in vars have higher priority than variables in the defaults directory.
- tasks: contains the main list of steps to be executed by the role.
- files: contains files that we want to be copied to the remote host. We don’t need to specify a path of resources stored in this directory.
- templates: contains file template which supports modifications from the role. We use the Jinja2 templating language for creating templates.
- meta: contains metadata of role like an author, support platforms, dependencies.
- handlers: contains handlers that can be invoked by “notify” directives and are associated with service.
How to Install LAMP [Linux, Apache, MySQL, PHP] in Centos using Ansible?
Apache:
- After creating the role navigate to the task directory under the apache role directory and open main.YAML file [Already created with role] and paste below content to install and start httpd service
--- # tasks file for apache - name: ensure apache is at the latest version yum: name=httpd state=latest - name: ensure service started service: name=httpd state=started
MySQL:
- After creating role crate task and handler file on each respective directories
- First I am going to create task file for MySQL 5.7 as follow
# cd /etc/ansible/roles/MySQL/tasks/
# vi main.yml
--- - name: Install MySQL 5.7 repo yum: name=http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm state=present - name: Install MySQL 5.7 yum: pkg={{ item }} with_items: - mysql-community-server - mysql-community-client - MySQL-python - name: Start the MySQL service service: name=mysqld state=started enabled=true - name: Change mysql root password and keep track in shell: | password_match=`awk '/A temporary password is generated for/ {a=$0} END{ print a }' /var/log/mysqld.log | awk '{print $(NF)}'` echo $password_match mysql -uroot -p$password_match --connect-expired-password -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'P@ssw0rd123'; flush privileges; " echo "[client]" user=root password=PassW0rd > /root/.my.cnf args: creates: /root/.my.cnf register: change_temp_pass notify: restart mysqld - meta: flush_handlers - debug: var: change_temp_pass
- After creating task file, I am going to create handler to restart mysql service as follow
# /etc/ansible/roles/MySQL/handlers
# vi main.yaml
--- # handlers file for mysql - name: restart mysqld service: name=mysqld state=restarted
- Create Role for PHP using ansible-galaxy command
# ansible-galaxy init php - After creating role open task folder and create yaml file to install php as follow
# cd /etc/ansible/roles/php/tasks
# vi main.yml
--- # tasks file for php - name: ensure php is at the latest version yum: name=php state=latest
- After creating all tasks file now creating YAML file for LAMP installation with roles
# vi LAMP.yaml
--- - hosts: all roles: - apache - MySQL - php
- Run YAML file using the ansible-playbook command
Now I am verifying each component on the Destination server
PHP:
No comments:
Post a Comment