{"id":507,"date":"2018-03-14T06:39:42","date_gmt":"2018-03-14T06:39:42","guid":{"rendered":"http:\/\/shijuvarghese.com\/?p=507"},"modified":"2024-02-23T06:04:02","modified_gmt":"2024-02-23T06:04:02","slug":"ansible-playbook-deploy-apache-php-mysql-and-copy-files","status":"publish","type":"post","link":"http:\/\/shijuvarghese.com\/?p=507","title":{"rendered":"Ansible Playbook: Deploy Apache, PhP, MySQL and copy files"},"content":{"rendered":"<p>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.<\/p>\n<p>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.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center;\">*************** *************<\/p>\n<p>Posted below is\u00a0a Playbook to install httpd and mysql in a node via Ansible in a CentOS 7 client node.<\/p>\n<p>In the Ansible server add the destination node&#8217;s IP\/hostname under a newly created group names &#8216;test-server&#8217; in the end of the file \u201c<strong>\/etc\/ansible\/hosts<\/strong>\u201c<\/p>\n<p><em>[&#8216;test-servers&#8217;]<\/em><br \/>\n<em> 192.168.1.6<\/em><\/p>\n<p>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 <a href=\"http:\/\/shijuvarghese.com\/?p=505\">here<\/a>.<\/p>\n<ul>\n<li>Create a file names <em><strong>\/etc\/ansible\/apache_php.yml<\/strong><\/em> with the following content starting with three hyphens as below (IMP: yml scripts are very particular about <i>white-spaces<\/i>. You may encounter issues related to this):<\/li>\n<\/ul>\n<p><strong><em>&#8212;<\/em><\/strong><\/p>\n<p><strong><em>&#8211; name: install apache &amp; php &amp; mysql<\/em><\/strong><br \/>\n<strong><em>\u00a0 \u00a0hosts: test-servers<\/em><\/strong><br \/>\n<strong><em>\u00a0 \u00a0become: true<\/em><\/strong><br \/>\n<strong><em>\u00a0 \u00a0become_user: root<\/em><\/strong><br \/>\n<strong><em>\u00a0 \u00a0 gather_facts: true<\/em><\/strong><\/p>\n<p><strong><em>\u00a0 \u00a0 tasks:<\/em><\/strong><br \/>\n<strong><em>\u00a0 \u00a0 \u00a0 \u00a0 &#8211; name: &#8220;Install apache&#8221;<\/em><\/strong><br \/>\n<strong><em>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 package: name=httpd state=present<\/em><\/strong><br \/>\n<strong><em>\u00a0 \u00a0 \u00a0 \u00a0 &#8211; name: &#8220;Install apache2-php5&#8221;<\/em><\/strong><br \/>\n<strong><em>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0package: name=php state=present<\/em><\/strong><\/p>\n<ul>\n<li>Now let us run the playbook by using the command &#8220;<em><strong>ansible-playbook \/etc\/ansible\/apache_php.yml<\/strong><\/em>&#8220;<\/li>\n<li>The execution of each task and the results will be displayed on screen. The result &#8220;<em>unreachable=0<\/em>&#8221; means all hosts were reachable<\/li>\n<li>Let us run a playbook to create mySQL users and database by using a playbook stored in file &#8220;<em><strong>\/etc\/ansible\/create_db.yml<\/strong><\/em>&#8221; with the following content:<\/li>\n<\/ul>\n<p><em><strong>&#8212;<\/strong><\/em><\/p>\n<p><em><strong>\u00a0 \u00a0 \u00a0&#8211; hosts: all<\/strong><\/em><br \/>\n<em><strong>\u00a0 \u00a0 \u00a0 \u00a0 remote_user: root<\/strong><\/em><\/p>\n<p><em><strong>\u00a0 \u00a0 \u00a0 \u00a0tasks:<\/strong><\/em><br \/>\n<em><strong>\u00a0 \u00a0 \u00a0 \u00a0 &#8211; name: &#8220;Install mySql&#8221;<\/strong><\/em><br \/>\n<em><strong>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 package: name=mariadb-server state=present<\/strong><\/em><br \/>\n<em><strong>\u00a0 \u00a0 \u00a0 \u00a0 &#8211; name: &#8220;Install MySQL-python&#8221;<\/strong><\/em><br \/>\n<em><strong>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 package: name=MySQL-python state=present<\/strong><\/em><\/p>\n<p><em><strong>\u00a0 \u00a0 \u00a0 \u00a0 &#8211; name: Create database user shiju<\/strong><\/em><br \/>\n<em><strong>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 mysql_user: user=shiju password=shiju123 priv=*.*:ALL state=present<\/strong><\/em><\/p>\n<p><em><strong>\u00a0 \u00a0 \u00a0 \u00a0 &#8211; name: Create database edu<\/strong><\/em><br \/>\n<em><strong>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 mysql_db: db=shiju state=present<\/strong><\/em><\/p>\n<p><em><strong>\u00a0 \u00a0 \u00a0 \u00a0 &#8211; name: Create a Table names thenames<\/strong><\/em><br \/>\n<em><strong>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0command: mysql -u shiju -pshiju123 -e &#8216;CREATE TABLE thenames (name varchar(3), <\/strong><\/em><em><strong>mailid varchar(30));&#8217; shiju<\/strong><\/em><\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li>Once complete, access the client node and see if you can see the database, table and the user<\/li>\n<\/ul>\n<ul>\n<li>Let us run a playbook to\u00a0copy a file from the server to a client. The playbook\u00a0&#8220;<em><strong>\/etc\/ansible\/copyfiles.yml<\/strong><\/em>&#8221;\u00a0will have the following content:<\/li>\n<\/ul>\n<p><em><strong>&#8212;<\/strong><\/em><\/p>\n<p><em><strong>\u00a0 \u00a0 \u00a0&#8211; name:\u00a0copy<\/strong><\/em><br \/>\n<em><strong> \u00a0 \u00a0 \u00a0 \u00a0hosts: test-servers<\/strong><\/em><br \/>\n<em><strong> \u00a0 \u00a0 \u00a0 \u00a0become: true<\/strong><\/em><br \/>\n<em><strong> \u00a0 \u00a0 \u00a0 \u00a0become_user: root<\/strong><\/em><br \/>\n<em><strong> \u00a0 \u00a0 \u00a0 \u00a0gather_facts: true<\/strong><\/em><\/p>\n<p><em><strong>\u00a0 \u00a0 \u00a0 \u00a0tasks:<\/strong><\/em><br \/>\n<em><strong> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0&#8211; name: &#8220;copy file&#8221;<\/strong><\/em><br \/>\n<em><strong> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 copy: src=\/root\/index.html dest=\/var\/www\/html\/index.html<\/strong><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>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 <a class=\"mh-excerpt-more\" href=\"http:\/\/shijuvarghese.com\/?p=507\" title=\"Ansible Playbook: Deploy Apache, PhP, MySQL and copy files\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32,15,21,3],"tags":[],"class_list":["post-507","post","type-post","status-publish","format-standard","hentry","category-ansible","category-configuration-management","category-devops","category-linux"],"_links":{"self":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/507","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=507"}],"version-history":[{"count":8,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/507\/revisions"}],"predecessor-version":[{"id":1025,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/507\/revisions\/1025"}],"wp:attachment":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=507"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=507"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}