-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_using_loop.yml
More file actions
118 lines (116 loc) · 3.6 KB
/
03_using_loop.yml
File metadata and controls
118 lines (116 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
---
# 繰り返し処理を利用する
# ----------------------------------
#
# 繰り返しタスクは`with_**`を利用し、以下のように短く書くことができます。
#
# ```
# - name: sample loop tasks
# debug:
# msg: "{{ item }}"
# with_items:
# - one
# - two
# - three
# ```
#
# 利用できるものは、以下のようなものがあります
#
# ### with_items
#
# 一つのモジュールを何度も繰り返したいときに使用します。
#
# http://docs.ansible.com/ansible/playbooks_loops.html#standard-loops
#
# ### with_fileglob
#
# 指定されたファイル名のパターンマッチングを行い、
# パターンに一致するファイルに対して、繰り返し処理を行います。
# 複数のcopyモジュールやtemplateモジュールを利用し、リモートホストに配置する場合などに利用します。
#
# http://docs.ansible.com/ansible/playbooks_loops.html#id4
#
# ※管理ホスト(Ansibleを実行しているマシン)側のファイルに対して検索を行います。
# リモートホスト側のファイルではないことに注意してください
#
# ### with_subelements
#
# 個人的にはコードが読みにくくなるんで使わねぇっす
# 多分、以下の記事を読むのが手っ取り早いかも
#
# * [サブ要素をループするAnsibleのwith_subelements](http://dev.classmethod.jp/server-side/ansible/looping_over_subelements/)
#
# Ansibleの繰り返し処理に関する詳細は、以下の公式ドキュメントを参照してください
# http://docs.ansible.com/ansible/playbooks_loops.html
#
# Playbookのサンプル
# ---------------------
- hosts: all
vars:
users:
- name: wate
password: hogehoge
- name: sperkbird
password: fugafuga
- name: 223n
password: foofoo
- name: nogajun
password: barbar
base_packages:
- firewalld
- fail2ban
- fail2ban-firewalld
- fail2ban-server
- vim
- bash-completion
- expect
- git
- etckeeper
- tig
active_services:
- firewalld
- fail2ban
tasks:
- name: Ensure user exists
user:
name: "{{ item.name }}"
password: "{{ item.password|password_hash('sha512') }}"
state: present
with_items: "{{ users }}"
- name: Ensure user accepts the SSH key
authorized_key:
user: "{{ item.name }}"
key: https://github.com/{{ item.name }}.keys
state: present
with_items: "{{ users }}"
- name: Ensure admin users is sudoer with no password required
lineinfile:
create: true
dest: /etc/sudoers.d/using_loop
regexp: '^{{ item.name }} ALL\='
line: '{{ item.name }} ALL=(ALL) NOPASSWD:ALL'
state: present
validate: 'visudo -cf %s'
with_items: "{{ users }}"
- name: Ensure EPEL is installed
yum:
name: epel-release
state: present
- name: Ensure base package is installed
yum:
name: "{{ item }}"
enablerepo: epel
state: latest
with_items: "{{ base_packages }}"
- name: Ensure service is enabled and running
service:
name: "{{ item }}"
state: started
enabled: true
with_items: "{{ active_services }}"
# 補足資料
# -----------------
# パスワード情報を別ファイルで管理する例に関しては、以下の記事を参考にしてください
#
# AnsibleでCSVファイルから参照したパスワードでプロビジョニングする
# http://dev.classmethod.jp/server-side/ansible/using_password_by_csv_file/