10
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ansibleでユーザとssh鍵を一括登録する

Last updated at Posted at 2016-01-06

ansibleでユーザ登録からssh公開鍵登録をまとめて行おうとしたのでやり方をまとめました。
LDAPを使うっていうのはとりあえず無しで。

やりたかったこと

  • グループ登録
  • ユーザ登録
  • ユーザの作成済みssh公開鍵をサーバへコピー

利用したもの

  • groupモジュール
  • userモジュール
  • authorized_keyモジュール

作ったもの

tasks/main.yml
- name: Add group
  group: name="{{item.name}}" gid="{{item.gid}}" state="{{item.state}}"
  with_items: "{{group}}"

- name: Add users
  user: name="{{item.name}}" uid={{item.uid}} comment="{{item.comment}}" group="{{item.group}}" state="{{item.state}}"
  with_items: "{{users}}"

- name: Add user ssh keys
  authorized_key: >
     user="{{item.name}}"
     key="{{ lookup('file', item.key) }}"
  with_items: "{{users}}"
vars/main.yml
users:
  - { name: 'user1',  uid: 5001, group: 'users' , comment: 'user1', home: '/home/user1', createhome: 'yes', state: 'present' , key: 'keys/user1'}
  - { name: 'user2',  uid: 5002, group: 'users' , comment: 'user2', home: '/home/user2', createhome: 'yes', state: 'present' , key: 'keys/user2'}

group:
 - { name: 'users', gid: 5000, state: 'present'}

ファイル配置

ロール名は適切につけてください。

somerole
├── files
│   └── keys
│       ├── user1
│       └── user2
├── tasks
│   └── main.yml
└── vars
    └── main.yml
10
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?