Ansible:變成兩倍

Ansible:變成兩倍

我想運行 ansible 這樣:

  • 遠端用戶是「普通」用戶

  • ……誰become透過 rootsudo來運行一堆任務

  • ……誰是becomepostgres 用戶su - postgres,透過 root 來建立 PostgreSQL 資料庫。

su這是 PostgreSQL 世界的典型過程:只能透過from存取的特殊使用者root

只有在以 root 身分連線時,才能在 play 中指定「become-ish」參數,因為 CLI 選項會覆寫 play 中定義的任何內容。

除了添加sudo規則或運行之外,是否有更優雅的方法來實現此目的

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

來自ansible,告訴它關閉警告?

答案1

為什麼必須更改有效用戶兩次?

您可以指定 ansible 需要根據任務運行的有效使用者 ID,因此當您需要以 postgres 使用者身分執行命令時,請直接設定。

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

通常,當用戶獲得sudo權限時,root他們已經獲得ALL並成為任何用戶,包括 postgres。

或者,建立額外的 sudo 權限,以便您的 ansible 使用者可以作為 postgres 使用者執行(特定)命令。

筆記:考慮使用 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

然後我就可以become任何使用者可以在不同的使用者 ID 和任務下執行命令,而無需先呼叫sudosu呼叫:

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在根區塊中為使用者執行任務。範例如下:

PB.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 box 環境:

  • 普通用戶正在測試用戶
  • 一般用戶 su 到 postgres 需要密碼
  • 普通用戶 sudo 到 root
  • root su 到 postgres
$ ssh testingserver
[testinguser@testingserver ~]$ su - postgres
Password:
[testinguser@testingserver ~]$ sudo -i
[sudo] password for testinguser:
[root@testingserver ~]# su - postgres
[postgres@testingserver ~]$ whoami
postgres

相關內容