ansible result.stdout.find('running') != -1 判断状态

[root@m01 ansible-playbook]# cat test_1103.yml
---
- hosts: webserver
  tasks:
    - file: path=/tmp/{{ item }} state=touch
      with_items:
        - a.txt
        - b.txt
- hosts: db01
  tasks:
    - shell: /etc/init.d/mysqld status
      register: result
#    - shell: echo "{{ result.stdout }}" >>/tmp/aa.txt
    - service: name=mysqld state=stopped
      when: result.stdout.find('running') != -1

 

[root@db01 ~]# /etc/init.d/mysqld status
 SUCCESS! MySQL running (1356)
[root@db01 ~]# /etc/init.d/mysqld status
 ERROR! MySQL is not running

转载于:https://www.cnblogs.com/gaoyuechen/p/7776376.html

TASK [apex : DEBUG - Confirm re_update.yaml is running] ************************************************************************************************************************************************* ok: [10.11.107.89] => { "msg": "re_update.yaml is definitely running!" } TASK [apex : Set type to 'jd' if dist_id is between 7000-7999] ****************************************************************************************************************************************** skipping: [10.11.107.89] => {"changed": false, "skip_reason": "Conditional result was False"} TASK [apex : Set tmp type to 'jd'] ********************************************************************************************************************************************************************** ok: [10.11.107.89] => {"ansible_facts": {"type": "jd"}, "changed": false} TASK [apex : Check each server's ax_update directory status] ******************************************************************************************************************************************** ok: [10.11.107.89] => (item={'ip': '47.94.174.254', 'port_num': '9010', 'line_num': '1', 'dist_id': '10000', 'line_num_cn': '??'}) => {"ansible_loop_var": "item", "changed": false, "examined": 0, "files": [], "item": {"dist_id": "10000", "ip": "47.94.174.254", "line_num": "1", "line_num_cn": "??", "port_num": "9010"}, "matched": 0, "msg": "All paths examined", "skipped_paths": {}} ok: [10.11.107.89] => (item={'ip': '47.94.174.254', 'port_num': '9012', 'line_num': '2', 'dist_id': '10000', 'line_num_cn': '??'}) => {"ansible_loop_var": "item", "changed": false, "examined": 0, "files": [], "item": {"dist_id": "10000", "ip": "47.94.174.254", "line_num": "2", "line_num_cn": "??", "port_num": "9012"}, "matched": 0, "msg": "All paths examined", "skipped_paths": {}} ok: [10.11.107.89] => (item={'ip': '47.94.174.254', 'port_num': '9014', 'line_num': '3', 'dist_id': '10000', 'line_num_cn': '??'}) => {"ansible_loop_var": "item", "changed": false, "examined": 0, "files": [], "item": {"dist_id": "10000", "ip": "47.94.174.254", "line_num": "3", "line_num_cn": "??", "port_num": "9014"}, "matched": 0, "msg": "All paths examined", "skipped_paths": {}} ok: [10.11.107.89] => (item={'ip': '47.94.174.254', 'port_num': '9016', 'line_num': '4', 'dist_id': '10000', 'line_num_cn': '??'}) => {"ansible_loop_var": "item", "changed": false, "examined": 0, "files": [], "item": {"dist_id": "10000", "ip": "47.94.174.254", "line_num": "4", "line_num_cn": "??", "port_num": "9016"}, "matched": 0, "msg": "All paths examined", "skipped_paths": {}} TASK [apex : Create proper server status list] ********************************************************************************************************************************************************** ok: [10.11.107.89] => {"ansible_facts": {"server_status": [{"empty": false, "exists": true, "line_num": 1, "path": []}, {"empty": false, "exists": true, "line_num": 2, "path": []}, {"empty": false, "exists": true, "line_num": 3, "path": []}, {"empty": false, "exists": true, "line_num": 4, "path": []}]}, "changed": false} TASK [apex : Print server_status] *********************************************************************************************************************************************************************** ok: [10.11.107.89] => { "server_status": [ { "empty": false, "exists": true, "line_num": 1, "path": [] }, { "empty": false, "exists": true, "line_num": 2, "path": [] }, { "empty": false, "exists": true, "line_num": 3, "path": [] }, { "empty": false, "exists": true, "line_num": 4, "path": [] } ] } TASK [apex : Ensure local directory exists for rsync] *************************************************************************************************************************************************** ok: [10.11.107.89] => {"changed": false, "gid": 0, "group": "root", "mode": "0755", "owner": "root", "path": "files/jd", "size": 4096, "state": "directory", "uid": 0} TASK [apex : Sync latest .zzz files from remote server] ************************************************************************************************************************************************* skipping: [10.11.107.89] => {"changed": false, "skip_reason": "Conditional result was False"} TASK [apex : Parse and set latest patch file] *********************************************************************************************************************************************************** ok: [10.11.107.89] => {"ansible_facts": {"patch_path": false}, "changed": false} TASK [apex : Terminate if no update patch found] ******************************************************************************************************************************************************** skipping: [10.11.107.89] => {"changed": false, "skip_reason": "Conditional result was False"} TASK [apex : Copy update pack only to empty directories] ************************************************************************************************************************************************ skipping: [10.11.107.89] => (item=svr1 (non-empty)) => {"ansible_loop_var": "item", "changed": false, "item": {"empty": false, "exists": true, "line_num": 1, "path": []}, "skip_reason": "Conditional result was False"} skipping: [10.11.107.89] => (item=svr2 (non-empty)) => {"ansible_loop_var": "item", "changed": false, "item": {"empty": false, "exists": true, "line_num": 2, "path": []}, "skip_reason": "Conditional result was False"} skipping: [10.11.107.89] => (item=svr3 (non-empty)) => {"ansible_loop_var": "item", "changed": false, "item": {"empty": false, "exists": true, "line_num": 3, "path": []}, "skip_reason": "Conditional result was False"} skipping: [10.11.107.89] => (item=svr4 (non-empty)) => {"ansible_loop_var": "item", "changed": false, "item": {"empty": false, "exists": true, "line_num": 4, "path": []}, "skip_reason": "Conditional result was False"} TASK [apex : Detect processes of GS] ******************************************************************************************************************************************************************** skipping: [10.11.107.89] => {"changed": false, "skip_reason": "Conditional result was False"} TASK [apex : Terminate if no gs worker] ***************************************************************************************************************************************************************** skipping: [10.11.107.89] => {"changed": false, "skip_reason": "Conditional result was False"} TASK [apex : Get worker state of port listening] ******************************************************************************************************************************************************** skipping: [10.11.107.89] => {"changed": false, "skip_reason": "Conditional result was False"} TASK [apex : Wait async wait_for] *********************************************************************************************************************************************************************** skipping: [10.11.107.89] => {"censored": "the output has been hidden due to the fact that 'no_log: true' was specified for this result", "changed": false} TASK [apex : Start apex worker] ************************************************************************************************************************************************************************* skipping: [10.11.107.89] => {"changed": false, "skip_reason": "Conditional result was False"} PLAY RECAP ********************************************************************************************************************************************************************************************** 10.11.107.89 : ok=13 changed=0 unreachable=0 failed=0 skipped=17 rescued=0 ignored=0 这个任务没有按预期执行,实际需求: 1.判断远程机器上{{ apex_home }}/svr{{ item.line_num }}/ax_update/目录下是否含有.zzz文件 2.如果不含则执行Sync latest .zzz files from remote server 和 Copy update pack only to empty directories 3.如果含有,则跳过执行 --- - name: DEBUG - Confirm re_update.yaml is running debug: msg: "re_update.yaml is definitely running!" - name: Set type to 'jd' if dist_id is between 7000-7999 ansible.builtin.set_fact: type: "jd" when: > dist_id is defined and (dist_id | int) >= 7000 and (dist_id | int) <= 7999 - name: Set tmp type to 'jd' ansible.builtin.set_fact: type: "jd" - name: Check each server's ax_update directory status ansible.builtin.find: path: "{{ apex_home }}/svr{{ item.line_num }}/ax_update/" recurse: no register: dir_status loop: "{{ lines }}" ignore_errors: yes # 兼容目录不存在的情况 changed_when: false - name: Create proper server status list ansible.builtin.set_fact: server_status: >- [{% for item in lines | zip(dir_status.results) %} { "line_num": {{ item.0.line_num }}, "path": {{ item.1.files | default([]) | to_json }}, "empty": {{ (item.1.files | default([]) | length) == 0 | to_json }}, "exists": {{ (item.1.files is defined) | to_json }} }{% if not loop.last %},{% endif %} {% endfor %}] - name: Print server_status ansible.builtin.debug: var: server_status - name: Ensure local directory exists for rsync ansible.builtin.file: path: "files/{{ type }}" state: directory mode: '0755' - name: Sync latest .zzz files from remote server ansible.builtin.command: > rsync -avz --port 50000 --include='*.zzz' --exclude='*' 10.14.101.113::apex_update/{{ type }}/ files/{{ type }}/ args: creates: "files/{{ type }}/" register: rsync_result changed_when: "'sent' in rsync_result.stdout" when: server_status | selectattr('empty') | list | length > 0 - name: Parse and set latest patch file ansible.builtin.set_fact: patch_path: "{{ query('filetree', 'files/' ~ type) | sort(attribute='mtime') | map(attribute='src') | last | default(false) }}" - name: Terminate if no update patch found ansible.builtin.fail: msg: "Update patch not found in files/{{ type }}/ after rsync" when: server_status | selectattr('empty') | list | length > 0 and not patch_path - name: Copy update pack only to empty directories ansible.builtin.copy: src: "{{ patch_path }}" dest: "{{ apex_home }}/svr{{ item.line_num }}/ax_update/" remote_src: false vars: patch_date: "{{ patch_path | regex_search(apex_date_re) }}" loop: "{{ server_status }}" when: - item.empty - not item.path | selectattr('path', 'contains', patch_date) | list loop_control: label: "svr{{ item.line_num }} ({{ 'empty' if item.empty else 'non-empty' }})"
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值