In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to use Ansible installation software, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
Use the Ansible script to automatically install and update software on the device.
Ansible is a popular automation tool used by system administrators and developers to keep computer systems at their best. Like the extensible framework, Ansible itself has limited functionality, and its real functionality is reflected in many modules. To some extent, the Ansible module is the command of the Linux system. They provide solutions to specific problems, and a common task when maintaining computers is to keep all computers up to date and consistent.
I used to use a text list of software packages to keep my system more or less synchronized: I would list the packages installed on my laptop and cross-reference them with a desktop or another server to manually make up for the differences. Of course, installing and maintaining applications on Linux machines is a basic feature of Ansible, which means you can list what you need on the computer you care about.
Find the correct Ansible module
The number of Ansible modules is very large, how to find the module that can complete your task? In Linux, you can find the application to run in the application menu or / usr/bin. When using Ansible, you can refer to the Ansible module index.
The index is listed by category. With a little search, you are likely to find the module you need. For package management, the Packaging module is suitable for almost all systems with a package manager.
Start writing an Ansible script
First, select the package manager on the local computer. For example, if you plan to write Ansible instructions (called "script playbook" in Ansible) on a laptop running Fedora, start with the dnf module. If you write on Elementary OS, use the apt module, and so on. This way you can start testing and verification, and you can extend it to other computers in the future.
The first step is to create a directory that represents your script. It's not absolutely necessary, but it's a good habit. Ansible only needs a configuration file to run in YAML, but if you want to extend the script later, you can control Ansible by changing directories and files. Now, just create a directory called install_packages or something like that:
$mkdir ~ / install_packages
You can name Ansible's script as you like, but you usually name it site.yml:
$touch ~ / install_packages/site.yml
Open site.yml in your favorite text editor and add the following:
-hosts: localhost tasks:-name: install packages become: true become_user: root dnf: state: present name:-tcsh-htop
You must adjust the name of the module you use to match the distribution you are using. In this example, I use dnf because I write a screenplay on Fedora Linux.
Just like the commands in the Linux terminal, knowing how to invoke the Ansible module is half successful. This sample script follows the standard script format:
Hosts is one or more computers. In this example, the target computer is localhost, which is the computer you are currently using (not the remote system to which you want Ansible to connect).
Tasks is a list of tasks you need to perform on the host.
Name is the humanized name of the task. In this case, I use install packages because that's what the task is doing.
Become allows Ansible to change the user who is running this task.
Become_user allows Ansible to run this task as a root user. This is necessary because only root users can install applications using dnf.
Dnf is the module name, which you can find in the module index on the Ansible website.
The nodes under the dnf are dedicated to the dnf module. This is the key to module documentation. Just like the man page of the Linux command, the module documentation will tell you the options available and the required parameters.
Ansible document
Installing the package is a relatively simple task that requires only two elements. The state option instructs Ansible to check for packages on the system, while the name option lists the packages to look for. Ansible adjusts to the state of the machine, so module instructions always mean change. If Ansible scans the system state and finds that the system described in the script (in this case, tcsh and htop exists) conflicts with the actual state, then the task of Ansible is to make the necessary changes to match the system to the script. Ansible can be changed through the dnf (or apt or any other package manager) module.
Each module may have a different set of options, so always refer to the module documentation when writing a screenplay. Unless you are very familiar with the module, this is the only reasonable way to expect the module to get the job done.
Verify YAML
The script is written in YAML. Because YAML follows strict syntax, it is helpful to install yamllint to check the script. Even better, there is an Ansible-specific inspection tool called ansible-lint, which is made specifically for scripts. Install it before continuing.
On Fedora or CentOs:
$sudo dnf ins tall yamllint python3-ansible-lint
On Debian, Elementary, or Ubuntu, the same:
$sudo apt install yamllint ansible-lint
Use ansible-link to verify your script. If you can't use ansible-lint, you can use yamllint.
$ansible-lint ~ / install_packages/site.yml
Success returns nothing, but if there are errors in the file, you must fix them before continuing. Common errors in copying and pasting include omitting line breaks at the end of the last line and indenting using tabs instead of spaces. Fix them in a text editor, rerun ansible-llint, and repeat the process until ansible-lint or yamllint does not return.
Install an application using Ansible
Now that you have a valid verifiable script, you can finally run it on your local computer, because you happen to know that the tasks defined by the script require root permissions, so you must use the-- ask-become-pass option when calling Ansible, so you will be prompted for your administrator password.
Start the installation:
$ansible-playbook-- ask-become-pass ~ / install_packages/site.ymlBECOME password:PLAY [localhost] * * TASK [Gathering Facts] * * ok: [localhost] TASK [install packages] * * ok: [localhost] PLAY RECAP * * localhost: ok=0 changed=2 unreachable=0 failed=0 [...]
After these commands are executed, the target system will be in the same state as described in the script.
Install the application on a remote system
Replacing a simple command with so many operations may be counterproductive, but the advantage of Ansible is that it can automate all your systems. You can use conditional statements to make Ansible use specific modules on different systems, but for now, assume that all computers use the same package manager.
To connect to a remote system, you must define the remote system in the / etc/ansible/hosts file, which is installed with Ansible, so it already exists, but it may be empty, except for some explanatory comments. Use sudo to open it in your favorite text editor.
You can define hosts by their IP address or host name (as long as the host name can be resolved). For example, if you have defined liavara in / etc/hosts and can successfully ping, you can set liavara as the host in / etc/ansible/hosts. Or, if you are running a domain name server or Avahi server and can ping liavara, you can define it in / etc/ansible/hosts. Otherwise, you must use its IP address.
You must also successfully establish a secure shell (SSH) connection to the target host. The easiest way is to use the ssh-copy-id command, but if you've never established a SSH connection to a host before, read my article on how to create an automatic SSH connection.
Once you have entered the hostname or IP address in the / etc/ansible/hosts file, you can change the hosts definition in the script:
-hosts: all tasks:-name: install packages become: true become_user: root dnf: state: present name:-tcsh-htop
Run ansible-playbook again:
$ansible-playbook-- ask-become-pass ~ / install_packages/site.yml
This time, the script will run on your remote system.
If you add more hosts, there are many ways to filter which hosts perform which tasks. For example, you can create host groups (webserves for servers, workstations for desktops, etc.).
Ansible for mixed environment
So far, we have assumed that all hosts configured by Ansible are running the same operating system (operating systems that use the dnf command for package management). So, what if you want to manage hosts for different distributions, such as Ubuntu (using apt) or Arch (using pacman), or other operating systems?
Ansible can come in handy as long as the target operating system has a package manager (MacOs has Homebrew,Windows and Chocolatey).
This is where the advantage of Ansible is most obvious. In the shell script, you must check which package managers are available on the target host, and even if you are using pure Python, you must also check the operating system. Ansible not only has these features built in, but also has a mechanism for using command results in scripts. Instead of using the dnf module, you can use the action keyword to perform tasks defined by variables provided by the Ansible fact collection subsystem.
-hosts: all tasks:-name: install packages become: true become_user: root action: > {{ansible_pkg_mgr}} name=htop,transmission state=present update_cache=yes
The action keyword loads the target plug-in. In this case, it uses the ansible_pkg_mgr variable, which is populated by Ansible during the initial collection of information. You don't need to tell Ansible to collect facts about its operating system, so it's easy to ignore this, but when you run a script, you'll see it in the default output:
TASK [Gathering Facts] * * ok: [localhost]
The action plug-in uses the information from this probe to populate the ansible_pkg_mgr with the relevant package manager commands to install the packages listed after the name parameter. With eight lines of code, you can overcome complex cross-platform challenges that are rarely allowed in other scripting options.
Thank you for reading this article carefully. I hope the article "how to use Ansible installation Software" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.