LoginSignup
14

More than 5 years have passed since last update.

ansibleでuserが存在しない場合のみuserを作成する方法

Last updated at Posted at 2016-01-07

概要

userのアカウントをansibleで作成したい場合に、既にuserが存在する場合は、それを変えたくない場合がありますが、あまり良い方法がありません。

あまり格好良くないのですが、以下のような案を考えてみました。
最初の版では、サーバの複数のアカウントを変更する場合に問題があったので、手順2.を修正しました。

手順

1.以下のタスクで、ユーザが存在するかチェックする。

- name: check user
  user: name={{ item.name }} password={{ initial_password }} update_password=on_create state=present
  with_items: myusers
  register: u

2. check user の結果に基づき、アカウントを作成(更新)する。

- name: create users
  user: name={{ item[0].name }} uid={{ item[0].uid }} group={{ item[0].group }} groups={{ item[0].groups }} password={{ initial_password }} update_password=on_create state=present
  with_together:
    - myusers
    - u.results
  ignore_errors: true
  when: item[1].changed is not defined or item[1].changed == true

with_togetherを使ったので、分かりにくくなってしまいました。
もっと格好良い方法は、ないのですかね?

以上

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
14