0
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 の使い方

Last updated at Posted at 2025-04-14

Ubuntu 24.10 で確認した方法です。

インストール

sudo apt install ansible

確認

$ ansible --version
ansible [core 2.16.3]
$ ansible-community --version
Ansible community version 9.2.0

MariaDB のインストール

host.ini
# hosts.ini
[local]
localhost ansible_connection=local
install_mariadb.yml
# install_mariadb.yml
---
- name: Install MariaDB on Ubuntu
  hosts: localhost
  become: true

  tasks:
    - name: Update apt cache
      apt:
        update_cache: yes

    - name: Install MariaDB server
      apt:
        name: mariadb-server
        state: present

    - name: Install pymysql (required for mysql_user module)
      apt:
        name: python3-pymysql
        state: present

    - name: Ensure MariaDB is started and enabled
      service:
        name: mariadb
        state: started
        enabled: yes

    - name: Set root password (optional)
      mysql_user:
        name: root
        host_all: true
        password: tiger123
        login_unix_socket: /var/run/mysqld/mysqld.sock

実行

ansible-playbook -i hosts.ini install_mariadb.yml

インストールされたことの確認

sudo systemctl status mariadb

MariaDB の動作確認

$ mysql -uroot -ptiger123
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.11.11-MariaDB-0ubuntu0.24.04.2 Ubuntu 24.04

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> exit
Bye
$

MariaDB の削除

host.ini
# hosts.ini
[local]
localhost ansible_connection=local
remove_mariadb.yml
- name: Remove MariaDB from Ubuntu
  hosts: localhost
  become: true

  tasks:
    - name: Purge multiple packages
      apt:
        name:
          - mariadb-server
          - mariadb-client
          - python3-pymysql
        state: absent
        purge: yes

    - name: Remove MariaDB data directory
      file:
        path: /var/lib/mysql
        state: absent

実行

ansible-playbook -i hosts.ini remove_mariadb.yml

MariaDB の停止

stop_mariadb.yml
- name: Stop MariaDB on Ubuntu
  hosts: localhost
  become: true

  tasks:
    - name: stop mariadb
      service:
        name: mariadb
        state: stopped

実行

ansible-playbook -i hosts.ini stop_mariadb.yml

確認

sudo systemctl status mariadb

MariaDB の起動

start_mariadb.yml
- name: Start MariaDB on Ubuntu
  hosts: localhost
  become: true

  tasks:
    - name: Start mariadb, if not started
      service:
        name: mariadb
        state: started

実行

ansible-playbook -i hosts.ini start_mariadb.yml

確認

sudo systemctl status mariadb
0
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
0
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?