コマンドは正常に機能しますが、SSH経由でリモートで実行すると失敗します。

コマンドは正常に機能しますが、SSH経由でリモートで実行すると失敗します。

サーバーで実行すると、次のコマンドはリモートAiXサーバーで正しく機能します。

ls -ltr /tmp/*.pid  | grep "$(date '+%b %e')"

出力:

-rwx-rwx-rw-- ........ Oct 1 /tmp/new.pid

echo $?
0

を使用して上記のコマンドを実行しようとしましたが、ssh失敗しました。以下の出力を参照してください。

ssh [email protected] -C 'ls -ltr /tmp/*.pid  | grep "$(date "'"+%b %e"'")"'

echo $?
1

注:このコマンドはansible自動化によって取得されます"$(date '+%b %e')"grep "$(date "'"+%b %e"'")"

-name: Chek is pid file was modified today
 raw: "ls -ltr /tmp/{{ another_folder }}/*.pid  | grep \"$(date '+%b %e')\""
 register: pidfiledet

この問題を解決し、コマンドをリモートで実行する方法を提案しますか?

更新:回答と説明セクションでユーザーが提案したコマンドを実行しましたが、sshが失敗しました。以下は、提案された回答のデバッグモードで失敗したSSHの出力です。

$ ssh -vvv -C -o ControlMaster=auto -o ControlPersist=60s -o 'IdentityFile="/app/mysshkeys/id_rsa"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="tbaadm"' -o ConnectTimeout=10 -o StrictHostKeyChecking=no -o ControlPath=/home/targetuser/.ansible/cp/f623d39604 -tt 10.9.9.12 'ls -ltr /tmp/prjfolder/bin/*.pid | grep "$(date +%b\ %e)"'
OpenSSH_7.4p1, OpenSSL 1.0.2k-fips  26 Jan 2017
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 58: Applying options for *
debug3: kex names ok: [curve25519-sha256,[email protected],diffie-hellman-group14-sha1,diffie-hellman-group14-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha1,diffie-hellman-group-exchange-sha256,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,gss-gex-sha1-,gss-group14-sha1-]
debug1: auto-mux: Trying existing master
debug2: fd 3 setting O_NONBLOCK
debug2: mux_client_hello_exchange: master version 4
debug3: mux_client_forwards: request forwardings: 0 local, 0 remote
debug3: mux_client_request_session: entering
debug3: mux_client_request_alive: entering
debug3: mux_client_request_alive: done pid = 73756
debug3: mux_client_request_session: session request sent
debug1: mux_client_request_session: master session id: 2
debug3: mux_client_read_packet: read header failed: Broken pipe
debug2: Received exit status from master 1
Shared connection to 10.9.9.12 closed.

SSHを使用して渡された上記のコマンドは、ターゲットホストでローカルで実行されると正しく機能します。

ベストアンサー1

残念ながら、私はそれに慣れていませんansibleが、混在しているように見え、ansible問題は参照を破るようにコマンドを変換する別のプロセスなので、コマンドのその部分から参照を削除するのが役立ちます。

次の基本的なコマンドを試してみる価値があります。

ls -ltr /tmp/*.pid  | grep "$(date +%b\ %e)"

翻訳者がこの内容も破ることができるか疑問が残っています。

おすすめ記事