AWS
Ansible
RHEL7
centos7
0

CentOS 7(REHL 7) 初期設定 Ansible 編

はじめに

CentOS 7(REHL 7) 初期設定 の 内容を Ansible の Playbook に 置き換えてみます。

ロケール、タイムゾーンの設定

ロケールの設定
- name: set locale
  shell: localectl set-locale LANG=ja_JP.utf8
  when: ansible_env.LANG | default('') != 'ja_JP.utf8'
タイムゾーンの設定
- name: set timezone to Asia/Tokyo
  timezone:
    name: Asia/Tokyo

Proxyの設定

サーバのProxy設定
# proxy.sh を templetes ディレクトリに配置
- name: set proxy.sh
  template:
    force: yes
    src: proxy.sh
    dest: /etc/profile.d/proxy.sh
    owner: root
    group: root
    mode: 0644
  register: proxy_file
- name: source proxy.sh
  shell: source /etc/profile.d/proxy.sh
  when:
  - proxy_file is changed
yumのProxy設定
# /etc/yum.conf に Proxy設定を追加
- name: set yum proxy
  lineinfile:
    dest: /etc/yum.conf
    state: present
    regexp: "^proxy=.*$"
    insertafter: EOF
    line: "proxy={{ http_proxy }}"
パッケージの最新化
- name: yum update
  yum:
    name: '*'
    state: latest
    skip_broken: yes
rpmのproxy設定
# macros を templates ディレクトリに配置
- name: set rpm proxy
  template:
    force: yes
    src: macros
    dest: /etc/rpm/macros
    owner: root
    group: root
    mode: 0644
wgetインストール
- name: install wget
  yum:
    name: wget
    state: latest
    skip_broken: yes
wgetのproxy設定
# /etc/wgetrc に Proxy設定を追加
- name: set wget proxy
  lineinfile:
    dest: /etc/wgetrc
    state: present
    regexp: "^{{ item }} = .*$"
    insertafter: "^#{{ item }}"
    line: "{{ item }} = {{ http_proxy }}"
  with_items:
    - http_proxy
    - https_proxy
    - ftp_proxy
gitのインストール
- name: install the latest version of git
  yum:
    name: git
    state: latest
gitのproxy設定
- name: set git proxy
  shell: "git config --global {{ item }} {{ http_proxy }}"
  ignore_errors: yes
  with_items:
    - http.proxy
    - https.proxy

社内CA証明書の設定

# CA証明書のハッシュ値を取得(証明書が変わった時にエラーにする為)
sha256sum xxx.ca
ca_sha256(ハッシュ値)
社内CA証明書の設定
- name: set Local CA Certificate
  get_url:
    url: "{{ ca_url }}"
    dest: /etc/pki/ca-trust/source/anchors
    checksum: "sha256:{{ ca_sha256 }}"
    use_proxy: no
    force: yes
  register: downloaded_ca

- name: extract ca
  shell: update-ca-trust extract
  when:
  - downloaded_ca is changed

さいごに

具体的なソースは GitHub を ご参照ください。

次は、今回作成したロールを Ansible Galaxy に登録して再利用する方法 を 紹介したいと思います。