12
16

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でLinuxユーザ作成

Last updated at Posted at 2017-06-01

#はじめに

  • 試行用の最小コードのため、ベストプラクティスに従ったスクリプト構成になっていません。
  • パスワードが平文でスクリプトに記載されているため、本運用時はansible vault などの利用を検討して下さい。
  • ansible 2.3.0.0, RHEL 7.2 の環境で試行しました。

#接続対象サーバの設定

inventory/test_inventory.ini
[test_servers]
localhost
ansible-target-01

#スクリプト本体

create-user.yml
---
- hosts: test_servers
  become: yes  #sudoで実行する
  vars_files:
    - user-list-var.yml
  tasks:
  - name: Create user
    user:
      #state: absent #ユーザを削除するときはコメントアウトを外す
      name: "{{ item.name }}"
      password: "{{ item.password }}"
      groups:  "{{ item.group }}"
      shell: /bin/bash
    with_items:
      - "{{ users }}"
    when: users #usersが1件以上の場合のみ実行

#作成対象ユーザのリスト

user-list-var.yml
users:
  - { name: 'user01', uid: 1001, group: 'users', password: "{{ 'passw0rd'|password_hash('sha512') }}", update_password: 'on_create', comment: 'user01' }
  - { name: 'user02', uid: 1002, group: 'users', password: "{{ 'passw0rd'|password_hash('sha512') }}", update_password: 'on_create', comment: 'user02' }

#実行

ansible-playbook -i inventory/inventory.ini create-user.yml --ask-pass --ask-sudo-pass

#参考

12
16
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
12
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?