运维开发网

Ansible 之 when判断语句使用

运维开发网 https://www.qedev.com 2021-01-22 13:09 出处:51CTO 作者:wx592bc92b285c7
一、when语句Ansiblewhen语句等同于if语句的作用二、判断test主机是否有redis-server进程----hosts:testvars_files:remote_user:cedarbecome:yesvars:test:"0"tasks:-name:"判断redis进程是否存在"shell:netstat-tlunp|grepredis-server|wc-lignore_er

一、when语句

Ansible when 语句等同于if语句的作用

二、判断test主机是否有redis-server进程

---
- hosts: test
  vars_files:
  remote_user: cedar
  become: yes
  vars:
    test: "0"
  tasks:
    - name: "判断redis进程是否存在"
      shell: netstat -tlunp | grep redis-server | wc -l
      ignore_errors: True
      register: result
    - name: "打印检查结果"
      debug:
        msg: "{{result}}"
    - name: "打印是否有redis-server进程"
      debug:
        msg: "{{result.stdout.find('1')}}"       
    - name: "判断是否启动"
      shell: touch /root/aaa.txt
      when: result.stdout.find('1') == 0        #判断屏幕输出结果是否为1

二、当test主机有redis-server进程时结果

[[email protected] playbook]# ansible-playbook process.yaml 

PLAY [test] *****************************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************
ok: [10.3.153.8]

TASK [判断redis进程是否存在] ********************************************************************************************************************************************
changed: [10.3.153.8]

TASK [打印检查结果] ***************************************************************************************************************************************************
ok: [10.3.153.8] => {
    "msg": {
        "changed": true, 
        "cmd": "netstat -tlunp | grep redis-server | wc -l", 
        "delta": "0:00:00.029704", 
        "end": "2021-01-21 08:23:15.244863", 
        "failed": false, 
        "rc": 0, 
        "start": "2021-01-21 08:23:15.215159", 
        "stderr": "", 
        "stderr_lines": [], 
        "stdout": "1", 
        "stdout_lines": [
            "1"
        ]
    }
}

TASK [打印是否有redis-server进程] **************************************************************************************************************************************
ok: [10.3.153.8] => {
    "msg": "0"                     #当test主机有redis-server进程时,该值为0,否则为-1
}

TASK [判断是否启动] ***************************************************************************************************************************************************
[WARNING]: Consider using the file module with state=touch rather than running 'touch'.  If you need to use command because file is insufficient you can add
'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
changed: [10.3.153.8]         #当redis-server存在时,执行该动作,创建文件

PLAY RECAP ******************************************************************************************************************************************************
10.3.153.8                 : ok=5    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

三、当test主机没有redis-server进程结果

[[email protected] playbook]# ansible-playbook process.yaml 

PLAY [test] *****************************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************
ok: [10.3.153.8]

TASK [判断redis进程是否存在] ********************************************************************************************************************************************
changed: [10.3.153.8]

TASK [打印检查结果] ***************************************************************************************************************************************************
ok: [10.3.153.8] => {
    "msg": {
        "changed": true, 
        "cmd": "netstat -tlunp | grep redis-server | wc -l", 
        "delta": "0:00:00.025883", 
        "end": "2021-01-21 08:23:46.505443", 
        "failed": false, 
        "rc": 0, 
        "start": "2021-01-21 08:23:46.479560", 
        "stderr": "", 
        "stderr_lines": [], 
        "stdout": "0", 
        "stdout_lines": [
            "0"
        ]
    }
}

TASK [打印是否有redis-server进程] **************************************************************************************************************************************
ok: [10.3.153.8] => {
    "msg": "-1"
}

TASK [判断是否启动] ***************************************************************************************************************************************************
skipping: [10.3.153.8]         #当没有redis-server进程时,跳过此步骤,不创建文件

PLAY RECAP ******************************************************************************************************************************************************
10.3.153.8                 : ok=4    changed=1    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   

四、技术点解释

1、register 大部分情况下,我们使用注册器用来接收shell命令的返回结果,结果中包含标准输出(stdout)和错误输出(stderr)。

扫码领视频副本.gif

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号