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 5 years have passed since last update.

CentOS 7 で、ansible を使って、firewalld を設定する

Posted at

ufw では簡単にできた、ssh のIPアドレスによるアクセス制限が意外と大変だったので、設定の例あげておきます。この程度でも役に立つかもしれないと思っての投稿です。
もっといい例があったらお教えください。

---
- name: firewalld の設定テスト甩
# vagrant CentOS7 甩ノスクリプトです。
  hosts: v-c7
  vars:
    ips:
      - 192.168.33.1     # vagrant 甩のIPアドレス 
      - 10.0.2.2         #  お使いのVagrant の設定に合わせてください
    mysql_ips:
      - 192.168.33.1     # mysql 甩のポートをあけます。
      - 192.168.0.0/24   # vagrant 甩のIPアドレスです。
  tasks:

  - name: firewalld のインストール 
    become: yes
    yum:
      name: firewalld
      state: latest
  # default は public で最初は、ssh,dhcp が開いています。
  # http,https を開けるように指定します。
  - name: http,https
    become: yes
    firewalld:
      zone: public
      service: "{{ item }}"
      permanent: yes
      state: enabled
    with_items:
      - http
      - https
  # ssh のアクセス元のIP制限かけます。IPは、変数で指定
  - name: ssh のアクセス元の許可
    become: yes
    firewalld:
      rich_rule: 'rule family=ipv4 source address="{{ item }}" service name="ssh" accept'
      permanent: yes
      state: enabled
    with_items: "{{ ips }}"
  # ssh の port 番号の変更用の指定です。12012 にしました。適当に、合わせて変更してください。
  - name: 12012 を設定
    become: yes
    firewalld:
      rich_rule: 'rule family=ipv4 source address="{{ item }}" port port="12012" protocol="tcp" accept'
      permanent: yes
      state: enabled
    with_items: "{{ ips }}"
  # mysql 用のポートをあけます。
  - name: port mysql
    become: yes
    firewalld:
      rich_rule: 'rule family=ipv4 source address="{{ item }}" port port="3306" protocol="tcp" accept'
      permanent: yes
      state: enabled
    with_items: "{{ mysql_ips }}"
  - name: ssh を落とす
    become: yes
    firewalld:
      service: ssh
      permanent: yes
      state: disabled

  - name: dhcpv6-client を落とす
    become: yes
    firewalld:
      service: dhcpv6-client
      permanent: yes
      state: disabled

  - name: restart firewalld これで、設定が反映されます。
    become: yes
    systemd:
      state: restarted
      name: firewalld

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?