ansible task flow control

A task entrusted

  By default, all tasks ansible are running on the specified machine, when arranged in a separate cluster environment, but just one operation wherein the hosts, or to run on a particular host, then We need to use ansible delegate the task function.

  Use the keyword delegate_to configuration tasks can be performed on the specified machine, two other tasks or running on a machine configured hosts keywords, to the task when the keyword is located, on the use of delegated machine running. E.g:

---
  - hosts: zookeeper
    remote_user: root
    gather_facts: no

    tasks:
      - name: test
        shell: "echo 123 > /tmp/bb"
      - name: test delegate
        shell: "echo 1 > /tmp/aa"
        register: aa
        delegate_to: 192.168.4.50
      - name: hhh
        debug:
          var: "{{ aa }}"

说明:zookeeper组里面是两天主机:
192.168.4.50
192.168.4.46

If you want to run ansible local server, in addition to the tasks entrusted to 127.0.0.1 or localhost, you can also use the keyword local_action completed, as follows:     Tasks:

- name: test
        shell: "echo 123 > /tmp/bb"
      - name: test delegate
        shell: "echo 1 > /tmp/aa"
        register: aa
        delegate_to: 127.0.0.1   
或者
    tasks:
      - name: test
        shell: "echo 123 > /tmp/bb"
      - name: test delegate
        local_action: shell "echo 1 > /tmp/aa"
        register: aa
        

Second, the task pause

  When run some tasks need to wait until recover some states, such as a host or just restart the application, you need to wait for it to open a port, this time on the tasks that need to be suspended. as follows:

---
  - hosts: hadoop
    remote_user: root
    gather_facts: no

    tasks:
    - name: test1
      local_action:
        module: wait_for           #模块名字
        port: 80
        host: 192.168.4.50
        delay: 10
        timeout: 300
        state: started

Use local_action with wait_for module to complete the task pause operation. The task every 80 checks the host port on the 10s is turned on, if the operation 300S, any port 80 is not on, returns failure information.

Guess you like

Origin www.cnblogs.com/yjt1993/p/10971801.html