Deploying and managing NXLog with Ansible

Ansible has become an industry standard when it comes to configuring and managing servers. As a configuration management tool, it carries the burden of simplifying system administration tasks, such as installing and updating software packages, and infrastructure provisioning. In this post, we will create an Ansible playbook that will enable us to automate the installation and configuration of NXLog across multiple endpoints. Whether you need only a single endpoint today or thousands of endpoints next week, Ansible will do the heavy lifting for you.

Why should I use Ansible to deploy NXLog?

One of the biggest challenges that any IT department faces today is large scale workload deployment. Some organizations require the deployment of hundreds of thousands of servers to meet their computing needs, which will constantly log events for forwarding to a collection server, cluster of relay servers, or directly to a SIEM. Given such vast numbers of endpoints that will use NXLog as their logging solution, there is a clear need for an automated solution like Ansible, capable of concurrently installing and configuring nodes.

To meet this challenge, we will use an Ansible playbook that can connect to any number of server IPs we provide, and then upload the required NXLog configuration file.

To install Ansible, please refer to Ansible Installation Docs for detailed instructions based on your operating system.

Writing your Ansible playbook

An Ansible playbook is a predefined set of instructions written in the YAML programming language that are executed on the remote servers to be managed. Under the hood, an SSH connection is established with the remote servers where bash commands are run.

Before you start writing your playbook, a few prerequisites have to be met:

  • A functional Ansible installation on your local machine (the control node) with an SSH client and a public-private key pair.

  • A provisioned set of servers (the target nodes), each having a known, accessible IP address or domain name, and each running an operational SSH server with key based authentication.

  • Root access on all servers (the target nodes).

At the very first step, we need a directory and file structure to work in. The below command will create a directory named ansible with a config subdirectory, with the three configuration files: playbook.yml, vars.yml, and inventory being in the same level as the config subdirectory. In addition, the script also creates the rhel.conf, ubuntu.conf, and windows.conf files in the config subdirectory. It then changes our current working directory to the ansible directory.

$ mkdir -p ansible; touch ansible/playbook.yml; touch ansible/vars.yml; touch ansible/inventory; mkdir -p ansible/config; cd ansible; touch config/{windows,ubuntu,rhel}.conf;

Once we have the directory structure, it is time to add content to the files. There are three of them to start with. We gave them real content and the content is explained after the samples.

Example 1. inventory
1
2
3
4
[nxlog_servers]
ubuntu ansible_ssh_host=192.168.1.10
rhel ansible_ssh_host=192.168.1.11
windows ansible_ssh_host=192.168.12

inventory is merely a file that groups our servers into categories. In our case, we’ll have single category called nxlog_servers.

Example 2. vars.yaml
ubuntu_focal_url: https://nxlog.co/system/files/products/files/348/nxlog-ce_3.0.2272_ubuntu_focal_amd64.deb
rhel_8_url: https://nxlog.co/system/files/products/files/348/nxlog-ce-3.0.2272_rhel8.x86_64.rpm
windows_url: https://nxlog.co/system/files/products/files/348/nxlog-ce-3.0.2272.msi

As can be seen in the YAML file above, each file’s location expressed as a URL is assigned to a YAML key. These keys can then be used, e.g. {{ rhel_8_url }}, instead of the long URLs in playbook.yml below to make it easier to read and maintain.

Example 3. playbook.yaml
---
- hosts: nxlog_servers
  vars_files:
    - vars.yml
  become: true
  become_user: root

  tasks:
    - name: Install NXLog on Ubuntu Focal
      apt:
        deb: "{{ ubuntu_focal_url }}"
      when: "ansible_distribution == 'Ubuntu' and ansible_distribution_release == 'focal'"

    - name: Copy a new "ubuntu.conf" file into the NXLog default config directory
      copy:
        src: config/ubuntu.conf
        dest: /opt/nxlog/etc/nxlog.d/ubuntu.conf
        owner: nxlog
        group: nxlog
        mode: '0755'
      when: "ansible_distribution == 'Ubuntu' and ansible_distribution_release == 'focal'"

    - name: Start NXLog on Ubuntu Focal
      command: service nxlog start
      when: "ansible_distribution == 'Ubuntu' and ansible_distribution_release == 'focal'"

#=============================================================================================================#
    - name: Install NXLog on RHEL 8 (Centos 8)
      yum:
        name="{{ rhel_8_url }}"
      when: "ansible_distribution == 'RedHat' and ansible_distribution_major_version == '8'"

    - name: Copy a new "rhel.conf" file to the NXLog default config directory
      copy:
        src: config/rhel.conf
        dest: /opt/nxlog/etc/nxlog.d/rhel.conf
        owner: nxlog
        group: nxlog
        mode: '0755'
      when: "ansible_distribution == 'RedHat' and ansible_distribution_major_version == '8'"

    - name: Start NXLog for RHEL 8 (Centos 8)
      command: systemctl start nxlog
      when: "ansible_distribution == 'RedHat' and ansible_distribution_major_version == '8'"

#==============================================================================================================#
    - name: Install NXLog on Windows
      win_package:
        path: "{{ windows_url }}"
        state: present
      when: ansible_distribution == 'Windows'

    - name: Copy a new "windows.conf" file into the NXLog default config directory
      copy:
        src: config/windows.conf
        dest: C:\Program Files\nxlog\conf\nxlog.d\windows.conf
        owner: nxlog
        group: nxlog
        mode: '750'
      when: ansible_distribution == 'Windows'

    - name: Set nxlog service startup mode to auto and ensure it's started.
      win_service:
        name: nxlog
        state: restarted

One very useful feature of Ansible’s copy plugin, is that it always compares the checksum of the src (local) file with the checksum of the dest (remote) file. The transfer will only be initiated if the checksums are different. These three configuration files are pretty much all that Ansible needs to deploy NXLog agents to your remote servers. The next order of business is to appropriately create a configuration file for each operating system. To achieve this, we created three configuration files, one for each type of server.

The Playbook schema

Even though it is not the goal of this post to be an Ansible User Guide, we thought it is worth sharing the explanation of the playbook schema. It could help you better understand the structure of the file.

hosts

Defines which remote servers are target nodes and will be accessed remotely for performing the tasks defined in the tasks section.

vars_files

Files containing custom-defined variables that the playbook can use. Multiple "var" files can be declared. As you may have noticed by now from our vars.yml, variables we declared in it are enclosed in double curly brackets, which are enclosed in double-quotes. This is Ansible’s syntax for expanding a variable to its assigned value within the playbook.

become

This instructs Ansible that it will be using a username other than the one that is associated with the SSH key-pair. In the playbook presented above, the SSH user is root. This is required because NXLog is not installed in the regular user’s home directory. By default, regular users typically don’t have write permissions beyond their home directories.

become_user

This is the user that was just discussed above: in our case root. Depending on the task, it might be different.

tasks

A list of the tasks that will be executed on the target nodes.

Each task has multiple keys:

  • name: The name or description of the task.

  • apt: Used to invoke the Debian/Ubuntu native package manager that installs, upgrades, or removes a program.

  • deb: Path to a .deb package. Can be a URL or a local file. If:// in the path, Ansible will attempt to download deb before installing.

  • command: The command to run on the remote machine. Alternatively, cmd: | can be used to execute multiple commands instead.

  • yum: Used to invoke the RHEL native package manager that installs, upgrades, or removes a software package.

  • win_package: Used to invoke the Windows Installer, that installs, upgrades, or removes a program.

  • when: Is used to define basic conditionals. It’s similar to if-else which is used in many programming languages.

Playbook tasks

By running the playbook above, we are taking advantage of Ansible’s automation capabilities which will perform three tasks for each operating system. This is what exactly happens:

  1. Deploying the correct, platform-appropriate NXLog installation package.

  2. Copying the appropriate NXLog configuration file to its appropriate remote node.

  3. Restarting NXLog to load the newly deployed configuration.

Now that we have finished writing our Ansible Playbook and we clear about what it will do, we can write our NXLog configuration file for each server.

NXLog configuration files

The in-depth explanation of the NXLog configuration files are out of the scope of this article, yet we are using real examples, so it is worth showing them. Do not forget, these files were created earlier in the, we are just giving content here. The are in the /ansible/config directory.

Example 4. Windows NXLog configuration

This example configuration collect logs from Windows Event Log using the im_msvistalog module, then sends them to another node using om_tcp.

windows.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<Extension syslog>
    Module    xm_syslog
</Extension>

<Input eventlog>
    Module    im_msvistalog
    <QueryXML>
        <QueryList>
            <Query Id='0'>
                <Select Path='Application'>*</Select>
                <Select Path='Security'>*[System/Level&lt;4]</Select>
                <Select Path='System'>*</Select>
            </Query>
        </QueryList>
    </QueryXML>
</Input>

<Output tcp>
    Module    om_tcp
    Host      192.168.1.12:514
    Exec      to_syslog_bsd();
</Output>

<Route eventlog_to_tcp>
    Path      eventlog => tcp
</Route>
Example 5. Ubuntu NXLog configuration

This example configuration uses the im_systemd module to read log messages from the Linux systemd journal, then sends them to another node using om_tcp.

ubuntu.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
<Input systemd>
    Module    im_systemd
</Input>

<Output tcp>
    Module    om_tcp
    Host      192.168.1.10:514
    Exec      to_syslog_bsd();
</Output>

<Route systemd_to_tcp>
    Path      systemd => tcp
</Route>
Example 6. RHEL NXLog configuration

This example configuration uses the im_file module to read log messages from the Linux file system, then sends them to another node using om_tcp.

rhel.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<Input file>
    Module  im_file
    File    "/var/log/messages"
</Input>

<Output tcp>
    Module    om_tcp
    Host      192.168.1.11:514
    Exec      to_syslog_bsd();
</Output>

<Route file_to_tcp>
    Path      file => tcp
</Route>

Playbook execution

To begin executing all tasks described in the Ansible Playbook, execute the command:

$ ansible-playbook -i inventory -u your_user_name --ask-become-pass --private-key ~/.ssh/ansible_id_ed25519  playbook.yml

Let’s take a closer look at the following options that we used in the command above:

  • -i: The name of the inventory file your playbook will use for logging in to the servers

  • -u: The username the playbook will be using

  • -ask-become-pass: The user’s password to execute commands with sudo privileges.

  • -private-key: The private key location you have already created. This example is using the ed25519 algorithm, since it performs much faster and provides the same level of security with significantly smaller keys.

Conclusion

In this post, we explored a practical example of how to deploy and manage NXLog to any number of servers using Ansible. One of the biggest advantages of NXLog is that it’s a cloud-agnostic log collection software, making it an ideal choice for deployment to your current cloud infrastructure. With Ansible, any changes to your NXLog configurations can be easily and instantly applied to an unlimited number of target nodes.

GET STARTED TODAY:
Disclaimer

While we endeavor to keep the information in this topic up to date and correct, NXLog makes no representations or warranties of any kind, express or implied about the completeness, accuracy, reliability, suitability, or availability of the content represented here.

The accurateness of the content was tested and proved to be working in our lab environment at the time of the last revision with the following software versions:

Windows 10, Ubuntu 20.04.4 LTS, RHEL 8
NXLog CE 3.0.2272
NXLog EE 5.4.7313

Last revision: 3 March 2022

NXLog Ltd. develops multi-platform log collection tools that support many different log sources, formats, transports, and integrations. The tools help administrators collect, parse, and forward logs so they can more easily respond to security issues, investigate operational problems, and analyze event data. NXLog distributes the free and open source NXLog Community Edition and offers additional features and support with the NXLog Enterprise Edition.

This document is provided for informational purposes only and is subject to change without notice. Trademarks are the properties of their respective owners.

Download a fully functional trial of the Enterprise Edition for free