Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
14
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

posted at

updated at

AnsibleでLinuxユーザ作成

はじめに

  • 試行用の最小コードのため、ベストプラクティスに従ったスクリプト構成になっていません。
  • パスワードが平文でスクリプトに記載されているため、本運用時は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

参考

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
14
Help us understand the problem. What are the problem?