码迷,mamicode.com
首页 > 其他好文 > 详细

【Ansible】Playbook实例

时间:2017-07-06 18:37:19      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:2.4   ansi   reg   参考   head   aws   sam   create   cat   

Learn to build Ansible playbooks with our guide, one step at a time

技术分享In our previous posts, we introduced Ansible fundamentals, and dove deeper into Ansible playbooks. Now let’s learn to create an Ansible playbook step by step. Working with a playbook, we’ll go from deploying a simple HTML website to a complete LAMP stack.

Deploying Simple HTML Page

To deploy a simple HTML page, we need to ensure that apache is installed and configured on our host machine. So therefore, in this section we will:

  • install Apache
  • start the Apache service
  • deploy a static webpage with images – This static webpage will leverage Ansible templates where it will display the text “Thank you for reading this post. My IP Address is <ip-address-of-instance>” and cloudacademy logo. To fetch the IP address of host, it will rely on Ansible Fact
  • restart Apache once the deployment is over

Before we move forward, let’s have a look at the high-level structure of this simple Ansible playbook.

Lets go through the configuration file line by line and see how configuration works.

hosts – points to Ansible hosts. Here’s a possible syntax:

site.yml – the starting point for executing our Ansible playbook. Includes information about hosts and roles associated with them.

If we want to log into our host machines using a different username and with sudo privileges, we need to use the “remote_user” and “sudo: yes” parameter in our site.yml file. There can be additional parameters too, but they’re not needed right now. Here, we have also defined roles granted to hosts in the [webservers] group.

main.yml (Tasks) – This configuration file defines tasks to be executed on hosts that have webservers roles granted. It looks like:

Since YAML files are so intuitive, we can easily see that this will install and run Apache on host instances and copy certain files and templates to the host’s document root.

main.yml (handlers) – This configuration file defines the action to be performed only upon notification of tasks or state changes. In main.yml (tasks), we defined notify: restart apache handler which will restart Apache once the files and templates are copied to hosts.

index.html.j2 (template) – a file you can deploy on hosts. However, template files also include some reference variables which are pulled from variables defined as part of an Ansible playbook or facts gathered from the hosts. Our index.html.j2 file looks like a regular html webpage with a referenced variable.

We have declared a reference variable “{{ ansible_eth0.ipv4.address }}” which will print the IP address of the host on which this Ansible playbook is executed.

cloud.png (files) – The regular image file to be copied to hosts.

Once we have all the files created and present, we can execute an ansible-playbook command and configure our hosts.

That’s it. We have installed Apache and deployed our webpage using host-based files. On browsing our host’s IP address, we will see our static webpage with the referenced variables value defined.

技术分享

Deploying a PHP webpage configured to work with a MySQL database

So until now, we’ve installed and started Apache, deployed a static webpage, and restarted Apache using handlers. Now we will upgrade the functionality of our existing Ansible playbook by adding additional features. Specifically, we’ll:

  • install php and related packages
  • install mysql server
  • create databases in mysql server
  • grant privileges to databases
  • deploy a php web page which will list the names of all the databases in our mysql server and print certain facts about our host.

This will modify the structure our existing Ansible playbook:

all (group_vars) : contains group-specific variables. Currently, we have only one group i.e., all.

hosts : We have to update our hosts file if the webserver and database server are configured on the same host.

site.yml : Once we have updated our hosts file with a new group “all”, we have to update our site.yml file which will grant the webserver and dbserver role to the “all” host group.

main.yml (tasks for webservers) : This YAML file will now install additional php related packages.

index.php.j2 (templates) : Instead of an html file, we’ve moved to index.php which includes application code to print names of all databases and other operating system related information:

main.yml (tasks for dbservers) : This configuration file will install the mysql-server, and mysql python packages, create databases, and create database users.

That’s it. Our Ansible playbook to deploy a LAMP stack is now ready. We built up a playbook that will install Apache, php, mysql-server, create a mysql user and databases and deploy our application code which prints information about Ansible’s host and list of databases.

To execute this Ansible playbook on host, we will use the ansible-playbook command:

Browsing to our host IP address will display:

技术分享

There’s lots more to learn about Ansible in future posts!

 

参考资料:https://cloudacademy.com/blog/building-ansible-playbooks-step-by-step/

 

【Ansible】Playbook实例

标签:2.4   ansi   reg   参考   head   aws   sam   create   cat   

原文地址:http://www.cnblogs.com/junneyang/p/7127300.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!