AlmaLinux9.2 Ansibleのインストール手順

環境
# cat /etc/redhat-release
AlmaLinux release 9.2 (Turquoise Kodkod)

操作方法
1.ansibleをインストールする

# dnf install epel-release
# dnf install ansible

2.ansibleバージョンを確認する

#  ansible --version
ansible [core 2.14.2]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.11/site-packages/ansible
  ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
  executable location = /bin/ansible
  python version = 3.11.2 (main, May 24 2023, 00:00:00) [GCC 11.3.1 20221121 (Red Hat 11.3.1-4)] (/usr/bin/python3.11)
  jinja version = 3.1.2
  libyaml = True

3.ディレクトリファイル構成

etc/ansible/
├── hosts
├── roles
│   └── system
│       └── tasks
│           └── main.yml
└── site.yml

4.hostsの定義
# vi /etc/ansible/hosts
以下の内容を追記する

[almalinux9-server]
localhost ansible_connection=local

5.site.ymlに以下の内容を追記する

- hosts: almalinux9-server
  roles:
    - system

6.main.ymlの作成

# vi /etc/ansible/roles/system/tasks/main.yml

以下の内容を追記する

# パッケージアップデート
- name: dnf upgrade
  yum: name=* state=latest

# パッケージインストール
- name: dnf install
  yum:
    name: "{{packages}}"
  vars:
    packages:
      - langpacks-ja
      - vim

# ロケール事前確認
- name: check locale
  shell: localectl status
  register: check_localectl_result
  check_mode: no
  changed_when: false

# ロケール設定
- name: set locale
  shell: localectl set-locale LANG=ja_JP.utf8
  when: "'LANG=ja_JP.utf8' not in check_localectl_result.stdout"

# タイムゾーン設定
- name: set timezone to Asia/Tokyo
  timezone:
    name: Asia/Tokyo

# SELinux無効化
- name: disable SELinux
  selinux: state=disabled

# firewalld無効化
- name: disable firewalld
  systemd:
    name: firewalld
    state: stopped
    enabled: false

7.playbookを実行する

# cd /etc/ansible
# ansible-playbook -i hosts site.yml -D

結果

# ansible-playbook -i hosts site.yml -D
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details

PLAY [almalinux9-server] *********************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************
ok: [localhost]

PLAY RECAP ***********************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

8.サーバーを再起動する

# reboot

 

Ansible

Posted by arkgame