Get the App
SLTechnology News&Howtos  ›  Servers  › 

What is idempotency in ansible? How do I use it?

Shulou Source: shulou.com Published: 2022-06-03 04:51:28 09月27日 Update

What is idempotency in ansible? How do I use it? In fact, all these troubles can be summed up as a problem, that is, the use of idempotency in ansible? It's not difficult to solve this problem, so let's take a look at the use of idempotency in ansible.

1. Idempotency in ansible

The vast majority of modules in ansible are idempotent, meaning that execution in turn or multiple times has no side effects. However, shell, command, script and raw are not idempotent, and all operations are repeated, but some operations are not allowed to be repeated, such as MySQL's initialization command mysql_install_db, which is logically initialized in sequence during the first configuration and should not be executed at any other time. Therefore, whenever you use these four modules, think about whether repeated execution of this command will have a negative impact.

Of course, of the above four modules, except for the raw module, all provide parameters that implement idempotent properties, namely creates and removes:

Creates parameter: the command is not executed when the specified file or directory exists; removes parameter: when the specified file or directory does not exist, the command is not executed

Examples are as follows:

-name: use some module hosts: node gather_facts: false tasks: # do not execute if the network card configuration file does not exist-name: use command module command: ifup ens33 args: removes: / etc/sysconfig/network-scripts/ifcfg-ens33 # mysql configuration file does not exist, avoid overwriting-name: use shell module shell: cp / tmp/my.cnf / etc/my.cnf args: creates: / etc/my.cnf

After testing, it can be proved that after using the removes or creates parameters, you can achieve idempotency and ensure that the command will not be repeated.

None of these four modules is limited to executing shell commands or shell scripts. You can specify other interpreters through the executable parameter, such as expec executes expect scripts, perl interpreters execute perl scripts, and so on. As follows:

[root@ansible ansible] # cat test.yaml-name: RUN a perl script script: / some/local/script.pl args: executable: perl

The above is the use of idempotency in ansible introduction, detailed use of their own use to know the specific essentials. If you want to read more related articles, you are welcome to follow the industry information channel!

Tags: Commands modules parameters files usage methods scripts configurations that is time features directories interpreters problems explanations contents side effects can be passed case meaning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Linux Huawei NVidia Redmi