当最重要的任务发生变化时,跟随任务执行

时间:2019-06-12 03:05:47

标签: ansible

当第一个任务(task1)成功在文件(blockinfile)中添加行时,想要执行第二个任务(task2)。

 - name: nodes ip server configuration
   hosts: "{{ hostname }}"
   become: true
   become_user: root
   tasks:
     - include_tasks: step1_iptables.yaml
     - include_tasks: step2_script_firewall.yaml

========更多step1_iptables.yaml =========

- name: ip firewall configuration
  blockinfile:
    path: /etc/init.d/test
    marker: "# {mark} Customer {{ admin_code }}-{{ ipadd }}-{{ ip }}-{{ rg1 }}-{{ rg2 }}"
    insertbefore: "## TO HERE"
    block: |
      $IPTABLES -A LOCALLY_MANAGED_RULES_INPUT -p udp -s {{ ipadd }} --sport 5060 -d $ip_SERVER_{{ ip }}_IP_ADDRESS --dport $ip_PORT -j ACCEPT
      $IPTABLES -A LOCALLY_MANAGED_RULES_OUTPUT -p udp -s $ip_SERVER_{{ ip }}_IP_ADDRESS --sport $ip_PORT -d {{ ipadd }} -j ACCEPT
    backup: yes

========更多step2_script_firewall.yaml =========

- name: Run script to save iptables
  command: sh /etc/init.d/firewall_node_local
  register: myoutput
- debug: var=myoutput.stdout_lines

================================================ ======================

task2仅在文件中添加task1中的行时运行。如果重复,则与脚本相关的task2会跳过。

   tasks:
      - include_tasks: step1_iptables.yaml
      - include_tasks: step2_script_firewall.yaml
        when: step1_iptables.yaml is changed

================================================ ======================

1 个答案:

答案 0 :(得分:0)

这就是handlers的作用。将 notify 添加到第一个任务

- name: ip firewall configuration
  blockinfile:
    path: /etc/init.d/test
    ...
    backup: yes
  notify: script_firewall

并使用处理程序创建处理程序部分

handlers:
- name: script_firewall
  command: sh /etc/init.d/firewall_node_local