リモートボックスに新しいユーザーを作成し、そのユーザーをsudo
ユーザーにして、root
ユーザーのSSHログインを無効にするAnsibleタスクがあります。その後、リモートボックス上のすべてのタスクがこの新しいユーザーとして実行されます。同じプレイ/プレイブックの新しいユーザーに切り替える方法はありますか?上記のように異なるユーザーと異なるタスクを実行します
#!/bin/bash
# Install a new remote user with 'sudo' privileges
ansible-playbook initial_setup/main.yml
# Start main installation process
ansible-playbook main.yml
:例えば
が、これは現在のディレクトリ構造がどのように見えるされているもの:現時点では
|-- initial_setup
| |-- change_hostname.yml
| |-- main.yml
| `-- newuser.yml
|-- main.yml
|-- README.md
|-- roles
| `-- apache
| |-- conf
| | `-- httpd.conf
| |-- handlers
| | `-- main.yml
| `-- tasks
| `-- main.yml
|
`-- start_install.sh
、私は、手続を開始するstart_install.sh
スクリプトを実行します。最初にinitial_setup/main.yml
を実行してから、「適切な」プレイブックに進みます。これらの両方は以下のとおりである:
initial_setup/main.yml
[[email protected] record1]# cat initial_setup/main.yml
- include: newuser.yml
- include: change_hostname.yml
initial_setup/newuser.yml
---
- hosts: redbox1
remote_user: root
vars:
NEW_SUDO_USER: 'ansibleuser'
tasks:
- name: Create a new secondary, non-root, sudo user
user: name={{ NEW_SUDO_USER }}
password='testpass'
shell='/bin/bash'
# followed by the tasks to give the user sudo privileges,
# allow SSH access to the new user, remove root SSH etc.
、次いで適切なハンドブックが(./main.yml
)は同様に、始まる:
[[email protected] record1]# cat main.yml
---
- hosts: redbox1
remote_user: ansibleuser
become: yes
become_method: sudo
roles:
- apache
私はこれが(つまり、異なるremote_user
を利用し、それぞれが異なる演劇を、実行するシェルスクリプトを使用して、ある)Ansibleで行うには、これらのタスクを取得するための適切な方法であることはよく分かりません。同じ結果を達成するためのもうひとつの、よりクリーンなソリューションがありますか?