Get the App
SLTechnology News&Howtos  ›  Servers  › 

Idempotency in ansible

Shulou Source: shulou.com Published: 2022-06-02 06:08:16 10月01日 Update

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

Tags: Commands modules parameters files scripts configurations times features directories interpreters interpreters side effects can be passed that is meaning examples first time network cards procedures logic Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi OPPO Reno Microsoft MariaDB Shulou Tech Info