Idempotentes Kopieren
In diesem Schritt werden Sie die Kopieroperation idempotent machen, indem Sie das Ansible Copy-Modul verwenden. Sie stellen sicher, dass die Datei nur kopiert wird, wenn sie auf dem Remote-Host nicht existiert oder wenn sich die Quelldatei geändert hat.
Zunächst ändern Sie die vorhandene Playbook-Datei, indem Sie den gesamten Inhalt entfernen und den folgenden Inhalt zur Playbook-Datei hinzufügen:
- hosts: localhost
tasks:
- name: Idempotent copy
copy:
src: /home/labex/file.txt
dest: /tmp/file2.txt
remote_src: yes
remote_src: yes
: Dieser Parameter gibt an, dass der src
-Pfad auf dem Remote-Host liegt, wodurch Ansible Dateien vom Remote-Host an einen anderen Ort übertragen kann.
Durch diese Playbook-Aufgabe wird die Datei /home/labex/file.txt
, die sich auf dem Remote-Host befindet, nur dann nach /tmp/file2.txt
auf dem Remote-Host kopiert, wenn sie nicht existiert oder wenn sich die Quelldatei geändert hat.
Als Nächstes prüfen Sie, ob die Datei /tmp/file2.txt
existiert.
ll /tmp/file2.txt
Beispielausgabe:
ls: cannot access '/tmp/file2.txt': No such file or directory
Führen Sie dann das Playbook mit dem folgenden Befehl aus:
ansible-playbook copy-module-playbook.yaml
Beispielausgabe:
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'
PLAY [localhost] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [localhost]
TASK [Idempotent copy] *********************************************************
changed: [localhost]
PLAY RECAP *********************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Verifizieren Sie abschließend, dass die Datei file2.txt
nur dann in den angegebenen Zielpfad auf dem Remote-Host kopiert wird, wenn sie nicht existiert oder wenn sich die Quelldatei geändert hat.
ll /tmp/file2.txt
Beispielausgabe:
-rw-rw-r-- 1 labex labex 33 Mar 9 08:34 /tmp/file2.txt
Führen Sie nun das Playbook erneut aus.
ansible-playbook copy-module-playbook.yaml
Beispielausgabe:
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'
PLAY [localhost] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [localhost]
TASK [Idempotent copy] *********************************************************
ok: [localhost]
PLAY RECAP *********************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Beachten Sie, dass changed=1
zu changed=0
wird, was beweist, dass dieses Ansible-Playbook erkennen kann, dass die Datei nur kopiert wird, wenn sie auf dem Remote-Host nicht existiert oder wenn sich die Quelldatei geändert hat.