再起動せずにグループを作成して使用する 質問する

再起動せずにグループを作成して使用する 質問する

グループを作成するタスクがあります。

- name: add user to docker group
  user: name=USERNAME groups=docker append=yes
  sudo: true

別のプレイブックでは、新しいグループの権限を必要とするコマンドを実行する必要があります。残念ながら、新しいグループはログアウトして再度ログインした後にのみ読み込まれるため、これは機能しません。

私は次のようなことを試しました:

su -l USERNAME

または

newgrp docker; newgrp

しかし、何も機能しませんでした。Ansible を強制的にホストに再接続して再ログインさせる変更はありますか? 再起動が最後のオプションになります。

ベストアンサー1

あなたはansible.builtin.meta: reset_connectionタスク:

- name: Add user to docker group
  ansible.builtin.user:
    name: USERNAME
    groups: docker
    append: true

- name: Reset ssh connection to allow user changes to affect ansible user
  ansible.builtin.meta:
    reset_connection

以下の点に注意してくださいないタスクが条件付きをサポートしていないため、変数を使用してタスクがansible.builtin.user変更された場合にのみタスクを実行します。reset_connectionwhen#27565

メタタスクreset_connectionはAnsible 2.3で追加されましたが、v2.5.8を除外するまで少しバグが残っていました。#27520

reset_connectionタスク自体は条件をサポートしていませんがwhen、次のように条件付きタスクの包含を使用できます。

- name: Add user to docker group
  ansible.builtin.user:
    name: USERNAME
    groups: docker
    append: true
  register: add_to_docker_group_result

- name: Include reset connection tasks
  ansible.builtin.include_tasks: reset_connection.yaml
  when: add_to_docker_group_result.changed == true

reset_connection.yaml含めるファイルは次のとおりです:

- name: Reset ssh connection
  ansible.builtin.meta: reset_connection

おすすめ記事