Ansible 使い始めたら便利だったのでメモ。
まずは Ansible をインストール。
sudo apt update -y sudo apt install software-properties-common sudo add-apt-repository --yes --update ppa:ansible/ansible sudo apt install ansible
non-root ユーザーでの作業を許可するために ansible.cfg に下記を追加
# /etc/ansible/ansible.cfg [default] allow_world_readable_tmpfiles = True pipelining = True
あとは Playbook を用意する。
例えば Ansible で PostgreSQL のセットアップを自動化するならこう書く。
---
- name: Install PostgreSQL and configure
hosts: localhost
become: yes
tasks:
- name: Update APT cache
apt:
update_cache: yes
- name: Install PostgreSQL and related tools
apt:
name:
- postgresql
- postgresql-contrib
state: present
- name: Set PostgreSQL user password
become: yes
become_user: postgres
become_method: sudo
shell: |
psql -c "ALTER USER postgres WITH PASSWORD '123456';" # Change password here
exit
- name: Add PostgreSQL bin directory to PATH
lineinfile:
path: /home/orenomemo/.profile # Replace username here
line: 'export PATH=$PATH:/usr/lib/postgresql/12/bin'
owner: orenomemo # Change username here
state: present
create: yes
- name: Enable PostgreSQL password login
postgresql_pg_hba:
dest: /etc/postgresql/12/main/pg_hba.conf
contype: local
users: all
databases: all
method: md5
state: present
- name: Disable PostgreSQL peer login for postgres
postgresql_pg_hba:
dest: /etc/postgresql/12/main/pg_hba.conf
contype: local
users: postgres
databases: all
method: peer
state: absent
Playbook を用意したら実行。
ansible-playbook inst_psql.yml