Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to install and configure Ansible Automation tools

2025-04-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "Ansible automation tools how to install configuration", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "Ansible automation tools how to install configuration" this article.

There are many automation tools on the market. I can give you a few examples, such as Puppet, Chef, CFEngine, Foreman, Katello, Saltstock, Space Walk, which are widely used by many organizations.

What can automation tools do?

Automated tools automate routine tasks without human intervention, making the Linux administrator's job easier. These tools allow users to perform configuration management, application deployment, and resource provisioning.

Why do you like Ansible?

Ansible is an agentless automation tool that uses SSH to perform all tasks, but other tools require agents to be installed on the client node.

What is Ansible?

Ansible is an open source, easy-to-use and powerful IT automation tool that performs tasks on client nodes through SSH.

It is built in Python, one of the most popular and powerful programming languages in the world today. Python is required on both sides to execute all modules.

It can configure the system, deploy software, and schedule advanced IT tasks, such as continuous deployment or zero downtime rolling updates. You can easily perform any type of automated tasks, including simple and complex tasks, through Ansible.

Before you begin, you need to know some Ansible terms that can help you better create tasks.

How does Ansible work?

Ansible works by pushing Mini Program called ansible modules on the client nodes, which are temporarily stored in the client nodes and communicate with the Ansible server through the JSON protocol.

Ansible runs these modules through SSH and deletes them when they are finished.

Modules are scripts written in Python or Perl and so on.

The control node, which controls all the functions of the script, including the client node (host).

Control node Control node

A host that uses Ansible to perform tasks on a controlled node You can have multiple control nodes, but you cannot use the Windows system host as the control node.

Controlled node Managed node

Controls the list of hosts configured by the node

List Inventory

A list of hosts managed by the control nodes, which are configured in the / etc/ansible/hosts file It contains information about each node, such as an IP address or its hostname, and can be grouped as needed.

Module Module

Each module is used to perform specific tasks, and there are currently 3387 modules

Peer-to-peer ad-hoc

It allows you to run a task at once, using the / usr/bin/ansible binary.

Task Task

Each

Action Play

Have a to-do list. Tasks are executed sequentially, one task at a time in the controlled node.

Script Playbook

You can use the script to perform multiple tasks at the same time, while peer-to-peer can only perform one task. The script is written in YAML and is easy to read. In the future, we will write an article about the script, which you can use to perform complex tasks.

Test environment

This environment consists of a control node (server.2g.lab) and three controlled nodes (node1.2g.lab, node2.2g.lab, node3.2g.lab), all of which run in a virtual environment with operating systems:

System PurposeHostnameIP AddressOSAnsible Control Nodeserver.2g.lab192.168.1.7Manjaro 18Managed Node1node1.2g.lab192.168.1.6CentOS7Managed Node2node2.2g.lab192.168.1.5CentOS8Managed Node3node3.2g.lab192.168.1.9Ubuntu 18.04User: daygeek precondition

Enable password-less authentication between the Ansible control node and the controlled node.

The control node must be Python 2 (version 2.7) or Python 3 (version 3.5 or later).

The controlled node must be Python 2 (version 2.6 or later) or Python 3 (version 3.5 or later).

If SELinux is enabled on remote nodes, you need to install libselinux-python on Ansible before you can use any copy, file, and template-related features in them.

How to install Ansible on the control node

For Fedora/RHEL 8/CentOS 8 systems, use the DNF command to install Ansible.

Note: you need to enable the EPEL repository on the RHEL/CentOS system because the Ansible package is not available in the official repository of the distribution.

$sudo dnf install ansible

For Debian/Ubuntu systems, use the APT-GET command or the APT command to install Ansible.

Configure the following PPA to install the latest stable version of Ansible on Ubuntu.

$sudo apt update$ sudo apt install software-properties-common$ sudo apt-add-repository-yes-update ppa:ansible/ansible$ sudo apt install ansible

For Debian systems, configure the following source list:

$echo "deb http://ppa.launchpad.net/ansible/ansible/ubuntu trusty main" | sudo tee-a / etc/apt/sources.list.d/ansible.list$ sudo apt-key adv-keyserver keyserver.ubuntu.com-recv-keys 93C4A3FD7BB9C367 $sudo apt update$ sudo apt install ansible

For Arch Linux systems, use the Pacman command to install Ansible:

$sudo pacman-S ansible

For RHEL/CentOS systems, use the YUM command to install Ansible:

$sudo yum install ansible

For openSUSE systems, use the Zypper command to install Ansible:

$sudo zypper install ansible

Alternatively, you can use the Python PIP package management tool to install:

$curl https://bootstrap.pypa.io/get-pip.py-o get-pip.py$ sudo python get-pip.py$ sudo pip install ansible

Check the installed version of Ansible on the control node:

$ansible-- version ansible 2.9.2 config file = / etc/ansible/ansible.cfg configured module search path = ['/ home/daygeek/.ansible/plugins/modules','/ usr/share/ansible/plugins/modules'] ansible python module location = / usr/lib/python3.8/site-packages/ansible executable location = / usr/bin/ansible python version = 3.8.1 (default, Jan 8 2020, 23:09:20) [GCC 9.2.0] how to install Python on a controlled node?

Install python on the controlled node using the following command:

$sudo yum install-y python$ sudo dnf install-y python$ sudo zypper install-y python$ sudo pacman-S python$ sudo apt install-y python how to set SSH key authentication in Linux (no password authentication)

Create the ssh key using the following command, and then copy it to the remote computer.

$ssh-keygen$ ssh-copy-id daygeek@node1.2g.lab$ ssh-copy-id daygeek@node2.2g.lab$ ssh-copy-id daygeek@node3.2g.lab

Refer to this article, "setting up SSH key authentication (no password authentication) on Linux".

How to create an Ansible host list

Add a list of nodes to manage in the / etc/ansible/hosts file. If you do not have the file, you can create a new file. The following is the host manifest file for my test environment:

$sudo vi / etc/ansible/hosts [web] node1.2g.labnode2.2g.lab [app] node3.2g.lab

Let's see if we can find all hosts using the following command.

$ansible all-- list-hosts hosts (3): node1.2g.lab node2.2g.lab node3.2g.lab

Run the following command on a single group:

$ansible web-- list-hosts hosts (2): how node1.2g.lab node2.2g.lab uses point-to-point commands to perform tasks

Once you have completed the host list verification check, you are ready to hit the road. Well done!

Syntax:

Ansible [pattern]-m [module]-a "[module options]" Details:=ansible: a commandpattern: Enter the entire inventory or a specific group-m [module]: Run the given module name-a [module options]: Specify the module arguments

Use the Ping module to ping all nodes in the host list:

$ansible all-m ping node3.2g.lab | SUCCESS = > {"ansible_facts": {"discovered_interpreter_python": "/ usr/bin/python"}, "changed": false, "ping": "pong"} node1.2g.lab | SUCCESS = > {"ansible_facts": {"discovered_interpreter_python": "/ usr/bin/python"}, "changed": false "ping": "pong"} node2.2g.lab | SUCCESS = > {"ansible_facts": {"discovered_interpreter_python": "/ usr/libexec/platform-python"}, "changed": false, "ping": "pong"}

All systems returned success, but nothing changed except pong for success.

You can use the following command to get a list of available modules.

$ansible-doc-l

There are currently 3387 built-in modules that will increase with the increment of Ansible versions:

$ansible-doc-l | wc-l3387

Use the command module to execute commands on all nodes in the host list:

$ansible all-m command-a "uptime" node3.2g.lab | CHANGED | rc=0 > > 18:05:07 up 1:21, 3 users, load average: 0.12,0.06, 0.01node1.2g.lab | CHANGED | rc=0 > > 06:35:06 up 1:21, 4 users, load average: 0.01,0.03, 0.05node2.2g.lab | CHANGED | rc=0 > > 18:05:07 up 1:25, 3 users, load average: 0.01,0.01,0.00

Executes the command module to the specified group.

Check the memory usage of the app group host:

$ansible app-m command-a "free-m" node3.2g.lab | CHANGED | rc=0 > > total used free shared buff/cache availableMem: 1993 1065 91 6 836 748Swap: 1425 0 1424

To run the hostnamectl command on the web group, use the following format:

$ansible web-m command-a "hostnamectl" node1.2g.lab | CHANGED | rc=0 > > Static hostname: CentOS7.2daygeek.com Icon name: computer-vm Chassis: vm Machine ID: 002f47b82af248f5be1d67b67e03514c Boot ID: dc38f9b8089d4b2d9304e526e00c6a8f Virtualization: kvm Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 3.10.0-957.el7.x86_64 Architecture: x86 -64node2.2g.lab | CHANGED | rc=0 > > Static hostname: node2.2g.lab Icon name: computer-vm Chassis: vm Machine ID: e39e3a27005d44d8bcbfcab201480b45 Boot ID: 27b46a09dde546da95ace03420fe12cb Virtualization: oracle Operating System: CentOS Linux 8 (Core) CPE OS Name: cpe:/o:centos:centos:8 Kernel: Linux 4.18.0-80.el8.x86_64 Architecture: x86-64 is "Ansible Automation" All the contents of the article "how to install the configuration of the tool" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report