ansibleを介して/ etc / sudoersに新しい行を追加します。

ansibleを介して/ etc / sudoersに新しい行を追加します。

/etc/sudoersansibleを介してファイルに新しい行を追加しようとしています。しかし、まだ動作しません。何が問題ですか?

ファイルを書き込み可能にvisudo -cするコマンドを試しました。sudoers

~を除く

sudo echo "test ALL=(ALL)       NOPASSWD: ALL" >> /etc/sudoers

すべての行が有効です。

これは私が実行したshファイルですが、そのうちの何も動作しませんでした。

 sudo adduser test
 sudo echo "******" | passwd --stdin test
 sudo cp /etc/sudoers /etc/sudoers_20180418
 sudo echo "test ALL=(ALL)       NOPASSWD: ALL" >> /etc/sudoers
 sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config20180418
 sudo sed -i 's/AllowUsers/AllowUsers test/g' /etc/ssh/sshd_config

.shファイル

 sudo adduser test
 sudo echo "******" | passwd --stdin test
 sudo cp /etc/sudoers /etc/sudoers_20180418
 visudo -c
 sudo echo "test ALL=(ALL)       NOPASSWD: ALL" >> /etc/sudoers
 sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config20180418
 sudo sed -i 's/AllowUsers/AllowUsers test/g' /etc/ssh/sshd_config

ベストアンサー1

問題はコマンドラインかもしれません。

sudo echo "test ALL=(ALL)       NOPASSWD: ALL" >> /etc/sudoers

コマンドの出力は元のユーザーのIDを使用してリダイレクトされます。sudo書き込みにはrootアクセス権が必要なため、/etc/sudoersこの操作は失敗します。コマンドはechoroot として実行されますが、リダイレクトは実行されません。これはsudo echo ...、コマンドラインを実行する準備が整ったシェルによってすでに設定されています。ここではsudoを使用しているので、シェルがrootとして実行されていないようです。

次のように書き換えることができます。

echo "test ALL=(ALL)       NOPASSWD: ALL" | sudo tee -a /etc/sudoers >/dev/null

このバージョンでは:

  • コマンドはecho元のユーザーとして実行されます。
  • /dev/null へのリダイレクトは元のユーザーとして行われます。
  • ただし、tee -a <filename>パイプされた入力のコピーを指定されたファイルにルートとして追加することが必要です。

おすすめ記事