Ist es möglich, zwei verschiedene Befehle in einen Ad-hoc-Ansible-Befehl einzufügen?

Ist es möglich, zwei verschiedene Befehle in einen Ad-hoc-Ansible-Befehl einzufügen?

Ich muss 2 verschiedene Befehle im Ad-hoc-Modus in Ansible ausführen? Ist das möglich?

1] Hostname

2] df -h

# ansible example -s -a "hostname && df -h"

Fehler

```[root@ansi1 ansible]# ansible example -s -a "hostname && df -h"
ansi2.example.com | FAILED | rc=4 >>
Usage: hostname [-v] {hostname|-F file}      set hostname (from file)
       domainname [-v] {nisdomain|-F file}   set NIS domainname (from file)
       hostname [-v] [-d|-f|-s|-a|-i|-y|-A|-I]  display formatted name
       hostname [-v]                         display hostname

       hostname -V|--version|-h|--help       print info and exit

    dnsdomainname=hostname -d, {yp,nis,}domainname=hostname -y

    -s, --short           short host name
    -a, --alias           alias names
    -i, --ip-address      addresses for the hostname
    -I, --all-ip-addresses all addresses for the host
    -f, --fqdn, --long    long host name (FQDN)
    -A, --all-fqdns        all long host names (FQDNs)
    -d, --domain          DNS domain name
    -y, --yp, --nis       NIS/YP domainname
    -F, --file            read hostname or NIS domainname from given file

   This command can read or set the hostname or the NIS domainname. You can
   also read the DNS domain or the FQDN (fully qualified domain name).
   Unless you are using bind or NIS for host lookups you can change the
   FQDN (Fully Qualified Domain Name) and the DNS domain name (which is
   part of the FQDN) in the /etc/hosts file.
```

Antwort1

Habe meinen Fehler gefunden, dummer

[root@ansi1 ansible]# ansible example -s -m shell -a "hostname && df -h"
ansi2.example.com | SUCCESS | rc=0 >>
ansi2.example.com
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_ansible2-lv_root
                      6.5G  980M  5.2G  16% /
tmpfs                 939M     0  939M   0% /dev/shm
/dev/sda1             477M   54M  398M  12% /boot

Antwort2

Es scheint, ansibledass zum Ausführen Ihres Befehls keine Shell verwendet wird. Sie verwenden jedoch offensichtlich Shell-Syntax in Ihrem benutzerdefinierten Befehl: &&. Eine Möglichkeit für Sie besteht also darin, Ansible direkt anzuweisen, die Shell zu verwenden:

$ ansible localhost -s -a "bash -c 'hostname && df -h'"

verwandte Informationen