title: ansible: how to check shell output
body: <p>I am new to Ansible(1.9.2). I want to check the current zlib version. If it is not 1.2.8 then Ansible need to install it from source.</p>
  
  <p>The command which check the current zlib version is</p>
  
  <pre><code>root@node2 zlib-1.2.8]# cat /usr/local/include/zlib.h | grep "#define ZLIB_VERSION" | awk '{print $3}'
  "1.2.8
  </code></pre>
  
  <p>My Ansible code</p>
  
  <pre><code> - name: "Check zlib version "
        shell: "cat /usr/local/include/zlib.h | grep \"#define ZLIB_VERSION\" | awk '{print $3}'"
        register: zlib_version
      - name: "Debug result"
        debug: result
      - name: "Build zlib "
        command: "{{ item }} chdir=/home/zlib-1.2.8"
        with_items:
          - "./configure --prefix=/usr/local --shared"
          - make
          - make install
        when: "zlib_version!=1.2.8"
  </code></pre>
  
  <p>The debug prints "hello world"!!!</p>
  
  <pre><code>TASK: [Debug result] ********************************************************** 
  ok: [192.168.111.81] =&gt; {
      "msg": "Hello world!"
  }
  </code></pre>
  
  <p>How do I check the shell output and run command accordingly. </p>
  
  <p>I have changed <code>debug: var=zlib_version.stdout</code>. It prints</p>
  
  <pre><code>TASK: [Debug result] ********************************************************** 
  ok: [192.168.111.81] =&gt; {
      "var": {
          "zlib_version.stdout": "\"1.2.8\""
      }
  }
  </code></pre>
  
  <p>However, now I need to write <code>zlib_version.stdout.find ("\"1.2.8\"") == -1</code>, twice. Is there an easy way so that Ansible skip all command,shell and unarchive when it matches <code>zlib_version.stdout.find ("\"1.2.8\"") == -1</code>?</p>
  
  <pre><code> - name: "Check zlib version "
        shell: "cat /usr/local/include/zlib.h | grep \"#define ZLIB_VERSION\" | awk '{print $3}'"
        ignore_errors: true
        register: zlib_version
      - unarchive: src=/home/files/nigex/zlib-1.2.8.tar.gz   dest=/home/
        when: zlib_version.stdout.find ("\"1.2.8\"") == -1
  
      - name: "Debug result"
        debug: var=zlib_version.stdout
      - name: "Build zlib "
        command: "{{ item }} chdir=/home/zlib-1.2.8"
        with_items:
          - "./configure --prefix=/usr/local --shared"
          - make
          - make install
        when: zlib_version.stdout.find ("\"1.2.8\"") == -1
  </code></pre>