Ansible が Windows 上でユーザーを作成すると、ユーザー名に「.EC2AMAZ-ELNOCH3」が追加されます

Ansible が Windows 上でユーザーを作成すると、ユーザー名に「.EC2AMAZ-ELNOCH3」が追加されます

Packer と、プロビジョナーとしての Ansible を使用して、Windows Server 2019 AMI を作成しました。

ユーザー を追加しjenkins、SSH ファイル (公開/秘密キー、known_hosts、authorized_keys) を にコピーしましたC:\Users\jenkins\.ssh

これは私の Ansible プレイブックの関連部分です:

    - name: Ensure user jenkins is present
      ansible.windows.win_user:
        name: jenkins
        password: ***REDACTED***
        state: present
        groups:
          - Users

    - name: Create directory structure
      ansible.windows.win_file:
        path: C:\Temp\
        state: directory

    - name: Allow write and execute access to User jenkins
      ansible.windows.win_acl:
        user: jenkins
        path: C:\Temp
        type: allow
        rights: ExecuteFile,Write

    - name: Copy SSH keys
      ansible.windows.win_copy:
        src: ./files/.ssh
        dest: C:\Users\jenkins
      vars:
        ansible_become_user: jenkins
        ansible_become_password: ***REDACTED***
        # The tmp dir must be set when using win_copy as another user
        # This ensures the become user will have permissions for the operation
        # Make sure to specify a folder both the ansible_user and the become_user have access to (i.e not %TEMP% which is user specific and requires Admin)
        ansible_remote_tmp: C:\Temp

この AMI から EC2 インスタンスを起動します。

sshでログインします:

ssh -i ~/.ssh/***REDACTED***.pem jenkins@ec2-***REDACTED***.compute.amazonaws.com -vvv

SSH キーではログインできませんが、パスワードではログインできます。

これは SSH デバッグ ログです:

debug3: load_hostkeys: loaded 1 keys from ***REDACTED***
debug1: Host 'ec2-***REDACTED***.compute.amazonaws.com' is known and matches the ECDSA host key.
debug1: Found key in /home/amedee/.ssh/known_hosts:161
debug3: send packet: type 21
debug2: set_newkeys: mode 1
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug3: receive packet: type 21
debug1: SSH2_MSG_NEWKEYS received
debug2: set_newkeys: mode 0
debug1: rekey in after 134217728 blocks
debug1: Will attempt key: /home/amedee/.ssh/***REDACTED***.pem  explicit
debug2: pubkey_prepare: done
debug3: send packet: type 5
debug3: receive packet: type 7
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521>
debug3: receive packet: type 6
debug2: service_accept: ssh-userauth
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug3: send packet: type 50
debug3: receive packet: type 51
debug1: Authentications that can continue: publickey,password,keyboard-interactive
debug3: start over, passed a different list publickey,password,keyboard-interactive
debug3: preferred gssapi-with-mic,publickey,keyboard-interactive,password
debug3: authmethod_lookup publickey
debug3: remaining preferred: keyboard-interactive,password
debug3: authmethod_is_enabled publickey
debug1: Next authentication method: publickey
debug1: Trying private key: /home/amedee/.ssh/***REDACTED***.pem
debug3: sign_and_send_pubkey: RSA SHA256:3OWWXRDheAUWZ9kxRiSJPvwFy1/Nh3//CdbLirDuFSM
debug3: sign_and_send_pubkey: signing using rsa-sha2-512 SHA256:3OWWXRDheAUWZ9kxRiSJPvwFy1/Nh3//CdbLirDuFSM
debug3: send packet: type 50
debug2: we sent a publickey packet, wait for reply
debug3: receive packet: type 51
debug1: Authentications that can continue: publickey,password,keyboard-interactive
debug2: we did not send a packet, disable method
debug3: authmethod_lookup keyboard-interactive
debug3: remaining preferred: password
debug3: authmethod_is_enabled keyboard-interactive
debug1: Next authentication method: keyboard-interactive
debug2: userauth_kbdint
debug3: send packet: type 50
debug2: we sent a keyboard-interactive packet, wait for reply
debug3: receive packet: type 51
debug1: Authentications that can continue: publickey,password,keyboard-interactive
debug3: userauth_kbdint: disable: no info_req_seen
debug2: we did not send a packet, disable method
debug3: authmethod_lookup password
debug3: remaining preferred: 
debug3: authmethod_is_enabled password
debug1: Next authentication method: password
jenkins@***REDACTED***.compute.amazonaws.com's password:

すると、Windows から次のように表示されます。

Microsoft Windows [Version 10.0.17763.1637]
(c) 2018 Microsoft Corporation. All rights reserved.

jenkins@EC2AMAZ-ELNOCH3 C:\Users\jenkins.EC2AMAZ-ELNOCH3>

jenkinsしたがって、私はユーザーとしてではなく、ユーザーとしてログインしていますjenkins.EC2AMAZ-ELNOCH3

次のユーザーjenkinsも存在します:

jenkins@EC2AMAZ-ELNOCH3 C:\Users\jenkins.EC2AMAZ-ELNOCH3>dir .. 
 Volume in drive C has no label.
 Volume Serial Number is E43B-9F7E

 Directory of C:\Users

12/11/2020  02:19 PM    <DIR>          .
12/11/2020  02:19 PM    <DIR>          ..
12/11/2020  01:51 PM    <DIR>          Administrator
12/11/2020  02:02 PM    <DIR>          jenkins
12/11/2020  02:22 PM    <DIR>          jenkins.EC2AMAZ-ELNOCH3 
12/12/2018  07:45 AM    <DIR>          Public
               0 File(s)              0 bytes
               6 Dir(s)  12,552,163,328 bytes free

Ansible を使用してコピーした SSH ファイルがあります。

jenkins@EC2AMAZ-ELNOCH3 C:\Users\jenkins.EC2AMAZ-ELNOCH3>dir ..\jenkins\.ssh
 Volume in drive C has no label.
 Volume Serial Number is E43B-9F7E

 Directory of C:\Users\jenkins\.ssh

12/11/2020  02:02 PM    <DIR>          .
12/11/2020  02:02 PM    <DIR>          ..
11/13/2020  10:57 AM             1,221 authorized_keys    
11/13/2020  10:57 AM             1,675 id_rsa
11/13/2020  10:57 AM               401 id_rsa.pub
11/13/2020  10:57 AM             7,962 known_hosts        
               4 File(s)         11,259 bytes
               2 Dir(s)  12,552,081,408 bytes free

これをユーザー名に追加して SSH 接続を試みると.EC2AMAZ-ELNOCH3、パスワードでログインできなくなります。

ssh -i ~/.ssh/***REDACTED***.pem jenkins.EC2AMAZ-ELNOCH3@ec2-***REDACTED***.compute.amazonaws.com -vvv
.
.
.
debug1: Next authentication method: password
jenkins.EC2AMAZ-ELNOCH3@ec2-***REDACTED***.compute.amazonaws.com's password: 
debug3: send packet: type 50
debug2: we sent a password packet, wait for reply
debug3: receive packet: type 51
debug1: Authentications that can continue: publickey,password,keyboard-interactive
Permission denied, please try again.
jenkins.EC2AMAZ-ELNOCH3@ec2-***REDACTED***.compute.amazonaws.com's password:

ユーザー としてログインしたら、からjenkins.EC2AMAZ-ELNOCH3にファイルをコピーして接続を切断します。次回 SSH でログインすると、パスワードなしのログインが機能し、SSH キーは正しいが間違ったディレクトリにあることが証明されます。C:\Users\jenkins\.ssh\C:\Users\jenkins.EC2AMAZ-ELNOCH3\.ssh\

このユーザーが存在することを望みませんjenkins.EC2AMAZ-ELNOCH3。このユーザーだけがjenkins存在するようにするにはどうすればよいでしょうか?

ちなみに、私は Linux にしか精通していないので、回答に高度な Windows 用語が含まれている場合は、詳しく説明してください。私が最後に使用した Windows バージョンは、Windows 3.11 でした。Ansible を使用するのは今回が初めてですが、その要点は理解していると思います。

答え1

jenkins@EC2AMAZ-ELNOCH3jenkinsは、あなたが機械 EC2AMAZ-ELNOCH3
C:\Users\jenkins.EC2AMAZ-ELNOCH3、ログイン時にフォルダーがC:\Users\jenkins既に存在していたが、ユーザーにとって正しいフォルダーではなかったことを Windows が検出したjenkinsため、Windows が新しいユーザー プロファイル フォルダーを作成する必要があったことを意味します。

barというローカル ユーザー アカウントを使用して というマシンにログインするとfoo、既定のユーザー プロファイル フォルダーは になりますC:\Users\foo。ただし、このフォルダーが既に存在し、Windows が実際のユーザー アカウントにマップできない場合 (何らかの理由により)、新しいフォルダーが作成されます。重複するフォルダー名を回避するために、Windows はマシン名をその名前に追加するため、新しいユーザー プロファイル フォルダーの名前は になりますC:\users\foo.bar

これはユーザー名とは何の関係もありませんfoo。ユーザー名は間違いなく です。


なぜこのようなことが起こったのかは分かりません。しかし、これは重複したユーザー プロファイル フォルダーがある場合の Windows の標準的な動作です。

答え2

使用コミュニティ.windows.win_ユーザープロファイル– Windows ユーザー プロファイルを管理します。ユーザーが作成された直後にホーム ディレクトリを作成し、その後でファイルをコピーします。

Ansible でこれを行う方法は次のとおりです。

    - name: Ensure user jenkins is present
      ansible.windows.win_user:
        name: jenkins
        password: ***REDACTED***
        state: present
        groups:
          - Users

    - name: Create a profile for user jenkins at C:\Users\jenkins
      community.windows.win_user_profile:
        username: jenkins
        name: jenkins
        state: present

    - name: Create directory structure
      ansible.windows.win_file:
        path: C:\Temp\
        state: directory

    - name: Allow write and execute access to User jenkins
      ansible.windows.win_acl:
        user: jenkins
        path: C:\Temp
        type: allow
        rights: ExecuteFile,Write

    - name: Copy SSH keys
      ansible.windows.win_copy:
        src: ./files/.ssh
        dest: C:\Users\jenkins
      vars:
        ansible_become_user: jenkins
        ansible_become_password: ***REDACTED***
        # The tmp dir must be set when using win_copy as another user
        # This ensures the become user will have permissions for the operation
        # Make sure to specify a folder both the ansible_user and the become_user have access to (i.e not %TEMP% which is user specific and requires Admin)
        ansible_remote_tmp: C:\Temp

関連情報