Ansible: Wie kopiert man Dateien, Verzeichnisse oder Skripte vom lokalen Host auf den Remote-Host?
Veröffentlicht: 2019-02-24 Als Teil der IT-Verwaltungsabteilung haben die Leute mit lots of scripts
und Befehlszeilentools zu tun. Ich bin mir sicher, dass Sie täglich mit Situationen konfrontiert sind, in denen Sie Dateien oder Skripte auf Tausende von Remote-Hosts kopieren müssen.
Mitarbeiter der IT-Abteilung melden sich nicht einfach bei thousands of hosts
und führen Skripte manuell aus. Und deshalb kommt Ansible ins Bild. Ansible ist eine sehr leistungsstarke IT-Management-Lösung, die für uns kostenlos zur Verfügung steht.
In diesem Tutorial werden wir die Schritte zum Kopieren von Dateien auf den Remote-Host mit Ansible durchgehen.
Was wir tun werden?
Kopieren Sie die Datei crunchify.txt
vom Speicherort /opt/ashah/
in das Zielverzeichnis /opt/ashah/
opt/ashah/ des Remote-Hosts 192.66.129.83
.
Schritt 1
Installieren Sie Ansible unter Linux Ubuntu oder macOS. Folgen Sie den Tutorials, um detaillierte Schritte durchzugehen.
Schritt 2
Lassen Sie uns einige Grundlagen und den Dateispeicherort für dieses Tutorial verstehen:
- Speicherort der Konfigurationsdatei: /etc/ansible/ansible.cfg
- Skriptdatei: /opt/ashah/crunchify.yml
- hosts-Datei: /opt/ashah/hosts
- zu kopierende Datei: /opt/ashah/crunchify.txt (Testdatei)
Schritt 3
Inhalt crunchify.yml
-Datei.
1 2 3 4 5 6 7 8 |
root @ localhost : / opt / ashah # cat crunchify.yml --- - hosts : crunchify - group tasks : - name : copy file to remote host copy : src : / opt / ashah / crunchify . txt dest : / opt / ashah |
Schritt 4
hosts
-Dateiinhalt. Stellen Sie sicher, dass Sie die IP-Adresse Ihres Remote-Hosts ändern.
1 2 3 4 |
root @ localhost : / opt / ashah # cat hosts #crunchify-group as a sample [ crunchify - group ] 192.66.129.83 |
Schritt-5
Erstellen Sie die Datei crunchify.txt
mit dem folgenden Inhalt.
1 2 3 4 |
root @ localhost : / opt / ashah # cat crunchify.txt This is sample example copying file to remote location using Ansible . More ansible tutorials at https : //crunchify.com/tag/ansible/ |
Schritt-6
Führen Sie diesen Befehl aus, um die Datei auf den Remote-Host zu kopieren:
-
Command
: ansible-playbook -b -vvv -u root crunchify.yml -kkkk –extra-vars „crunchify-group“ -i hosts
Lassen Sie uns alle Parameter verstehen:
- -b : Operationen mit Become ausführen (dasselbe wie —become).
- -vvv : Ausführlicher Modus (-vvv für mehr, -vvvv zum Aktivieren des Verbindungs-Debugging).
- -u : Als dieser Benutzer verbinden (Standard = Keiner).
- -kkkk : Fragt nach dem Verbindungspasswort.
- –extra-vars : Setze zusätzliche Variablen als Schlüssel=Wert oder YAML/JSON, wenn dem Dateinamen ein @ vorangestellt ist.
- -i : Inventarhostpfad oder kommagetrennte Hostliste angeben.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
root @ localhost : / opt / ashah # ansible-playbook -b -vvv -u root crunchify.yml -kkkk --extra-vars "crunchify-group" -i hosts Using / etc / ansible / ansible . cfg as config file SSH password : / opt / ashah / hosts did not meet host_list requirements , check plugin documentation if this is unexpected / opt / ashah / hosts did not meet script requirements , check plugin documentation if this is unexpected Parsed / opt / ashah / hosts inventory source with ini plugin PLAYBOOK : crunchify . yml * ************************************************************************************************************************ 1 plays in crunchify . yml PLAY [ crunchify - group ] ********************************************************************************************************************************* TASK [ Gathering Facts ] ************************************************************************************************************************ < 192.66.129.83 > ESTABLISH SSH CONNECTION FOR USER : root < 192.66.129.83 > SSH : EXEC sshpass - d9 ssh - C - o ControlMaster = auto - o ControlPersist = 60s - o User = root - o ConnectTimeout = 10 - o ControlPath =/ root / . ansible / cp / 5fca43321f 192.66.129.83 '/bin/sh -c ' "'" 'echo ~root && sleep 0' "'" '' < 192.66.129.83 > ( 0 , '/root\n' , '' ) < 192.66.129.83 > ESTABLISH SSH CONNECTION FOR USER : root < 192.66.129.83 > SSH : EXEC sshpass - d9 ssh - C - o ControlMaster = auto - o ControlPersist = 60s - o User = root - o ConnectTimeout = 10 - o ControlPath =/ root / . ansible / cp / 5fca43321f 192.66.129.83 '/bin/sh -c ' "'" '( umask 77 && mkdir -p "` echo /root/.ansible/tmp/ansible-tmp-1550966284.57-6253496643791 `" && echo ansible-tmp-1550966284.57-6253496643791="` echo /root/.ansible/tmp/ansible-tmp-1550966284.57-6253496643791 `" ) && sleep 0' "'" '' < 192.66.129.83 > ( 0 , 'ansible-tmp-1550966284.57-6253496643791=/root/.ansible/tmp/ansible-tmp-1550966284.57-6253496643791\n' , '' ) Using module file / usr / lib / python2 . 7 / dist - packages / ansible / modules / system / setup . py < 192.66.129.83 > PUT / root / . ansible / tmp / ansible - local - 32435HXzj7o / tmpjFxUXp TO / root / . ansible / tmp / ansible - tmp - 1550966284.57 - 6253496643791 / AnsiballZ_setup . py < 192.66.129.83 > SSH : EXEC sshpass - d9 sftp - o BatchMode = no - b - - C - o ControlMaster = auto - o ControlPersist = 60s - o User = root - o ConnectTimeout = 10 - o ControlPath =/ root / . ansible / cp / 5fca43321f '[192.66.129.83]' < 192.66.129.83 > ( 0 , 'sftp> put /root/.ansible/tmp/ansible-local-32435HXzj7o/tmpjFxUXp /root/.ansible/tmp/ansible-tmp-1550966284.57-6253496643791/AnsiballZ_setup.py\n' , '' ) < 192.66.129.83 > ESTABLISH SSH CONNECTION FOR USER : root < 192.66.129.83 > ESTABLISH SSH CONNECTION FOR USER : root < 192.66.129.83 > SSH : EXEC sshpass - d9 ssh - C - o ControlMaster = auto - o ControlPersist = 60s - o User = root - o ConnectTimeout = 10 - o ControlPath =/ root / . ansible / cp / 5fca43321f 192.66.129.83 '/bin/sh -c ' "'" 'rm -f -r /root/.ansible/tmp/ansible-tmp-1550966284.57-6253496643791/ > /dev/null 2>&1 && sleep 0' "'" '' < 192.66.129.83 > ( 0 , '' , '' ) ok : [ 192.66.129.83 ] TASK [ deploy topology ] ******************************************************************************************************************************************** ok : [ 192.66.129.83 ] = > { "changed" : false , "checksum" : "289b0c36372210c92bf028f2a19d57333af4d63c" , "dest" : "/opt/ashah/crunchify.txt" , "diff" : { "after" : { "path" : "/opt/ashah/crunchify.txt" } , "before" : { "path" : "/opt/ashah/crunchify.txt" } } , "gid" : 0 , "group" : "root" , "invocation" : { "module_args" : { "_diff_peek" : null , "_original_basename" : "crunchify.txt" , "access_time" : null , "access_time_format" : "%Y%m%d%H%M.%S" , "attributes" : null , "backup" : null , "content" : null , "delimiter" : null , "dest" : "/opt/ashah" , "directory_mode" : null , "follow" : true , "force" : false , "group" : null , "mode" : null , "modification_time" : null , "modification_time_format" : "%Y%m%d%H%M.%S" , "owner" : null , "path" : "/opt/ashah/crunchify.txt" , "recurse" : false , "regexp" : null , "remote_src" : null , "selevel" : null , "serole" : null , "setype" : null , "seuser" : null , "src" : null , "state" : "file" , "unsafe_writes" : null } } , "mode" : "0644" , "owner" : "root" , "path" : "/opt/ashah/crunchify.txt" , "size" : 21 , "state" : "file" , "uid" : 0 } PLAY RECAP * ******************************************************************************************************************************************************* 192.66.129.83 : ok = 2 changed = 0 unreachable = 0 failed = 0 |
Das ist es. Sie haben die Datei erfolgreich kopiert.

Wie kann ich überprüfen, ob die Datei auf den neuen Remote-Host kopiert wurde?
Melden Sie sich einfach beim Remote-Host an und überprüfen Sie den Speicherort /opt/ashah
, und Sie werden die Datei crunchify.txt
sehen
Wie kopiere ich einen Verzeichnisinhalt in ein anderes Verzeichnis?
Ändern Sie einfach den src
und desc
Wert der Datei crunchify.yml
. Hier ist eine aktualisierte crunchify.yml-Datei.
1 2 3 4 5 6 7 8 |
root @ localhost : / opt / ashah # cat crunchify.yml --- - hosts : crunchify - group tasks : - name : copy all files from folder to remote host copy : src : / opt / ashah / dest : / opt / ashah / |
Alle Dateien im Ordner /opt/ashah/
werden jetzt auf Remote-Hosts kopiert.
Lassen Sie mich wissen, wenn Sie Fragen zum Kopieren von Dateien an entfernte Speicherorte mit Ansible haben.