Ansibleメモ
Ansibleでのファイル更新テンプレート
ディレクトリ構造
[root@server01 13:52:17 ansible]# tree --charset=C -I _old
.
|-- 00_OSsetting.yaml
|-- group_vars
| |-- lvs01
| | `-- hoge.yaml
| `-- server01
| `-- hoge.yaml
|-- inventory.ini
`-- roles
|-- 00_hosts
| |-- tasks -> ../xx_template/tasks/
| |-- template -> ../xx_template/template
| `-- vars
| `-- main.yaml
|-- 01_selinux
| |-- tasks -> ../xx_template/tasks/
| |-- template -> ../xx_template/template
| `-- vars
| `-- main.yaml
`-- xx_template
|-- tasks
| |-- create_dir.yaml
| |-- main.yaml
| |-- update_file.yaml
| `-- update_file_choice.yaml
|-- template
| |-- file.j2
| |-- file_choise.j2
| `-- vars.j2
`-- vars
`-- main.yaml
16 directories, 14 files
[root@server01 13:52:34 ansible]#
ファイル構造
00_OSsetting.yaml
---
- name: file change
hosts: all
become: no
vars:
yyyymmdd: "{{ lookup('pipe','date +%Y%m%d%H%M') }}"
roles:
- 00_hosts
- 01_selinux
group_vars/server01/hoge.yaml
RHEL:
hosts:
- "127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4:::〇"
- "::1 localhost localhost.localdomain localhost6 localhost6.localdomain6:::〇"
- ":::〇"
- "192.168.56.10 proxy01 proxy01.rhel.local:::〇"
- "192.168.56.11 proxy02 proxy02.rhel.local:::〇"
- "192.168.56.20 server01 server01.rhel.local:::〇"
- "192.168.56.40 lvs01:::〇"
- ":::〇"
- "172.16.0.10 proxy01b proxy01b.rhel.local:::〇"
- "172.16.0.20 server01b server01b.rhel.local:::〇"
selinux:
- ""
- "# This file controls the state of SELinux on the system."
- "# SELINUX= can take one of these three values:"
- "# enforcing - SELinux security policy is enforced."
- "# permissive - SELinux prints warnings instead of enforcing."
- "# disabled - No SELinux policy is loaded."
- "SELINUX=disabled"
- "# SELINUXTYPE= can take one of three two values:"
- "# targeted - Targeted processes are protected,"
- "# minimum - Modification of targeted policy. Only selected processes are protected. "
- "# mls - Multi Level Security protection."
- "SELINUXTYPE=targeted "
- ""
- ""
roles/00_hosts/vars/main.yaml
---
#-----------------------------------------------------------------------------#
# ユーザ変更可能定義
#-----------------------------------------------------------------------------#
### 出力先ファイル情報
output_dir: "/var/tmp" # Directory
output_file: "hosts" # FileName
output_file_owner: "root" # Owner
output_file_group: "root" # Group
output_file_mode: "0644" # Permision
### 出力先ファイル情報(Aパターン)
# default:全サーバ同じファイルで更新パターン
#update_case: "default"
#j2_vars_list: "{{ RHEL.hosts }}" # groupvarsで定義した変数
### 出力先ファイル情報(Bパターン)
# choicet:〇が付与されているものを選択して更新パターン
update_case: "choice" # default or choice
j2_vars_list: "{{ RHEL.hosts }}" # groupvarsで定義した変数
j2_vars: "hosts" # templateで使用する変数
j2_vars_choise_list: "{{ updateos[j2_vars] }}" # ユーザ変更不可定義
#-----------------------------------------------------------------------------#
# ユーザ変更不可定義
#-----------------------------------------------------------------------------#
### コントローラノードのみ作成される作業用一時ディレクトリ
local_work_dir: "/var/tmp/ansible/local_work/{{ inventory_hostname }}"
backup_dir: "/var/tmp/ansible/backup"
roles/01_selinux/vars/main.yaml
---
#-----------------------------------------------------------------------------#
# ユーザ変更可能定義
#-----------------------------------------------------------------------------#
### 出力先ファイル情報
output_dir: "/var/tmp" # Directory
output_file: "config" # FileName
output_file_owner: "root" # Owner
output_file_group: "root" # Group
output_file_mode: "0644" # Permision
### 出力先ファイル情報(Aパターン)
# default:全サーバ同じファイルで更新パターン
update_case: "default"
j2_vars_list: "{{ RHEL.selinux }}" # groupvarsで定義した変数
### 出力先ファイル情報(Bパターン)
# choicet:〇が付与されているものを選択して更新パターン
#update_case: "choice" # default or choice
#j2_vars_list: "{{ RHEL.hosts }}" # groupvarsで定義した変数
#j2_vars: "hosts" # templateで使用する変数
#j2_vars_choise_list: "{{ updateos[j2_vars] }}" # ユーザ変更不可定義
#-----------------------------------------------------------------------------#
# ユーザ変更不可定義
#-----------------------------------------------------------------------------#
### コントローラノードのみ作成される作業用一時ディレクトリ
local_work_dir: "/var/tmp/ansible/local_work/{{ inventory_hostname }}"
backup_dir: "/var/tmp/ansible/backup"
roles/xx_template/template/file.j2
{% for var in j2_vars_list %}
{{ var }}
{% endfor %}
roles/xx_template/template/file_choise.j2
{% for var in j2_vars_choise_list %}
{{ var }}
{% endfor %}
roles/00_file_change/template/vars.j2
updateos:
{{ j2_vars }}:
{% for var in j2_vars_list %}
{% if var.split(':::')[1] == '〇' %}
- {{ var.split(':::')[0] }}
{% endif %}
{% endfor %}
roles/xx_template/tasks/main.yaml
---
#-----------------------------------------------------------------------------#
# ディレクトリ作成
#-----------------------------------------------------------------------------#
- import_tasks: create_dir.yaml
#-----------------------------------------------------------------------------#
# ファイル更新 #
#-----------------------------------------------------------------------------#
- import_tasks: update_file.yaml
when:
- update_case is defined
- "'default' in update_case"
- import_tasks: update_file_choice.yaml
when:
- update_case is defined
- "'choice' in update_case"
roles/xx_template/tasks/main.yaml
---
#------------------------------------------------------------------------------#
# 作業用一時ディレクトリ作成
#------------------------------------------------------------------------------#
- name: Create Temp Direcotry
file:
path: "{{ local_work_dir }}"
state: directory
mode: 0755
delegate_to: localhost
changed_when: no
#------------------------------------------------------------------------------#
# バックアップディレクトリ作成
#------------------------------------------------------------------------------#
- name: Create Temp Direcotry
file:
path: "{{ backup_dir }}/{{ output_dir }}"
state: directory
mode: 0755
changed_when: no
roles/xx_template/tasks/update_file.yaml
---
#------------------------------------------------------------------------------#
# ファイルバックアップ(latest)
#------------------------------------------------------------------------------#
- name: file backup latest
copy:
src: "{{ output_dir }}/{{ output_file }}"
dest: "{{ backup_dir }}/{{ output_dir }}/{{ output_file }}"
remote_src: yes
failed_when: no
changed_when: no
#------------------------------------------------------------------------------#
# ファイルバックアップ(yyyymmdd)
#------------------------------------------------------------------------------#
- name: file backup yyyymmdd
copy:
src: "{{ output_dir }}/{{ output_file }}"
dest: "{{ backup_dir }}/{{ output_dir }}/{{ output_file }}_{{ yyyymmdd }}"
remote_src: yes
failed_when: no
changed_when: no
#------------------------------------------------------------------------------#
# ファイル出力
#------------------------------------------------------------------------------#
- name: output file
template:
src: template/file.j2
dest: "{{ output_dir }}/{{ output_file }}"
owner: "{{ output_file_owner }}"
group: "{{ output_file_group }}"
mode: "{{ output_file_mode }}"
roles/xx_template/tasks/update_file_choice.yaml
---
#------------------------------------------------------------------------------#
# 値の「〇」がついている値のみ、抜き出して作成。セパレータは「:::」
#------------------------------------------------------------------------------#
- name: generate myhost vars file
template:
src: template/vars.j2
dest: "{{ local_work_dir }}/vars.j2"
delegate_to: localhost
changed_when: no
#------------------------------------------------------------------------------#
# yaml再読み込みして変数上書き
#------------------------------------------------------------------------------#
- name: include myhost vars file
include_vars: "{{ local_work_dir }}/vars.j2"
delegate_to: localhost
#------------------------------------------------------------------------------#
# ファイルバックアップ(latest)
#------------------------------------------------------------------------------#
- name: file backup latest
copy:
src: "{{ output_dir }}/{{ output_file }}"
dest: "{{ backup_dir }}/{{ output_dir }}/{{ output_file }}"
remote_src: yes
failed_when: no
changed_when: no
#------------------------------------------------------------------------------#
# ファイルバックアップ(yyyymmdd)
#------------------------------------------------------------------------------#
- name: file backup yyyymmdd
copy:
src: "{{ output_dir }}/{{ output_file }}"
dest: "{{ backup_dir }}/{{ output_dir }}/{{ output_file }}_{{ yyyymmdd }}"
remote_src: yes
failed_when: no
changed_when: no
#------------------------------------------------------------------------------#
# ファイル出力
#------------------------------------------------------------------------------#
- name: output file
template:
src: template/file_choise.j2
dest: "{{ output_dir }}/{{ output_file }}"
owner: "{{ output_file_owner }}"
group: "{{ output_file_group }}"
mode: "{{ output_file_mode }}"
Author And Source
この問題について(Ansibleメモ), 我々は、より多くの情報をここで見つけました https://qiita.com/hakoritenshi/items/6627a191c200ebeb0d43著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .