1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Ansible学習メモ①(LVM作成、ユーザ管理)

Last updated at Posted at 2025-04-14

業務で最近Ansibleを使い始めました。私自身はエンジニア職ではないので普段コーディングをほとんどしないです。少しAnsibleがわかるように学習し始めています。

Ansibleとは

対象マシンの各ソフトウェアのインストール、設定ファイルの編集、パッチ適用などを自動化できるツールです。

Terraformとの違いに関して

AnsibleとTerraform両製品は、Devops用の自動化ツールとしてコードに基づき、自動的にインフラ構築~構成管理を実現できるという点でほぼ同じのように見えますが、実は得意とする領域が異なります。

Terraformはクラウドインフラの構築に優れていうことに対して、Ansibleの場合対象インフラ上にソフトウェアやファイルの構成管理が得意なようです。

Ansible使用例

今回Linux上にAnsibleを実施、対象マシンのOSも同じLinux。

1.LVMパーティションを作成

使用するモジュール:community.general.parted
※こちらのモジュールを使用する前に以下のcollectionをインストールする必要がある。

ansible-galaxy collection install community.general

マニフェストファイルを以下のように作成

---
- name: disk demo
  hosts: all
  become: true
  tasks:
    - name: create partition
      community.general.parted:
        device: /dev/sdb
        number: 1
        flags: [ lvm ]
        state: present

マニフェストを実行する。

ansible-playbook -i inventory 15.yaml

2.Linuxマシン上のユーザ管理

使用するモジュール:ansible.builtin.user

2-1 ユーザ作成

マニフェストファイルを以下のように作成した。
注意事項:Linuxユーザのパスワードは暗号化されるハッシュ値にする必要がある。

---
- name: create user account
  hosts: all
  become: true
  tasks:
     - name: create user example
       ansible.builtin.user:
         name: example
         password: "{{ 'password' | password_hash('sha512', 'mysecretsalt') }}"
         groups:
           - wheel
         state: "present"
         shell: "/bin/bash"
         system: false
         create_home: yes
         home: "/home/example"
         comment: "Ansible example"
         generate_ssh_key: true
2-2 グループ作成
---
- name: create group in linux
  hosts: all
  become: true
  tasks:
    - name: create group in linux
      ansible.builtin.group:
        name: 'example'
        state: present
2-3 ユーザのPrimaryグループを変更
---
- name: user primary group change
  hosts: all
  become: true
  vars:
    myuser: "example"
    mygroup: "example"
  tasks:
    - name: change primary group
      ansible.builtin.user:
        name: "{{ myuser }}"
        group: "{{ mygroup }}"
2-4 ユーザのSecondグループを変更

appendというパラメータですが、groups パラメーターと併用する
yesの場合、
ユーザーアカウントを groups パラメーターに指定したグループに追加する
no ← defaultの場合、
ユーザーアカウントを groups パラメーターに指定したグループだけに所属
させ、他のグループから削除する

---
- name: user second group change
  hosts: all
  become: true
  vars:
    myuser: "example"
    mygroups:
      - adm
      - sys
  tasks:
    - name: change second groups
      ansible.builtin.user:
        name: "{{ myuser }}"
        groups: "{{ mygroups }}"
        append: true
1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?