【问题标题】:Create vars from host inventory从主机清单创建变量
【发布时间】:2019-12-23 04:12:08
【问题描述】:

我有以下基础架构(3 个运行 Windows 2016 服务器版的服务器),其中主服务器在端口 80(示例)上运行 IIS 服务,并且需要 2 个代理连接到它们。为了允许通信,我需要添加 Windows 防火墙规则以将 IP 地址列入白名单

  • 一个主服务器(mas)
  • 和两个代理服务器 (agt)

我需要通过 ansible 执行的任务是,我只需要在主服务器上添加以下防火墙规则,并且不应该在代理主机上运行。如何仅在主服务器上运行以下任务,以便在配置防火墙规则时使用代理(agt)机器的 IP 地址详细信息。

- hosts: mas, agt
  tasks:
    - name: Firewall Rule Modifications
      win_firewall_rule:
        name: "IIS port"
        localport: "80"
        direction: in
        action: allow
        remoteip: "{{ansible_ip_addresses[0]}}"
        protocol: "tcp"
        description: "Allow agents"
        enabled: yes
        state: present

【问题讨论】:

  • 请正确缩进代码示例。谢谢!

标签: ansible windows-firewall


【解决方案1】:

如下所述,我能够创建一个解决方案(使用 centos 7 进行 vagrant 测试设置),但我认为应该有一种更简单的方法来实现这一点 :-)

库存文件:

[master]
mas

[agents]
agt1
agt2

剧本:

- name: Configure Iptables
  hosts: all
  serial: 1
  tasks:
    - name: create a file to store inventory IP's
      file:
        dest: /tmp/foo
        state: touch
        force: yes
      delegate_to: localhost

    - name: Register IP address
      shell: echo "{{ ansible_enp0s8.ipv4.address }}"
      register: op
      delegate_to: localhost

    - name: write IP's to a temp file
      lineinfile:
        dest: /tmp/foo
        line: "{{ op.stdout_lines[0] }}"
        insertafter: EOF
      delegate_to: localhost

    - name: Add firewall rules
      iptables:
        chain: INPUT
        source: "{{item}}"
        protocol: tcp
        destination_port: 80
        jump: ACCEPT
      with_lines: cat /tmp/foo
      when: ansible_hostname == 'mas'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 2019-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-04
    相关资源
    最近更新 更多