Arch Linux BasedManjaro

How To Install Ansible on Manjaro

Install Ansible on Manjaro

Automation has become a critical aspect of modern system administration, allowing IT professionals to streamline repetitive tasks and maintain consistency across multiple systems. Ansible stands out as one of the most popular automation tools due to its simplicity and effectiveness. If you’re using Manjaro, an Arch Linux-based distribution known for its user-friendly approach, installing Ansible can significantly enhance your system management capabilities. This comprehensive guide will walk you through the process of installing Ansible on Manjaro, from preparation to configuration and basic usage.

What is Ansible and Why Use It?

Ansible is an open-source automation platform that simplifies IT infrastructure management, application deployment, and task automation. Unlike other configuration management tools, Ansible operates in an agentless fashion, meaning you don’t need to install any special software on managed nodes. It uses SSH for secure connections and executes tasks defined in YAML-based files called playbooks.

Key Features of Ansible:

  • Agentless architecture that reduces overhead on managed systems
  • Simple YAML syntax that is easy to read and write
  • Powerful automation capabilities for configuration management
  • Extensive module library for various tasks and platforms
  • Cross-platform compatibility, supporting Linux, Windows, and cloud environments

For Manjaro users specifically, Ansible provides several advantages. The tool integrates seamlessly with Arch-based systems, allowing for efficient management of pacman packages, AUR installations, and system configurations. Whether you’re managing a single Manjaro workstation or multiple servers, Ansible can help you maintain consistency and reduce manual intervention.

Compared to alternatives like Chef or Puppet, Ansible has a significantly lower learning curve while still offering robust automation capabilities. Its straightforward approach to automation makes it an excellent choice for both beginners and experienced system administrators.

Prerequisites for Installing Ansible on Manjaro

Before proceeding with the installation, ensure that your system meets the following requirements:

  • A Manjaro Linux installation (any edition)
  • Administrative (sudo) privileges on your system
  • Basic knowledge of terminal commands
  • Internet connectivity for downloading packages
  • Python 3.x (comes pre-installed on Manjaro)

While Ansible doesn’t have significant hardware requirements, having at least 2GB of RAM and sufficient disk space for storing playbooks and related files is recommended. Since Ansible uses SSH for connections, make sure your system has proper network connectivity, especially if you’re planning to manage remote hosts.

Preparing Your Manjaro System

The first step toward installing Ansible is to ensure your Manjaro system is up-to-date. This reduces the chance of compatibility issues and ensures you’re working with the latest package versions.

Start by refreshing your pacman mirrors to ensure you’re using the fastest and most reliable package sources:

sudo pacman-mirrors -f

Next, update your system packages:

sudo pacman -Syu

This command synchronizes the package database and upgrades all installed packages to their latest versions. It’s crucial to perform this step before installing any new software on Manjaro.

Additionally, you might want to install git and other utility tools that complement Ansible:

sudo pacman -S git xclip --noconfirm

Git will be useful for version control of your Ansible playbooks, while xclip helps with managing SSH keys for remote connections.

Installing Ansible on Manjaro Using Pacman

Manjaro, being based on Arch Linux, uses the pacman package manager. Installing Ansible through pacman is straightforward and ensures proper integration with your system.

To install Ansible, open your terminal and run:

sudo pacman -S ansible

This command will download and install the ansible package along with its dependencies. When prompted, confirm the installation by pressing ‘Y’.

The package manager will automatically handle all dependencies, including python modules required by Ansible. The installation typically completes within a few minutes, depending on your internet connection speed.

If you want to install specific components, you can also choose to install just ansible-core, which provides the base functionality without the additional collections:

sudo pacman -S ansible-core

For linting capabilities that help identify issues in your playbooks, you can add ansible-lint:

sudo pacman -S ansible-lint

These commands ensure you have the necessary tools for both running and developing Ansible playbooks on your Manjaro system.

Alternative Installation Methods

While pacman is the recommended method for installing Ansible on Manjaro, there are alternative approaches that might be better suited for specific use cases.

Installing via Python pip:

If you require a specific version of Ansible not available in the Manjaro repositories, or if you want to install it in a virtual environment, you can use pip:

# Install pip if not already installed
sudo pacman -S python-pip

# Install Ansible using pip
pip install --user ansible

Using pip allows for installation in a user’s home directory without affecting system packages. This is particularly useful for testing different Ansible versions or when working in development environments.

Using Virtual Environments:

For isolated environments, you can install Ansible in a Python virtual environment:

# Install virtualenv
sudo pacman -S python-virtualenv

# Create and activate a virtual environment
virtualenv ansible-env
source ansible-env/bin/activate

# Install Ansible within the environment
pip install ansible

This approach prevents potential conflicts with system packages and allows you to maintain multiple Ansible versions simultaneously.

Each installation method has its advantages and considerations. The pacman method ensures system integration and automatic updates, while pip and virtual environments offer greater flexibility and version control.

Verifying Your Ansible Installation

After installing Ansible, it’s important to verify that it’s working correctly. The simplest way to do this is by checking the version:

ansible --version

This command should display information about your Ansible installation, including:

  • The installed version of Ansible
  • Python version being used
  • Configuration file location
  • Configured module search path
  • Default ansible.cfg file location

For example, you might see output similar to:

ansible [core 2.14.0]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/user/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.10/site-packages/ansible
  python version = 3.10.5 (main, Jun 6 2022, 18:58:36) [GCC 12.1.0]
  jinja version = 3.1.2

Additionally, you can run a simple command to test Ansible’s functionality:

ansible localhost -m ping

This command uses the “ping” module to test connectivity to the local machine. If successful, you should see a response indicating “SUCCESS” and “ping”: “pong”.

Configuring Ansible After Installation

Once Ansible is installed, you’ll need to configure it for your specific environment. The main configuration file is ansible.cfg, which can be located in several places:

  1. The file specified by the ANSIBLE_CONFIG environment variable
  2. ./ansible.cfg (in the current directory)
  3. ~/.ansible.cfg (in the home directory)
  4. /etc/ansible/ansible.cfg (the system-wide configuration)

For most users, creating a configuration file in their project directory or home directory is sufficient. Here’s a basic ansible.cfg template to get started:

[defaults]
inventory = ./inventory
remote_user = your_username
host_key_checking = False

The inventory file defines the hosts that Ansible will manage. Create a simple inventory file:

[local]
localhost ansible_connection=local

[servers]
server1 ansible_host=192.168.1.10
server2 ansible_host=192.168.1.11

This configuration allows you to organize your managed hosts into groups and specify connection details. For Manjaro-specific setups, you might want to create a group for all your Manjaro machines to apply consistent configurations.

Setting Up SSH for Remote Management

Since Ansible uses SSH for remote connections, setting up proper SSH authentication is crucial for seamless automation.

Generating SSH Keys:

If you don’t already have an SSH key pair, generate one:

ssh-keygen -t ed25519 -C "your_email@example.com"

Follow the prompts to complete the key generation process. For enhanced security, use a strong passphrase.

Distributing Your Public Key:

To enable passwordless authentication, copy your public key to each remote host:

ssh-copy-id username@remote_host

Alternatively, you can use Ansible itself to distribute your SSH keys once you have initial password access:

ansible all -m authorized_key -a "user=username key='{{ lookup('file', '~/.ssh/id_ed25519.pub') }}'" --ask-pass

Testing SSH Connectivity:

Verify that you can connect to your managed hosts without a password:

ssh username@remote_host

Proper SSH configuration eliminates the need for password prompts during Ansible operations, making automation truly hands-free.

Creating Your First Ansible Playbook

Playbooks are Ansible’s configuration, deployment, and orchestration language. They describe a policy you want your remote systems to enforce, using a simple YAML format.

Let’s create a basic playbook that updates a Manjaro system:

---
- name: Update Manjaro System
  hosts: manjaro_hosts
  become: yes
  tasks:
    - name: Refresh mirrors
      command: pacman-mirrors -f

    - name: Update package cache
      pacman:
        update_cache: yes

    - name: Upgrade all packages
      pacman:
        upgrade: yes

Save this file as update_manjaro.yml. To run the playbook:

ansible-playbook update_manjaro.yml

This simple playbook demonstrates the basic structure:

  • A play definition with a name and target hosts
  • The become: yes directive to use sudo privileges
  • A list of tasks to execute sequentially

Understanding YAML syntax is important when writing playbooks. Remember that indentation matters, and all items at the same level should have the same indentation.

Advanced Ansible Usage with Manjaro

After mastering the basics, you can leverage Ansible for more advanced Manjaro-specific tasks.

Managing AUR Packages:

Manjaro users often need to install packages from the Arch User Repository (AUR). You can automate this using the community.general.pacman module:

- name: Install AUR helper
  pacman:
    name: yay
    state: present

- name: Install AUR packages
  community.general.aur:
    name:
      - visual-studio-code-bin
      - google-chrome
    use: yay

To use AUR-related modules, you’ll need to install the community.general collection:

ansible-galaxy collection install community.general

System Configuration Automation:

Create playbooks for consistent system configuration:

- name: Configure Manjaro Desktop
  hosts: workstations
  become: yes
  tasks:
    - name: Set up GNOME preferences
      dconf:
        key: "/org/gnome/desktop/interface/enable-animations"
        value: "false"

    - name: Configure pacman settings
      lineinfile:
        path: /etc/pacman.conf
        regexp: "^#Color"
        line: "Color"

These examples demonstrate how Ansible can handle both package management and system configuration tasks specific to Manjaro.

Ansible Playbooks for Manjaro Configuration

Creating dedicated playbooks for Manjaro configuration helps maintain consistency across multiple systems. Here are some examples of useful playbooks for Manjaro environments.

Desktop Environment Configuration:

---
- name: Configure Manjaro Desktop Environment
  hosts: desktops
  become: yes
  tasks:
    - name: Install GNOME desktop packages
      pacman:
        name:
          - gnome
          - gnome-tweaks
          - gnome-shell-extensions
        state: present

    - name: Set default desktop session
      copy:
        content: "[Desktop]\nSession=gnome\n"
        dest: /etc/sddm.conf.d/default.conf
        mode: '0644'

Development Environment Setup:

---
- name: Set up Development Environment
  hosts: dev_machines
  become: yes
  tasks:
    - name: Install development tools
      pacman:
        name:
          - base-devel
          - git
          - docker
          - visual-studio-code
          - nodejs
          - npm
        state: present

    - name: Enable and start Docker service
      systemd:
        name: docker.service
        enabled: yes
        state: started

    - name: Add user to Docker group
      user:
        name: "{{ ansible_user }}"
        groups: docker
        append: yes

These playbooks can be further customized to match your specific requirements and preferences. The modular nature of Ansible allows you to include or exclude tasks as needed.

Troubleshooting Common Installation Issues

Even with careful preparation, you might encounter issues when installing or using Ansible on Manjaro. Here are solutions to common problems:

Python Version Mismatches:

Ansible requires a compatible Python version. If you encounter errors related to Python, check your Python version:

python --version

For Ansible 2.17+ with Python 3.12+, you might encounter a “future feature annotations is not defined” error. This can be resolved by:

  1. Downgrading Python to a compatible version
  2. Setting a specific Python interpreter in your ansible.cfg:
    [defaults]
    interpreter_python = /usr/bin/python3.11
  3. Waiting for an Ansible update that addresses compatibility

Repository Access Problems:

If you encounter issues accessing repositories, refresh your mirrors:

sudo pacman-mirrors -f && sudo pacman -Syyu

This ensures you’re using the most reliable mirrors for package downloads.

Module Not Found Errors:

If you see errors like “couldn’t resolve module/action,” install the required collections:

ansible-galaxy collection install community.general

For persistent issues, verify the collection is properly installed:

ansible-galaxy collection list

In some cases, you might need to specify the full collection path in your playbook.

Permission Issues:

Errors related to permissions can be addressed by:

  1. Using the become: yes directive in your playbooks
  2. Setting up proper sudo permissions for your user
  3. Configuring SSH key-based authentication correctly

When troubleshooting, the -vvvv flag can be helpful for increasing verbosity:

ansible-playbook -vvvv playbook.yml

This provides detailed debugging information to identify the root cause of issues.

Maintaining and Updating Ansible

Keeping Ansible up-to-date is important for security and accessing new features. On Manjaro, updating Ansible is straightforward:

sudo pacman -Syu

This command updates all packages, including Ansible. For more targeted updates:

sudo pacman -S ansible

When maintaining Ansible, consider the following best practices:

  1. Version Compatibility: Be aware of compatibility between Ansible core and collections.
  2. Regular Updates: Schedule regular updates to benefit from security patches.
  3. Backup Configurations: Before major updates, back up your ansible.cfg and inventory files.
  4. Testing: Test playbooks after updates to ensure compatibility.

For collections and modules managed separately, use:

ansible-galaxy collection install --upgrade community.general

This ensures your modules stay current with the latest features and fixes.

Congratulations! You have successfully installed Ansible. Thanks for using this tutorial for installing Ansible on your Manjaro Linux system. For additional help or useful information, we recommend you check the official Ansible website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button