openSUSE

How To Install Ansible on openSUSE

Install Ansible on openSUSE

Ansible stands as one of the most powerful automation tools available for system administrators and DevOps professionals today. As an agentless configuration management tool, Ansible simplifies complex tasks, streamlines deployment processes, and enables infrastructure automation at scale. For openSUSE users, installing and configuring Ansible provides a robust solution for managing multiple systems efficiently. This comprehensive guide walks you through the entire process of installing Ansible on openSUSE, from understanding its core concepts to creating your first automation playbook.

Table of Contents

Understanding Ansible and Its Benefits

Ansible is an open-source automation platform that simplifies complex IT tasks. Unlike other configuration management tools, Ansible operates without requiring agents installed on remote systems, instead relying on SSH for secure connections. This agentless architecture makes it lightweight and easy to implement across diverse environments.

The key features that make Ansible particularly valuable include:

  • Declarative Language: Ansible uses YAML (YAML Ain’t Markup Language) for its playbooks, making automation scripts human-readable and accessible even to those without extensive programming experience.
  • Idempotence: Ansible operations can be run multiple times without changing the result beyond the initial application, ensuring consistent system states.
  • Extensive Module Library: With thousands of built-in modules, Ansible can automate virtually any IT system, from servers and network devices to cloud services and containers.

For openSUSE administrators, Ansible offers particular advantages. The platform’s flexibility aligns well with openSUSE’s enterprise-grade stability, making it ideal for both development environments and production deployments. Whether you’re managing a single server or orchestrating complex multi-tier applications, Ansible provides the scalability needed for growing infrastructure demands.

Prerequisites for Installing Ansible on openSUSE

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

  • A running openSUSE system (compatible with both Leap and Tumbleweed versions)
  • Administrative (sudo) privileges on your system
  • Basic familiarity with the command line interface
  • Network connectivity for downloading packages
  • Python 3 installed (comes pre-installed on most openSUSE distributions)
  • SSH client installed for remote server management

It’s also recommended to update your system before installation to ensure compatibility with the latest packages:

sudo zypper update

Taking time to prepare your environment properly will help prevent common installation issues and ensure a smooth setup process for your Ansible environment.

Installation Methods Overview

When it comes to installing Ansible on openSUSE, you have several options to consider. Each method has distinct advantages depending on your specific requirements and environment. The three primary installation methods are:

  1. Official openSUSE Repositories: This is the simplest and most straightforward approach. Installation via official repositories ensures system compatibility and simplified updates through the package manager. This method is ideal for those who prefer stability and ease of maintenance.
  2. Python Pip Installation: Using Python’s package manager provides access to the latest Ansible versions, often ahead of distribution repositories. This method offers greater flexibility and control over versions, making it suitable for development environments or when specific features from newer releases are required.
  3. Source Code Installation: Installing from source gives you complete control over the installation process and access to development versions. This approach is best for advanced users who need to customize their installation or contribute to Ansible development.

Consider your specific use case, required features, and maintenance preferences when selecting the installation method. For most users, the repository installation provides the best balance of simplicity and stability.

Method 1: Installing Ansible Using openSUSE Official Repositories

Installing Ansible through openSUSE’s official repositories is the most straightforward method and ensures system compatibility. Follow these detailed steps to install Ansible using zypper, the openSUSE package manager:

Step 1: Update System Packages

First, ensure your system’s package database is up-to-date:

sudo zypper refresh

This command refreshes the repository metadata, ensuring you have access to the latest package information.

Step 2: Install Ansible

Install Ansible directly from the repository with:

sudo zypper install ansible

The package manager will automatically resolve and install all required dependencies.

Step 3: Verify Installation

Check that Ansible was installed correctly by running:

sudo zypper info ansible

This will display detailed information about the installed Ansible package, including version and installation status.

Step 4: Check Ansible Version

Confirm the installation by checking the version:

ansible --version

This command displays not only the version number but also configuration file locations and Python version used by Ansible.

The repository installation method provides the advantage of receiving updates alongside your regular system maintenance. When openSUSE updates its repositories with new Ansible versions, you’ll receive these updates through the standard system update process.

Method 2: Installing Ansible via Python Pip

Installing Ansible through Python’s package manager (pip) often provides access to newer versions than those available in the distribution repositories. This method is particularly useful for development environments or when you need specific features from the latest releases.

Step 1: Install Python Dependencies

First, ensure you have Python and pip installed on your system:

sudo zypper install python3 python3-pip sshpass

These packages provide the foundation needed for installing and running Ansible.

Step 2: Install Ansible with Pip

Install Ansible globally using pip:

sudo pip3 install ansible

Alternatively, to install a specific version of Ansible:

sudo pip3 install ansible==X.Y.Z

Replace X.Y.Z with your desired version number (e.g., 2.12.1).

Step 3: Create a Virtual Environment (Optional)

For isolated installations, consider using Python virtual environments:

python3 -m venv ansible_env
source ansible_env/bin/activate
pip install ansible

This approach prevents conflicts with system packages and allows multiple Ansible versions to coexist on the same system.

Step 4: Verify the Installation

Confirm the installation was successful:

ansible --version

The command should display Ansible’s version and configuration details.

When using pip installation, keep in mind that you’ll need to manage updates manually through pip rather than through the system’s package manager. This provides more control but requires more active maintenance.

Method 3: Installing Ansible from Source

Installing Ansible from source gives you complete control over the installation process and access to development versions. This method is recommended for advanced users who require specific customizations or want to contribute to Ansible development.

Step 1: Install Required Dependencies

First, install the necessary development tools and dependencies:

sudo zypper install git python3-devel python3-pip python3-setuptools

These packages provide the development environment needed to build Ansible from source.

Step 2: Clone the Ansible Repository

Clone the official Ansible repository from GitHub:

git clone https://github.com/ansible/ansible.git
cd ansible

For a specific version, check out the appropriate tag:

git checkout stable-2.12  # Replace with your desired version

Step 3: Set Up the Environment

Set up your environment to use the development version:

source ./hacking/env-setup

This script configures your environment variables to use the development version of Ansible.

Step 4: Install Required Python Packages

Install the required Python dependencies:

pip3 install -r requirements.txt

This installs all the Python packages needed by Ansible.

Step 5: Verify the Installation

Confirm that the source installation works correctly:

ansible --version

The output should show that you’re using the development version from the local directory.

Source installation requires more maintenance as you’ll need to manually pull updates from the repository and rebuild when needed. However, it provides the advantage of immediate access to the latest features and fixes.

Verifying Your Ansible Installation

After installing Ansible, it’s crucial to verify that the installation was successful and is functioning correctly. Proper verification ensures that you can proceed with confidence to configuring and using Ansible.

Checking the Ansible Version

The most basic verification is checking the installed version:

ansible --version

This command should produce output similar to:

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.x/site-packages/ansible
executable location = /usr/bin/ansible
python version = 3.x.x

The output provides valuable information about your Ansible installation, including the configuration file location, module search paths, and Python version.

Testing Basic Functionality

To verify that Ansible can execute commands properly, try a simple ping command against localhost:

ansible localhost -m ping

A successful response should look like:

localhost | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

If you encounter errors during verification, check common issues like:

  • Python version compatibility
  • Path configurations
  • Permission issues
  • Missing dependencies

Proper verification at this stage prevents frustration later when attempting more complex automation tasks.

Configuring Ansible After Installation

Once Ansible is installed and verified, proper configuration is essential for optimal functionality. Ansible uses a hierarchical configuration system that allows for global, user-level, and project-specific settings.

Creating the Ansible Directory Structure

First, set up the recommended directory structure:

mkdir -p ~/ansible/playbooks
mkdir -p ~/ansible/inventory
mkdir -p ~/ansible/roles

This organization helps manage your Ansible resources efficiently as your automation grows.

Setting Up the Inventory File

The inventory file defines the hosts and groups that Ansible will manage. Create a basic inventory file:

nano ~/ansible/inventory/hosts

Add your servers (for example):

[webservers]
web1.example.com
web2.example.com

[dbservers]
db1.example.com
db2.example.com

[local]
localhost ansible_connection=local

This file organizes your servers into logical groups for targeted automation.

Configuring ansible.cfg

Create or modify the Ansible configuration file to customize behavior:

nano ~/ansible/ansible.cfg

Add basic configuration:

[defaults]
inventory = ~/ansible/inventory/hosts
remote_user = ansible
host_key_checking = False
roles_path = ~/ansible/roles

[privilege_escalation]
become = True
become_method = sudo
become_user = root
become_ask_pass = False

This configuration specifies your inventory location, remote user, and privilege escalation preferences.

Understanding configuration priorities is important. Ansible looks for configuration in the following order:

  1. ANSIBLE_CONFIG environment variable
  2. ansible.cfg in the current directory
  3. ~/.ansible.cfg (user’s home directory)
  4. /etc/ansible/ansible.cfg (system-wide)

Proper configuration sets the foundation for effective Ansible usage across your infrastructure.

Setting Up SSH for Remote Management

Since Ansible relies on SSH for remote management, proper SSH configuration is critical for seamless operation. Follow these steps to set up secure, password-less authentication:

Generating SSH Keys

If you haven’t already generated an SSH key pair:

ssh-keygen -t rsa -b 4096

Press Enter at the prompts to accept the default location and create a key without a passphrase (for automation purposes).

Distributing SSH Keys to Remote Servers

To enable password-less authentication, copy your public key to each target server:

ssh-copy-id username@remote_server

Replace username and remote_server with your actual username and server hostname or IP address.

Testing SSH Connectivity

Verify that you can connect without a password:

ssh username@remote_server

If successful, you’ll connect without being prompted for a password.

SSH Configuration Options for Ansible

For advanced SSH configurations, create or edit ~/.ssh/config:

nano ~/.ssh/config

Add custom configurations for your servers:

Host web*
    User ansible
    IdentityFile ~/.ssh/id_rsa
    StrictHostKeyChecking no
    
Host db*
    User ansible
    IdentityFile ~/.ssh/id_rsa
    Port 2222

This configuration allows for custom settings per server or server group.

Proper SSH setup eliminates authentication friction and makes your Ansible automation more reliable and secure.

Creating Your First Ansible Playbook

Now that Ansible is installed and configured, it’s time to create your first playbook. Playbooks are YAML files that define automation tasks and are the core of Ansible’s functionality.

Understanding Playbook Structure

Ansible playbooks consist of:

  • Hosts: Specifies which servers to target
  • Tasks: Actions to perform on those hosts
  • Handlers: Triggered by tasks when changes occur
  • Variables: Values that can be reused throughout the playbook
  • Modules: Specific functions that perform the actual work

Creating a Simple Test Playbook

Create a new file for your playbook:

nano ~/ansible/playbooks/first-playbook.yml

Add the following content for a basic web server setup:

---
- name: Configure web server
  hosts: local
  become: yes
  
  tasks:
    - name: Update package cache
      zypper:
        update_cache: yes
      
    - name: Install Apache web server
      zypper:
        name: apache2
        state: present
      
    - name: Start and enable Apache service
      service:
        name: apache2
        state: started
        enabled: yes
      
    - name: Create a test web page
      copy:
        content: "<html><body><h1>My first Ansible-managed website</h1></body></html>"
        dest: /srv/www/htdocs/index.html
        owner: wwwrun
        group: www
        mode: '0644'

Running Your First Playbook

Execute the playbook with:

ansible-playbook ~/ansible/playbooks/first-playbook.yml

The output will show each task being executed and its result status.

Understanding this basic structure allows you to create increasingly complex automation for your infrastructure needs. As you become more familiar with Ansible, you can incorporate variables, conditions, loops, and roles to make your playbooks more powerful and reusable.

Managing Remote Servers with Ansible

After mastering basic playbooks, you can expand your Ansible usage to manage multiple remote servers efficiently. This section covers key concepts for remote server management.

Setting Up Inventory Groups

Organize your servers into logical groups in your inventory file:

[frontend]
front1.example.com
front2.example.com

[backend]
back1.example.com
back2.example.com

[production:children]
frontend
backend

[development]
dev.example.com

This hierarchical structure allows targeting specific server types or environments.

Targeting Specific Hosts or Groups

When running playbooks, you can target specific hosts or groups:

# Target all servers
ansible-playbook playbook.yml

# Target only frontend servers
ansible-playbook playbook.yml --limit frontend

# Target specific hosts
ansible-playbook playbook.yml --limit front1.example.com,back1.example.com

This flexibility allows precise control over your automation scope.

Using Ad-hoc Commands for Quick Tasks

For simple tasks, Ansible’s ad-hoc commands provide a quick alternative to playbooks:

# Check disk space on all servers
ansible all -a "df -h"

# Update packages on backend servers
ansible backend -b -m zypper -a "name=* state=latest"

# Restart a service on a specific server
ansible front1.example.com -b -m service -a "name=apache2 state=restarted"

Ad-hoc commands are perfect for one-off tasks that don’t require the structure of a full playbook.

Privilege Escalation on Remote Systems

Many operations require root privileges. Control how Ansible escalates privileges:

- name: Configure system settings
  hosts: all
  become: yes               # Enable privilege escalation
  become_method: sudo       # Use sudo for escalation
  become_user: root         # Escalate to root user
  
  tasks:
    - name: Update system configuration
      lineinfile:
        path: /etc/sysctl.conf
        line: "net.ipv4.ip_forward=1"

These settings ensure Ansible can perform administrative tasks securely across your infrastructure.

Effective remote server management with Ansible requires understanding these concepts and applying them to your specific environment needs.

Common Issues and Troubleshooting

Even with careful planning, you may encounter issues when working with Ansible on openSUSE. This section addresses common problems and their solutions.

SSH Connection Problems

Issue: Ansible fails to connect to remote hosts with SSH errors.

Solutions:

  • Verify SSH key permissions: chmod 600 ~/.ssh/id_rsa
  • Check host key checking settings in ansible.cfg
  • Test SSH connection manually: ssh user@host
  • Add -vvv to ansible commands for verbose SSH debugging: ansible host -m ping -vvv

Permission Issues

Issue: Tasks fail with permission denied errors.

Solutions:

  • Ensure proper sudo configuration on remote hosts
  • Check that the remote user has appropriate sudo privileges
  • Verify the become settings in your playbooks
  • Test sudo access manually on the remote host

Python Version Conflicts

Issue: Ansible reports Python compatibility errors.

Solutions:

  • Specify Python interpreter in inventory: ansible_python_interpreter=/usr/bin/python3
  • Install compatible Python version on remote hosts
  • Use a virtual environment with specific Python version
  • Check Python dependencies with pip show ansible

Module Installation Problems

Issue: Missing modules or module dependencies.

Solutions:

  • Install required Python packages: pip install pywinrm (for Windows support)
  • Check module documentation for prerequisites
  • Verify Ansible collection installation: ansible-galaxy collection list
  • Update Ansible to the latest version for module compatibility

Debugging Techniques

When troubleshooting complex issues:

# Increase verbosity
ansible-playbook playbook.yml -vvv

# Check playbook syntax
ansible-playbook playbook.yml --syntax-check

# Run in check mode without making changes
ansible-playbook playbook.yml --check

# Step through playbook tasks
ansible-playbook playbook.yml --step

These debugging techniques help isolate and resolve issues efficiently, keeping your automation reliable and effective.

Keeping Ansible Updated on openSUSE

Maintaining an up-to-date Ansible installation ensures access to the latest features, security patches, and bug fixes. The update process varies depending on your installation method.

Updating Ansible Through Repositories

If you installed Ansible from the openSUSE repositories, update it along with your regular system updates:

sudo zypper update

For a targeted update of just Ansible and its dependencies:

sudo zypper update ansible

This method ensures compatibility with your openSUSE version and maintains system stability.

Updating Pip Installations

If you installed Ansible using pip, update it with:

sudo pip3 install --upgrade ansible

To update to a specific version:

sudo pip3 install --upgrade ansible==X.Y.Z

Remember that pip updates bypass the distribution’s package testing, so consider testing updates in a non-production environment first.

Managing Dependencies

Ansible depends on various Python packages that may need updating independently:

pip list --outdated | grep ansible
pip install --upgrade <package-name>

This helps ensure all components remain compatible.

Version Compatibility Considerations

Before updating, check the Ansible release notes for:

  • Breaking changes that might affect your playbooks
  • Deprecated features you may be using
  • New requirements that might impact your environment
  • Compatibility with your openSUSE version

Establishing a testing workflow for Ansible updates helps prevent unexpected issues in production environments.

Best Practices for Using Ansible on openSUSE

To maximize the benefits of Ansible on openSUSE, follow these best practices that experienced administrators have developed over years of usage.

Directory Structure Organization

Maintain a clean, logical directory structure:

ansible/
├── inventory/
│   ├── production
│   ├── staging
│   └── development
├── group_vars/
│   ├── all.yml
│   └── webservers.yml
├── host_vars/
│   └── specific-host.yml
├── roles/
│   ├── common/
│   ├── webserver/
│   └── database/
└── playbooks/
    ├── site.yml
    ├── webserver.yml
    └── database.yml

This organization improves maintainability and makes collaboration easier, especially in large environments.

Version Control for Playbooks

Store your Ansible content in a version control system like Git:

# Initialize a Git repository
cd ~/ansible
git init

# Create a .gitignore file
echo "*.retry" > .gitignore

# Commit your files
git add .
git commit -m "Initial Ansible configuration"

Version control provides history tracking, collaboration features, and rollback capabilities.

Documentation Standards

Document your Ansible infrastructure thoroughly:

  • Add comments in playbooks to explain complex tasks
  • Maintain a README file for each role explaining its purpose and usage
  • Document variables and their expected values
  • Create a centralized wiki or documentation site for your Ansible practices

Good documentation reduces onboarding time for new team members and helps prevent errors.

Testing Strategies

Implement testing procedures for your Ansible code:

  • Use --check mode to simulate changes before applying them
  • Create staging environments that mirror production
  • Utilize ansible-lint to check playbook quality
  • Implement molecule for role testing

Thorough testing prevents unexpected issues when deploying to production systems.

Security Considerations

Prioritize security in your Ansible implementation:

  • Use vault for encrypting sensitive data: ansible-vault create secrets.yml
  • Regularly rotate SSH keys and credentials
  • Apply the principle of least privilege for Ansible users
  • Review playbooks for security implications before deployment
  • Use specific task delegation rather than running everything as root

Congratulations! You have successfully installed Ansible. Thanks for using this tutorial for installing Ansible on your openSUSE 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