Ansible (10) first exercise and ansible first exercise
Summary
We have learned apt, yum, zypper, copy, and other modules. You must be bored.
The following is a practical example.
Actual case:
- Batch tomcat configuration
Only the knowledge of the above modules is enough for us to orchestrate a Playbook to configure a large number of physical/virtual servers.
Actual scenarios
A company has applied for 100 cloud computing virtual machines and needs to build an environment. First, let's talk about how to build the environment. First, let's focus on setting up the environment: tomcat installation and configuration.
Step Analysis
Tomcat installation is simple and everyone understands it.
1. Install jdk
2. Create tomcat users
3. Install javasart
4. Configure tomcat and restart.
So how do we write playbook?
1. Define Hosts
2. Define roles
3. Define vars
4. orchestrate playbook
Now that we have all analyzed this, can we start? The following playbook can be found in my csdn code library.
Define Host
Create a new hosts file. Remember the file name.
[tomcat-servers]webserver1webserver2webserver3
This defines the host. Here, xx. xx can be equivalent to localhost, 127.0.0.1, 192.168.1.1, and so on. You can specify multiple server ip addresses under the jdk label.
Define roles
Create a new site. yml file. Of course, the name can be defined by yourself.
---# This playbook deploys a simple standalone Tomcat 7 server. - hosts: tomcat-servers user: root roles: - tomcat
This defines the first simplest main "function" -- site. yml of the playbook. It tells ansible to get all servers under the jdk label in the hosts file to perform the actions defined by jdk roles as root.
Define var
Since it is an orchestration, we certainly hope that the jdk installation location or version information can be customized, right?
# Here are variables related to the Tomcat installationhttp_port: 8080https_port: 8443# This will configure a default manager-gui user:admin_username: adminadmin_password: adminsecret
This defines vars, which is located in group_vars/tomcat-servers. tomcat-servers must be consistent with the labels in hosts. Otherwise, vars is not found by default.
Orchestrate playbook
---- name: Install Java 1.7 yum: name=java-1.7.0-openjdk state=present- name: add group "tomcat" group: name=tomcat- name: add user "tomcat" user: name=tomcat group=tomcat home=/usr/share/tomcat sudo: True- name: delete home dir for symlink of tomcat shell: rm -fr /usr/share/tomcat sudo: True- name: Download Tomcat get_url: url=http://www.us.apache.org/dist/tomcat/tomcat-7/v7.0.55/bin/apache-tomcat-7.0.55.tar.gz dest=/opt/apache-tomcat-7.0.55.tar.gz- name: Extract archive command: chdir=/usr/share /bin/tar xvf /opt/apache-tomcat-7.0.55.tar.gz -C /opt/ creates=/opt/apache-tomcat-7.0.55- name: Symlink install directory file: src=/opt/apache-tomcat-7.0.55 path=/usr/share/tomcat state=link- name: Change ownership of Tomcat installation file: path=/usr/share/tomcat/ owner=tomcat group=tomcat state=directory recurse=yes- name: Configure Tomcat server template: src=server.xml dest=/usr/share/tomcat/conf/ notify: restart tomcat- name: Configure Tomcat users template: src=tomcat-users.xml dest=/usr/share/tomcat/conf/ notify: restart tomcat- name: Install Tomcat init script copy: src=tomcat-initscript.sh dest=/etc/init.d/tomcat mode=0755- name: Start Tomcat service: name=tomcat state=started enabled=yes- name: deploy iptables rules template: src=iptables-save dest=/etc/sysconfig/iptables notify: restart iptables- name: wait for tomcat to start wait_for: port={{http_port}}
Just follow the above arrangement. Some of the modules have not been introduced before, as a subject for your own research :).
The playbook above is located in roles/tomcat/tasks/main. yml.
Playbook attachment
In the above playbook, there are templates and file modules.
Both are used to copy the configuration file from the local device to the remote node. The difference is described in later chapters.
The template configuration file is in roles/templates.
The file configuration file is in roles/files.
Handler, which starts with policy, is also introduced in later chapters.
---- name: restart tomcat service: name=tomcat state=restarted- name: restart iptables service: name=iptables state=restarted
The handler above is located in roles/handlers/main. yml.
This completes the deployment of tomcat. You can also define the node and tomcat-manager Administrator password :).
This chapter is somewhat advanced, but does not prevent you from understanding ansible.