ansible official document translation -- ad-hoc command

The first part (it doesn't have to be in order, but the first part can let you get started quickly):
ansible official document translation - Quick Start (based on rhel8 system, applicable to centos)
Original link: Introduction to ad hoc commands

Introduction to ad hoc commands (temporary commands)

The easy ad hoc command automates a task on one or more controlled nodes using the / usr / bin / easy command line tool. Ad hoc commands are simple and convenient, but they cannot be reused. So why should we learn ad hoc commands first? The ad hoc command shows the simplicity and function of ansible. The concepts you learn here will run through your learning of ansible, including the playbook later. Before reading and executing these examples, please understand How to build your inventory (the translation of this has not started yet. I will make it up when it is translated later).

Why use ad hoc commands?

Ad hoc commands are usually used for tasks that you rarely repeat. For example, if you want to shut down all computers in your lab on Christmas day, you can execute a single line ansible command instead of writing a playbook. An ad hoc command is usually like this:

$ ansible [pattern] -m [module] -a "[module options]"

You will learn more about pattern and module later. (in fact, the plural is used here)

Applicable scenarios of ad hoc commands

Ad hoc tasks can be used to restart services, copy files, management packs and users. You can use ansible templates in ad hoc tasks. Both ad hoc tasks and playbook can be executed using specific modules until a specific state is reached. Before starting execution, ansible will check whether the target has reached the target state. If not, it will start execution.

Restart the server

You can use the ansible module and adopt the ad hoc mode to restart all web servers in atlanta at 10 o'clock. Before ansible executes this, you must put all servers in the [atlanta] group in your list file, and configure your ssh permissions in that group. Restart all servers in [atlanta] using the following command:

$ ansible atlanta -a "/sbin/reboot"

By default, ansible can execute five processes at the same time. If you have more hosts, ansible will also interact with them, but it will take longer. If you want to restart ten hosts at the same time, you can execute the following command:

$ ansible atlanta -a "/sbin/reboot" -f 10"

/usr/bin/ansible will execute the command with your user name by default. If you want to connect with other users:

$ ansible atlanta -a "/sbin/reboot" -f 10 -u username

Normal restart requires certain permissions. You can use the become keyword to log in with the username user, but execute the command as root:

$ ansible atlanta -a "/sbin/reboot" -f 10 -u username --become [--ask-become-pass]

If you add -- ask come pass or - K, ansible will prompt you for a password to authenticate.

  • Note: the command module does not support additional shell commands such as pip and redirection (even if the environment variables still work). If your command requires shell syntax, please use the shell module. stay this You can see more differences between them on this page.

So far, all our examples adopt the default command module. If you want to use other modules, add - m and module name. For example, use ansible.builtin.shell modular:

$ ansible raleigh -m ansible.builtin.shell -a 'echo $TERM'

When using ansible to execute ad hoc commands, be sure to pay attention to the shell's reference rules, so that the shell will retain local variables and transfer them to ansible. For example, in the above example, the value in double quotation marks will be evaluated.

Management documents

An ad hoc command can transfer multiple files to multiple machines by using the functions of ansible and scp. The following command can directly transfer a file to all nodes of the [atlanta] group:

$ ansible atlanta -m ansible.builtin.copy -a "src=/etc/hosts dest=/tmp/hosts"

If you want to repeat one of these tasks, use it in playbook ansible.builtin.template modular.
ansible.builtin.file The module allows us to change the permissions and owners of files. These options can also be transferred directly to the copy module:

$ ansible webservers -m ansible.builtin.file -a "dest=/srv/foo/a.txt mode=600"
$ ansible webservers -m ansible.builtin.file -a "dest=/srv/foo/b.txt mode=600 owner=mdehaan group=mdehaan"

The file module can also be used to create directories, similar to the mkdir -p command:

$ ansible webservers -m ansible.builtin.file -a "dest=/path/to/c mode=755 owner=mdehaan group=mdehaan state=directory"

Deleting a file is the same as deleting a folder:

$ ansible webservers -m ansible.builtin.file -a "dest=/path/to/c state=absent"

Management package

You may also use ad hoc commands and modules like yum to install, upgrade or uninstall packages on controlled nodes. If you want to ensure that a package is installed without performing an installation:

$ ansible webservers -m ansible.builtin.yum -a "name=acme state=present"

You can confirm the current version of a package by:

$ ansible webservers -m ansible.builtin.yum -a "name=acme-1.5 state=present"

This ensures that a package is the latest version:

$ ansible webservers -m ansible.builtin.yum -a "name=acme state=latest"

This ensures that a package is not installed:

$ ansible webservers -m ansible.builtin.yum -a "name=acme state=absent"

ansible has modules for managing packages under different operating systems. If there is no module to manage your package, please use the command module or create a module for your package management tool.

Manage users and groups

You can ensure that a service has started running on all web servers:

$ ansible webservers -m ansible.builtin.service -a "name=httpd state=started"

You can also restart a service on all web servers in this way:

$ ansible webservers -m ansible.builtin.service -a "name=httpd state=restarted"

Ensure that a service has stopped:

$ ansible webservers -m ansible.builtin.service -a "name=httpd state=stopped"

Collect facts

Facts represents discovered variables on a system. You can use facts to implement conditional tasks, or you can get the current information of your system. If you want to view all facts:

$ ansible all -m ansible.builtin.setup

You can also display only specific facts, which you can view ansible.builtin.setup Module documentation to learn more.
Now that you know the basics of ansible execution, you can continue to learn about repeatable automated tasks ansible playbook.

Keywords: ansible

Added by rami on Sat, 19 Feb 2022 23:13:32 +0200