Wie man Ansible auf die neueste Version aktualisiert

AnsibleAnsibleBeginner
Jetzt üben

💡 Dieser Artikel wurde von AI-Assistenten übersetzt. Um die englische Version anzuzeigen, können Sie hier klicken

Introduction

Ansible is a powerful open-source automation tool that has gained widespread adoption in the IT industry. As Ansible evolves, it is important to keep your installation up-to-date to take advantage of the latest features and improvements. This tutorial will guide you through the process of upgrading Ansible to the latest version on Ubuntu, which is the operating system used in this lab environment.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL ansible(("Ansible")) -.-> ansible/ModuleOperationsGroup(["Module Operations"]) ansible(("Ansible")) -.-> ansible/PlaybookEssentialsGroup(["Playbook Essentials"]) ansible/ModuleOperationsGroup -.-> ansible/apt("Package Manager") ansible/ModuleOperationsGroup -.-> ansible/command("Execute Commands") ansible/ModuleOperationsGroup -.-> ansible/copy("Transfer Files") ansible/ModuleOperationsGroup -.-> ansible/debug("Test Output") ansible/ModuleOperationsGroup -.-> ansible/file("Manage Files/Directories") ansible/ModuleOperationsGroup -.-> ansible/stat("File Statistics") ansible/PlaybookEssentialsGroup -.-> ansible/playbook("Execute Playbook") subgraph Lab Skills ansible/apt -.-> lab-414855{{"Wie man Ansible auf die neueste Version aktualisiert"}} ansible/command -.-> lab-414855{{"Wie man Ansible auf die neueste Version aktualisiert"}} ansible/copy -.-> lab-414855{{"Wie man Ansible auf die neueste Version aktualisiert"}} ansible/debug -.-> lab-414855{{"Wie man Ansible auf die neueste Version aktualisiert"}} ansible/file -.-> lab-414855{{"Wie man Ansible auf die neueste Version aktualisiert"}} ansible/stat -.-> lab-414855{{"Wie man Ansible auf die neueste Version aktualisiert"}} ansible/playbook -.-> lab-414855{{"Wie man Ansible auf die neueste Version aktualisiert"}} end

Understanding Ansible and Checking Your Current Version

Before we upgrade Ansible, it is helpful to understand what Ansible is and check which version you currently have installed.

What is Ansible?

Ansible is an open-source automation platform that simplifies configuration management, application deployment, and task automation. It uses YAML syntax for creating playbooks, which are documents that describe the desired state of your systems.

Some key benefits of Ansible include:

  • Agentless architecture: No need to install agents on managed nodes
  • Simple syntax: Uses human-readable YAML files
  • Idempotent execution: Running the same playbook multiple times produces the same result
  • Extensible: Supports custom modules and plugins

Checking Your Current Ansible Version

Let us start by checking if Ansible is already installed and, if so, which version you have. Open a terminal and run:

ansible --version

You should see output similar to this:

ansible [core 2.12.0]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/labex/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  ansible collection location = /home/labex/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0]
  jinja version = 3.0.3
  libyaml = True

The first line displays your current Ansible version. If you do not see output like this, Ansible may not be installed yet, which is fine because we will install it in the next step.

Understanding Ansible Versioning

Ansible follows semantic versioning with three numbers: major.minor.patch

  • Major version: Significant changes that may require modifications to your playbooks
  • Minor version: New features without breaking changes
  • Patch version: Bug fixes and minor improvements

Now that we understand what Ansible is and have checked our current version, we are ready to proceed with installing or upgrading Ansible in the next step.

Installing or Upgrading Ansible on Ubuntu

In this step, we will install or upgrade Ansible on the Ubuntu 22.04 system. We will use the official Ansible PPA (Personal Package Archive) to get the latest stable version.

Adding the Ansible PPA Repository

First, we need to make sure we have the necessary dependency for adding PPAs:

sudo apt update
sudo apt install -y software-properties-common

This command updates your package index and installs the software-properties-common package, which provides the add-apt-repository command.

Now, let us add the official Ansible PPA repository:

sudo add-apt-repository --yes --update ppa:ansible/ansible

You will see output showing that the repository has been added to your system. The --yes flag automatically confirms the addition, and --update refreshes the package list immediately.

Installing the Latest Ansible Version

With the repository added, we can now install the latest version of Ansible:

sudo apt install -y ansible

The -y flag automatically confirms the installation without prompting for confirmation.

This command will either install Ansible if it is not already present or upgrade it to the latest version available in the PPA.

Verifying the Installation or Upgrade

After the installation completes, let us verify that Ansible is installed correctly and check its version:

ansible --version

The output should show the latest version of Ansible available in the PPA. If you previously had an older version installed, you should now see a newer version number.

To keep track of our progress for the verification script, let us create a marker file:

touch /tmp/ansible_version_checked
touch /tmp/ansible_installed

Now we have successfully installed or upgraded Ansible on our Ubuntu system. In the next step, we will explore how to use some basic Ansible commands to verify that our installation is working correctly.

Verifying Your Ansible Installation with a Simple Playbook

Now that Ansible is installed, let us confirm that it works correctly by creating and running a simple Ansible playbook. Playbooks are YAML files that describe the desired state of your systems and the tasks Ansible should perform to reach that state.

Creating a Test Playbook

First, let us create a directory for our Ansible project:

mkdir -p ~/project/ansible-test
cd ~/project/ansible-test

Now, let us create a simple playbook using the built-in code editor. In the WebIDE, click on the "Explorer" icon in the left sidebar, navigate to the ansible-test directory we just created, and create a new file called test-playbook.yml.

Add the following content to this file:

---
- name: Test Ansible Installation
  hosts: localhost
  connection: local
  gather_facts: no

  tasks:
    - name: Print a message
      debug:
        msg: "Ansible is installed and working correctly!"

    - name: Get Ansible version
      command: ansible --version
      register: ansible_version_output

    - name: Display Ansible version
      debug:
        msg: "{{ ansible_version_output.stdout_lines[0] }}"

    - name: Create a test file
      file:
        path: /tmp/ansible-test-file.txt
        state: touch
        mode: "0644"

This playbook does the following:

  1. Targets the local machine (localhost)
  2. Prints a success message
  3. Retrieves and displays the Ansible version
  4. Creates a test file at /tmp/ansible-test-file.txt

Running the Playbook

Now let us run the playbook to verify that Ansible is working correctly:

cd ~/project/ansible-test
ansible-playbook test-playbook.yml

You should see output similar to this:

PLAY [Test Ansible Installation] ***********************************************

TASK [Print a message] *********************************************************
ok: [localhost] => {
    "msg": "Ansible is installed and working correctly!"
}

TASK [Get Ansible version] *****************************************************
changed: [localhost]

TASK [Display Ansible version] *************************************************
ok: [localhost] => {
    "msg": "ansible [core 2.12.0]"
}

TASK [Create a test file] ******************************************************
changed: [localhost]

PLAY RECAP *********************************************************************
localhost                  : ok=4    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

The output shows that all tasks completed successfully. The changed=2 in the recap indicates that two tasks made changes to the system (getting the version and creating the file).

Verifying the Test File

Let us check if the test file was created:

ls -l /tmp/ansible-test-file.txt

You should see the file listed with the permissions we specified:

-rw-r--r-- 1 labex labex 0 Aug 30 12:34 /tmp/ansible-test-file.txt

Let us mark this step as complete for our verification script:

touch /tmp/ansible_playbook_tested

Congratulations! You have successfully installed the latest version of Ansible and verified that it works correctly by running a simple playbook. This confirms that your Ansible installation is fully functional and ready for use.

Understanding Ansible Configuration and Modules

Now that we have Ansible installed and working, let us explore its configuration and the concept of modules.

Ansible Configuration

Ansible uses configuration files to control its behavior. The main configuration file is ansible.cfg, which can exist in several locations. Let us examine the default configuration file:

ls -la /etc/ansible/

You should see the default Ansible directory structure:

total 20
drwxr-xr-x  2 root root 4096 Aug 30 12:00 .
drwxr-xr-x 85 root root 4096 Aug 30 12:00 ..
-rw-r--r--  1 root root 8669 Aug 30 12:00 ansible.cfg
-rw-r--r--  1 root root 1021 Aug 30 12:00 hosts

Let us take a look at the hosts file, which is the default inventory file:

cat /etc/ansible/hosts

The inventory file contains a list of hosts that Ansible can manage. By default, it contains examples that are commented out.

Creating a Custom Configuration

Let us create our own Ansible configuration file in our project directory. In the WebIDE, create a new file called ansible.cfg in the ~/project/ansible-test directory with the following content:

[defaults]
inventory = ./inventory
host_key_checking = False
stdout_callback = yaml

Now, let us create a simple inventory file. In the WebIDE, create a new file called inventory in the ~/project/ansible-test directory with the following content:

[local]
localhost ansible_connection=local

Exploring Ansible Modules

Ansible modules are reusable units of code that perform specific tasks. Let us explore some of the available modules:

ansible-doc -l | wc -l

This command lists all available modules and counts them. You should see a large number, typically over 1000 modules.

Let us view the documentation for a specific module, such as the file module we used in our playbook:

ansible-doc file

You can press q to exit the documentation viewer.

Creating a More Complex Playbook

Now, let us create a more advanced playbook that demonstrates a few more Ansible modules. In the WebIDE, create a new file called modules-demo.yml in the ~/project/ansible-test directory with the following content:

---
- name: Ansible Modules Demo
  hosts: localhost
  connection: local
  gather_facts: yes

  tasks:
    - name: Display system information
      debug:
        msg: "System: {{ ansible_distribution }} {{ ansible_distribution_version }}"

    - name: Create a directory
      file:
        path: /tmp/ansible-demo
        state: directory
        mode: "0755"

    - name: Copy a file
      copy:
        content: "Created by Ansible modules demo playbook\n"
        dest: /tmp/ansible-demo/info.txt
        mode: "0644"

    - name: Gather information about a file
      stat:
        path: /tmp/ansible-demo/info.txt
      register: file_info

    - name: Show file information
      debug:
        msg: "File created at {{ file_info.stat.mtime }}"

Let us run this playbook:

cd ~/project/ansible-test
ansible-playbook modules-demo.yml

The playbook does the following:

  1. Displays information about your system
  2. Creates a directory at /tmp/ansible-demo
  3. Creates a file with custom content
  4. Gathers information about the file
  5. Displays the file's modification time

Let us mark this step as complete for our verification script:

touch /tmp/ansible_modules_explored

You have now learned about Ansible configuration, inventory files, and explored various modules. These are essential components for working with Ansible effectively.

Summary

In this lab, you have successfully learned how to install and upgrade Ansible to the latest version on an Ubuntu system. You have accomplished several key tasks:

  1. Understood the basics of Ansible and its versioning system
  2. Installed or upgraded Ansible using the official PPA repository
  3. Verified your installation by creating and running a simple playbook
  4. Explored Ansible configuration files and inventory management
  5. Learned about Ansible modules and their documentation
  6. Created and executed more complex playbooks to perform various tasks

These skills provide a solid foundation for working with Ansible in automation projects. By keeping your Ansible installation up-to-date, you can take advantage of the latest features, bug fixes, and security updates.

As you continue your Ansible journey, you can explore more advanced topics such as roles, collections, and Ansible AWX/Tower for enterprise management. These tools will help you build more sophisticated automation workflows and manage infrastructure at scale.