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 (core 2.14.18)】初期構築

Last updated at Posted at 2025-10-02

環境

  • コントロールノード : Miracle Linux 9.6
  • ターゲットノード : Miracle Linux 9.6
    ※ コントロールノードからターゲットノードへAnsibleを流す
  • ansible core 2.14.18
  • python 3.9.21

コントロールノード側の設定

  1. パッケージ入手
    • # dnf upgrade
    • # dnf install ansible-core
    • $ ansible --version
  2. ssh接続設定 (鍵を生成し公開鍵の方を送る。ここでは名前はid_rsaとした)
    • $ ssh-keygen -t rsa
    • $ ssh-copy-id -i $HOME/.ssh/id_rsa.pub <ターゲットノードのIP>
  3. ssh接続テスト
    • $ ssh <ターゲットノードのユーザ名>@<ターゲットノードのIP>
      接続出来たら $ exit で閉じる
  4. ansible動作確認
    • $ vi inventory.ini
      ターゲットノードのIPアドレスを記載する
    • $ ansible -i inventory.ini <ターゲットノードのIP> -m ansible.builtin.ping
      SUCCESSと表示されることを確認する

ディレクトリ構成について

  • ansible.builtin.ping を題材に ansible-playbook 用の簡単なディレクトリ構成を記載
ディレクトリ構成
ansibleWork
├── group_vars
│   ├── all.yml           /* 全ホスト用変数用を記載 */
│   └── test_targets.yml      /* test_targetsグループ用グループ変数を記載 */
├── host_vars
│   └── <ターゲットノードのIP>.yml  /* ターゲットノード用のホスト変数を記載 */
├── inventory.ini
└── status_check.yml
inventory.ini
# inventory.ini

[test_targets]
<ターゲットノードのIP>
status_check.yml
# status_check.yml
---
- hosts: test_targets
  tasks:
    - name: status check ansible ping
      ansible.builtin.ping:
  • 実行 : ansible-playbook -i inventory.ini status_check.yml

roles と ディレクトリ構成について

  • ターゲットノードにディレクトリを作成しそこにconfファイルを置きたいとする。そこで以下のようなディレクトリ構成とファイルの内容を考える
ディレクトリ構成

ansibleWork2
├── distribute_test.yml
├── group_vars
│   ├── all.yml
│   └── test_targets.yml
├── host_vars
│   └── <ターゲットノードのIP>.yml
├── inventory.ini
└── templates
    └── hoge.conf.j2        /* 何かしらのconfファイルの想定 */

distribute_test.yml
# distribute_test.yml
---

- hosts: test_targets

  vars:

  tasks:
    - name: create directory
      ansible.builtin.file:
        path: /home/ada2522/distribute_test_directory1
        state: directory
        owner: ada2522
        group: ada2522
        mode: '0755'
        
    - name: distribute conf file
      ansible.builtin.template:
        src: templates/hoge.conf.j2
        dest: /home/ada2522/distribute_test_directory1/hoge.conf
        owner: ada2522
        group: ada2522
        mode: '0644'
      notify: execute ls

  handlers:
    - name: ls -la
      ansible.builtin.command:
        cmd: ls -la /home/ada2522/distribute_test_directory1
      register: ls_result
      listen: execute ls

    - name: show ls_result
      ansible.builtin.debug:
        var: ls_result
      listen: execute ls

    - name: cat test.conf
      ansible.builtin.command:
        cmd: cat /home/ada2522/distribute_test_directory1/hoge.conf
      listen: execute ls
  • この調子だと1つのファイルが長くなってしまうので roles を使って distribute_test.yml を分割してみる
ディレクトリ構成
ansibleWork3
├── distribute_test.yml
├── group_vars
│   ├── all.yml
│   └── test_targets.yml
├── host_vars
│   └── <ターゲットノードのIP>.yml
├── inventory.ini
└── roles
    └── distribute_file
        ├── defaults
        │   └── main.yml    /* 変数の初期値を記載 */
        ├── files
        │   └── Readme.md    /* copyモジュールなど用 */
        ├── handlers
        │   └── main.yml    /* handlerの処理用 */
        ├── meta
        │   └── main.yml    /* 依存関係やansible galaxy用 */
        ├── tasks
        │   └── main.yml    /* taskの処理用 */
        ├── templates
        │   └── hoge.conf.j2  /* templateモジュール用 */
        └── vars
            └── main.yml      /* 変数定義用 */
distribute_test.yml
# distribute_test.yml
---

- hosts: test_targets

  name: create directory and distribute file

  roles:
    - distribute_file

roles/distribute_file/handlers/main.yml
# roles/distribute_file/handlers/main.yml
---

- name: ls -la and show ls_result

  block:
    - name: ls -la
      ansible.builtin.command:
        cmd: ls -la /home/ada2522/distribute_test_directory1
      register: ls_result
      listen: execute ls

    - name: show ls_result
      ansible.builtin.debug:
        var: ls_result
      listen: execute ls

- name: cat test.conf
  ansible.builtin.command:
    cmd: cat /home/ada2522/distribute_test_directory1/hoge.conf
  listen: execute ls

roles/distribute_file/tasks/main.yml
# roles/distribute_file/tasks/main.yml
---

- name: create directory
  ansible.builtin.file:
    path: /home/ada2522/distribute_test_directory1
    state: directory
    owner: ada2522
    group: ada2522
    mode: '0755'

- name: distribute conf file
  ansible.builtin.template:
    src: templates/hoge.conf.j2
    dest: /home/ada2522/distribute_test_directory1/hoge.conf
    owner: ada2522
    group: ada2522
    mode: '0755'
  notify: execute ls

  • 実行

  • ansible-playbook -i inventory.ini distribute_test.yaml -CD

  • ansible-playbook -i inventory.ini distribute_test.yaml -D

become について

  • ここではターゲットノード側でsudoersに記載せずにrootになる方法を記載する
ディレクトリ構成
ansibleWork4
├── group_vars
│   ├── all.yml
│   └── test_targets.yml
├── host_vars
│   └── 192.168.10.108.yml
├── inventory.ini
└── package_upgrade.yml
package_upgrade.yml
# package_upgrade.yml
---

- hosts: test_targets
 
  become: yes
  become_method: su
  become_user: root

  tasks:
    - name: dnf upgrade
      ansible.builtin.dnf:
        name: '*'
        state: latest
  • 実行

  • ansible-playbook -i inventory.ini package_upgrade.yml --ask-become-pass -CD

  • ansible-playbook -i inventory.ini package_upgrade.yml --ask-become-pass -D

おわりに

  • 追記予定
    • ansible vault
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?