2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

AmazonLinux2設定用Ansibleサンプル

Last updated at Posted at 2022-12-16

内容

Amazon Linux 2の初期設定サンプルのPlayBookです。Ansibleの学習用に。

ディレクトリ構成

.
|-- inventory
|   |-- group_vars
|   |   `-- all.yml
|   `-- hosts
|-- roles
|   `-- setup
|       |-- files
|       |-- tasks
|       |   `-- main.yml
|       `-- templates
|           `-- snmpd.conf.j2
`-- site.yml

ファイル説明

フォルダ名 説明
site.yml このファイルを最初に呼び出す。ここからrole配下が実行
inventory/group_vars/all.yml 変数を格納
inventory/hosts 実行対象を記載
roles/setup/tasks/main.yml メインの処理を記述
roles/setup/templates/snmpd.conf.j2 snmpの設定、メインの処理の中でコピーされる

実行している内容

  • yum update
  • モジュールのインストール
  • モジュールのenable/didable
  • TimeZone関連設定
  • ホスト名設定
  • IPv6無効化
  • SELInux無効化
  • ユーザー作成
  • snmpd.confコピー(前にsnmpdを使用していたため)
roles/setup/tasks/main.yml
---

- name: yum update
  yum:
    name: "{{ item }}"
    state: latest
    update_cache: yes
  loop:
    - "*"

- name: install module
  yum: 
    name: "{{item}}"
    state: present
  loop: "{{ install_packages_list | default([], true) }}"

- name: systemd enable
  systemd:
    name: "{{item}}"
    enabled: yes
  loop: "{{enabled_packages_list | default([], true) }}"

- name: systemd disable
  systemd:
    name: "{{item}}"
    enabled: no
  loop: "{{disabled_packages_list | default([], true) }}"

- name: set timezone
  timezone:
    name: "{{ local_timezone }}"
  
- name: set locale
  command: "localectl set-locale LANG={{ local_locale }}"

- name: set keymap
  command: "localectl set-keymap {{ locale_keymap }}"

- name: set hostname
  hostname:
    name: "{{ linux_hostname }}"

- name: IPv6 disabled
  sysctl:
    state: present
    name: "{{ item }}"
    value: "1"
    sysctl_set: no
    reload: no
  with_items:
    - net.ipv6.conf.all.disable_ipv6
    - net.ipv6.conf.default.disable_ipv6
    - net.ipv6.conf.lo.disable_ipv6

- name: Disable SELinux
  selinux:
    state: disabled

- name: create operation users
  user: 
    name: "{{ item.key }}"
    password: "{{ item.value.password }}"
    groups: "{{ item.value.groups }}"
    shell: /bin/bash
    home: "/home/{{item.key}}"
    state: present
  loop: "{{ user_newusers|dict2items }}"

- name: snmpd.conf file copy
  template:
    src: "snmpd.conf.j2"
    dest: "/etc/snmp/snmpd.conf"
    owner: root
    group: root
    mode: 0600

その他ファイルはGitHub参照

実行方法

  • Amazon Linux作成
  • ansible準備
sudo su -
amazon-linux-extras enable ansible2
yum install -y ansible
  • 鍵の配置
vi /home/ec2-user/.ssh/key.pem # 使用しているKeyペアの内容を貼り付ける
chmod 400 /home/ec2-user/.ssh/key.pem
  • Playbook実行
yum install -y git
git clone https://github.com/Toru-Kubota/Ansible-AmazonLinux2-Setup.git
cd Ansible-AmazonLinux2-Setup
ansible-playbook -i inventory/hosts site.yml
2
3
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
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?