2017-05-11 9 views
0

リモートボックスに新しいユーザーを作成し、そのユーザーを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で行うには、これらのタスクを取得するための適切な方法であることはよく分かりません。同じ結果を達成するためのもうひとつの、よりクリーンなソリューションがありますか?

答えて

0

become_userあなたが希望する特権を持つユーザーに設定しました。ユーザーは となります。ログインするユーザーではありません。となって意味するものではありません:はい、 にあなたが、variableファイルを作成し、あなたのグループの変数の在庫group_vars /すべて、脚本のgroup_varsにそれを置くことができ、ホストレベル

- name: Run a command as nobody 
    command: somecommand 
    become: true 
    become_method: su 
    become_user: nobody 
    become_flags: '-s /bin/sh' 
. 

ほんの少しの例に設定することができるようにします/すべて、在庫group_vars/*、脚本のgroup_vars/*または自分の役割の脚本にし、そこからそれを含め、さまざまな役割に新しいユーザーの作成を分離し、このような何か:

--- 
- hosts: redbox1 
    gather_facts: True 
    remote_user: root 
    become: yes 
    become_user: root 
    become_method: sudo 

    pre_tasks: 

     - include_vars: "../environments/{{ env_name }}/group_vars/all" 
     # or just put the var file in role and include it from the `new user` playbook, or you can do it from here. 

     roles: 
     - { role: 'newuser', tags: 'newuser', gather_facts: True, become: yes, become_user: root, become_method: sudo } 
     - { role: 'apache', tags: 'apache', gather_facts: True, become: yes, become_user: ansibleuser, become_method: sudo } 

最初にrootユーザーとして実行し、2番目はansiblとして実行しますユーザー。

関連する問題