Ansible: 2倍になる

Ansible: 2倍になる

次のように Ansible を実行したいと思います。

  • リモートユーザーは「通常」のユーザーです

  • ...一連のタスクを実行するためにbecomeルート権限を持つ人sudo

  • ... root から whobecomeの postgres ユーザーを経由してsu - postgres、PostgreSQL データベースを作成します。

suこれは PostgreSQL の世界では一般的な手順です。からのみアクセスできる特別なユーザーですroot

プレイで become-ish パラメータを指定すると、CLI オプションによってプレイで定義されている内容が上書きされるため、ルートとして接続している場合にのみ機能します。

sudoルールを追加したり実行したりする以外に、これを実現するよりエレガントな方法はありますか?

command: /usr/bin/su - postgres -c '/bin/createuser -D -R -S myuser

ansible から警告を消すように指示しますか?

答え1

有効なユーザーをなぜ 2 回変更する必要があるのでしょうか?

タスクごとに Ansible を実行するために必要な有効なユーザー ID を指定できるため、postgres ユーザーとしてコマンドを実行する必要がある場合は、それを直接設定します。

tasks:
  - name: Create Postgres User
    shell: /bin/createuser -D -R -S myuser
    become: yes
    become_user: postgres

多くの場合、ユーザーがsudo権限を取得すると、rootすでに取得している権限が与えALLられ、どれでもユーザー(postgres を含む)。

あるいは、ansible ユーザーが postgres ユーザーとして (特定の) コマンドを実行できるように、追加の sudo 権限を作成します。

注記: Ansible Postgresの使用を検討してくださいデータベースモジュールデータベース、ロール、権限を管理します。


あなたのコメントへの返信: 私は通常のユーザーとして Ansible に接続します:

ansible example -a "/bin/whoami"
example | SUCCESS | rc=0 >>
hbruijn

/etc/sudoers.d/hbruijn私 (Ansible ユーザー) が実行できることに何の制限も課さない、かなり典型的な " " スニペットがあります。

# /etc/sudoers.d/hbruijn
hbruijn  ALL = NOPASSWD: ALL

そして私はbecomesudo最初にまたはを呼び出さずに、任意のユーザーが別のユーザー ID とタスクでコマンドを実行できますsu

ansible example -b --become-user=root  -a "/bin/whoami"
example | SUCCESS | rc=0 >>
root

または:

ansible example -b --become-user=postgres  -a "/bin/whoami"
example | SUCCESS | rc=0 >>
postgres

Ansible がスーパーユーザーとして直接接続する必要はありません。

答え2

postgres基本的に、ルート ブロック内のユーザーのタスクを実行する必要があります。例を以下に示します。

python.yml のサンプル

- hosts: testingserver
  gather_facts: false
  tasks:
    - name: this is me without become
      command: whoami

    - name: this is my sudo `root` block
      become: true
      block:
        - name: checking who I am
          command: whoami

        - name: this is me becoming `postgres`
          become_user: postgres
          command: whoami

出力:

$ ANSIBLE_CONFIG=ansible.cfg ansible-playbook -i inven pb.yml -K -v
BECOME password:

PLAY [testingserver] ***********************************************************************************************************************************************************************************************

TASK [this is me without become] ***********************************************************************************************************************************************************************************
changed: [testingserver] => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"}, "changed": true, "cmd": ["whoami"], "delta": "0:00:00.006233", "end": "2023-07-05 11:01:57.229844", "msg": "", "rc": 0, "start": "2023-07-05 11:01:57.223611", "stderr": "", "stderr_lines": [], "stdout": "testinguser", "stdout_lines": ["testinguser"]}

TASK [checking who I am] *******************************************************************************************************************************************************************************************
changed: [testingserver] => {"changed": true, "cmd": ["whoami"], "delta": "0:00:00.006191", "end": "2023-07-05 11:01:57.964360", "msg": "", "rc": 0, "start": "2023-07-05 11:01:57.958169", "stderr": "", "stderr_lines": [], "stdout": "root", "stdout_lines": ["root"]}

TASK [this is me becoming `postgres`] *************************************************************************************************************************************************************************
changed: [testingserver] => {"changed": true, "cmd": ["whoami"], "delta": "0:00:00.003580", "end": "2023-07-05 11:01:58.768622", "msg": "", "rc": 0, "start": "2023-07-05 11:01:58.765042", "stderr": "", "stderr_lines": [], "stdout": "postgres", "stdout_lines": ["postgres"]}

PLAY RECAP *********************************************************************************************************************************************************************************************************
testingserver               : ok=3    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

これはあなたのケースにおける私の Linux ボックス環境です:

  • 通常のユーザーはtestinguserです
  • 通常のユーザーが su を実行して postgres にアクセスするにはパスワードが必要です
  • 通常ユーザーからrootへのsudo
  • postgres にルート su する
$ ssh testingserver
[testinguser@testingserver ~]$ su - postgres
Password:
[testinguser@testingserver ~]$ sudo -i
[sudo] password for testinguser:
[root@testingserver ~]# su - postgres
[postgres@testingserver ~]$ whoami
postgres

関連情報