• No results found

Install Apache Solr

In document Ansible for Devops (Page 80-83)

Ubuntu 12.04 includes a package for Apache Solr, but it installs a very old version, so we’ll install the latest version of Solr from source. The first step is downloading the source:

20 - name: Download Solr.

21 get_url:

22 url: http://apache.osuosl.org/lucene/solr/4.9.1/solr-4.9.1.tgz 23 dest: "{{ download_dir }}/solr-4.9.1.tgz"

24 sha256sum: 4a546369a31d34b15bc4b99188984716bf4c0c158c0e337f3c1f98088aec70ee

We’re installing Apache Solr 4.9.1, a recent stable version. When downloading files from remote servers, the get_url module provides more flexibility and convenience than raw wget or curl commands.

You have to passget_urlaurl(the source of the file to be downloaded), and adest(the location where the file will be downloaded). If you pass a directory to the dest parameter, Ansible will place the file inside, but will always re-download the file on subsequent runs of the playbook (and overwrite the existing download if it has changed). To avoid this extra overhead, we give the full path to the downloaded file.

We also usesha256sum, an optional parameter, for peace of mind; if you are downloading a file or archive that’s critical to the functionality and security of your application, it’s a good idea to check the file to make sure it is exactly what you’re expecting.sha256sumcompares a hash of the data in the downloaded file to a 256-bit hash that you specify (useshasum -a 256 /path/to/fileto get the sha256sumof a file). If the checksum doesn’t match the supplied hash, Ansible will fail and discard the freshly-downloaded (and invalid) file.

Chapter 4 - Ansible Playbooks 62 25 - name: Expand Solr.

26 command: >

27 tar -C /tmp -xvzf {{ download_dir }}/solr-4.9.1.tgz 28 creates={{ download_dir }}/solr-4.9.1/dist/solr-4.9.1.war 29

30 - name: Copy Solr into place.

31 command: >

32 cp -r {{ download_dir }}/solr-4.9.1 {{ solr_dir }}

33 creates={{ solr_dir }}/dist/solr-4.9.1.war

We need to expand the Apache Solr archive, then copy it into place. For both of these steps, use the built-intarandcputilities (with the appropriate options) to do the work. Settingcreatestells Ansible to skip these steps in subsequent runs, since the Solr war file will already be in place.

34 # Use shell so commands are passed in correctly.

35 - name: Copy Solr components into place.

36 shell: >

37 cp -r {{ item.src }} {{ item.dest }}

38 creates={{ item.creates }}

39 with_items:

40 # Solr example configuration and war file.

41 - {

51 # Solr log4j logging configuration.

52 - {

The next task copies into place certain directories and files required to run Apache Solr.

Nothing too special here, but this example illustrates the use of comments withinwith_itemslists to help clarify the items in the list. We could’ve added each command as its own task, but doing it this way reduces the total number of Ansible tasks and allows us to move thewith_itemslist to an external variable if desired.

63 - name: Ensure solr example directory is absent.

64 file:

65 path: "{{ solr_dir }}/example"

66 state: absent 67

68 - name: Set up solr data directory.

69 file:

70 path: "{{ solr_dir }}/data"

71 state: directory 72 owner: tomcat7 73 group: tomcat7

The latest version of Apache Solr searches through all the directories inside {{ solr_dir }}

recursively, loading any potential search configuration it finds. Since we copied over one of the examples to use as the server’s default search core, Solr would see it as a duplicate of one of the examples and crash. So, we use thefilemodule with apathto the example directory to make sure the directory is gone (state=absent).

After removing the example directory (and in future runs, ensuring it’s still gone), we set up the data directory where Solr will store index data, ensuring it exists as a directory and is owned by the tomcat7user and group.

73 - name: Configure solrconfig.xml for new data directory.

74 lineinfile:

75 dest: "{{ solr_dir }}/collection1/conf/solrconfig.xml"

76 regexp: "^.*<dataDir.+$"

77 line: "<dataDir>${solr.data.dir:{{ solr_dir }}/data}</dataDir>"

78 state: present

As we found earlier, lineinfile is a helpful module for ensuring consistent configuration file settings with idempotence. In this case, we need to make sure the <dataDir> line in our default search core’s configuration file is set to a specific value.

Chapter 4 - Ansible Playbooks 64 79 - name: Set permissions for solr home.

80 file:

81 path: "{{ solr_dir }}"

82 recurse: yes 83 owner: tomcat7 84 group: tomcat7

To set ownership options on the entire contents of the{{ solr_dir }}correctly, we use thefile module with therecurseparameter set toyes. This is equivalent to the shell commandchown -R tomcat7:tomcat7 {{ solr_dir }}.

84 - name: Add Catalina configuration for solr.

85 template:

86 src: templates/solr.xml.j2

87 dest: /etc/tomcat7/Catalina/localhost/solr.xml 88 owner: root

89 group: tomcat7 90 mode: 0644

91 notify: restart tomcat

The final task copies a template file (solr.xml.j2) to the remote host, substituting variables via Jinja2 syntax, and sets the file’s ownership and permissions as needed for Tomcat.

Before the task runs, the local template file will need to be created. Create a ‘templates’ folder in the same directory as your Apache Solr playbook, and create a new file namedsolr.xml.j2inside, with the following contents:

1 <?xml version="1.0" encoding="utf-8"?>

2 <Context docBase="{{ solr_dir }}/solr.war" debug="0" crossContext="true">

3 <Environment name="solr/home" type="java.lang.String" \ 4 value="{{ solr_dir }}" override="true"/>

5 </Context>

Run the playbook with $ ansible-playbook [playbook-name.yml], and after a few minutes (depending on your server’s Internet connection speed), you should be able to access the Solr admin interface at http://example.com:8080/solr (where ‘example.com’ is your server’s hostname or IP address).

In document Ansible for Devops (Page 80-83)