Ansible之等待服务状态变成成功

ansible的脚本中我们可能会要启动某项服务并等待,直到服务启动起来,然后再进行下一步

这个非常重要,我们举例来实验一下:

 1- hosts: localhost
 2  vars:
 3    local__service: ssh
 4  tasks:
 5    - block:
 6      - name: "Stop {{ local__service }} service"
 7        systemd:
 8          service: "{{ local__service }}"
 9          state: stopped
10
11      - name: "Populate ansible_facts.services variable"
12        ansible.builtin.service_facts:
13
14      - name: "{{ local__service }} state will be stopped as expected"
15        assert:
16          that:
17            ansible_facts.services[local__service].state == 'stopped'
18
19      - name: "Start {{ local__service }} service"
20        systemd:
21          service: "{{ local__service }}"
22          state: started
23
24      - name: "Registered {{ local__service }} state will still be stopped as it was not refreshed"
25        assert:
26          that:
27            ansible_facts.services[local__service].state == 'stopped'
28
29      - name: "Refresh ansible_facts.services variable"
30        ansible.builtin.service_facts:
31
32      - name: "{{ local__service }} state will be running as expected"
33        assert:
34          that:
35            ansible_facts.services[local__service].state == 'running'

上面的 playbook 一共有7步:

1、停止 ssh 服务

2、获取 ansible_fact 变量

3、断言 ansible_facts 中的 ssh 服务是 stopped 状态

4、重新启动 ssh 服务

5、断言 ansible_facts 中的 ssh 服务仍然是 stopped 状态

6、重新获取 ansible_fact 变量

7、这时 ansible_facts 中的 ssh 服务状态刷新了,变成了 running

就是服务启动后,必须去不断刷新 ansible_facts,才能获得刷新后的状态,所以我们利用这一点,脚本如下:

 1- hosts: localhost
 2  vars:
 3    local__service: ssh
 4  tasks:
 5    - block:
 6      - name: "Stop {{ local__service }} service"
 7        systemd:
 8          service: "{{ local__service }}"
 9          state: stopped
10
11      - name: "Wait until {{ local__service }} service is stopped"
12        ansible.builtin.service_facts:
13        register: temp__service_facts
14        until: temp__service_facts.ansible_facts.services[local__service].state == 'stopped'
15        retries: 20
16        delay: 2
17
18      - name: "Start {{ local__service }} service"
19        systemd:
20          service: "{{ local__service }}"
21          state: started
22
23      - name: "Wait until {{ local__service }} service is running"
24        ansible.builtin.service_facts:
25        register: temp__service_facts
26        until: temp__service_facts.ansible_facts.services[local__service].state == 'running'
27        retries: 20
28        delay: 2

上面wait until的步骤就是,注册一个临时的变量,然后不断去测试,重试20次,每次的延时是2秒。这样就可以解决了。


LVM系统的Restore
Awk的用法
comments powered by Disqus