ansible:如何检查shell输出
我是Ansible(1.9.2)的新手。我想检查当前的zlib版本。如果不是1.2.8,则Ansible需要从源代码处安装 检查当前zlib版本的命令为ansible:如何检查shell输出,ansible,Ansible,我是Ansible(1.9.2)的新手。我想检查当前的zlib版本。如果不是1.2.8,则Ansible需要从源代码处安装 检查当前zlib版本的命令为 root@node2 zlib-1.2.8]# cat /usr/local/include/zlib.h | grep "#define ZLIB_VERSION" | awk '{print $3}' "1.2.8 我的易解码 - name: "Check zlib version " shell: "cat /usr/lo
root@node2 zlib-1.2.8]# cat /usr/local/include/zlib.h | grep "#define ZLIB_VERSION" | awk '{print $3}'
"1.2.8
我的易解码
- 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"
调试打印“hello world”
如何检查shell输出并相应地运行命令
我已更改调试:var=zlib\u version.stdout。它打印
TASK: [Debug result] **********************************************************
ok: [192.168.111.81] => {
"var": {
"zlib_version.stdout": "\"1.2.8\""
}
}
但是,现在我需要编写zlib\u version.stdout.find(“\'1.2.8\”)==-1
,两次。有没有一种简单的方法可以让Ansible在匹配zlib\u version.stdout.find(“\”1.2.8\”)==-1时跳过所有命令、shell和unarchive
- 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
shell命令的输出可通过stdout
property获得。因此,zlib\u version.stdout
将为您提供命令的输出
- name: "Debug result"
debug: var=zlib_version.stdout
可以创建一个简单的脚本来避免两次写入条件。大概是这样的:
# file roles/zlib/tasks/main.yml
---
- unarchive: src=/home/files/nigex/zlib-1.2.8.tar.gz dest=/home/
- name: "Build zlib "
command: "{{ item }} chdir=/home/zlib-1.2.8"
with_items:
- "./configure --prefix=/usr/local --shared"
- make
- make install
然后仅当条件为true时才包含角色
# pre_tasks is needed instead of tasks since this needs to be run before the role is included
pre_tasks:
- name: "Check zlib version "
shell: "cat /usr/local/include/zlib.h | grep \"#define ZLIB_VERSION\" | awk '{print $3}'"
ignore_errors: true
register: zlib_version
roles:
- { role: zlib, when: "zlib_version.stdout.find ('\"1.2.8\"') == -1" }
谢谢你的回复。我已经更改了调试行,它可以正常工作。但是,我无法比较shell输出。@Khoga,我认为您的做法正好相反。您需要zlib\u version.stdout.find(“\'1.2.8\”)==-1
。谢谢。它是有效的:)。有一件事,请查看我的帖子。现在我需要输入两次[when:zlib\u version.stdout.find(“\”1.2.8\”)=-1]。有没有一个简单的方法,使Ansible跳过所有的命令,无存档和shell等,当它是真的吗?谢谢朋友。你真的帮了大忙。谢谢:)
# pre_tasks is needed instead of tasks since this needs to be run before the role is included
pre_tasks:
- name: "Check zlib version "
shell: "cat /usr/local/include/zlib.h | grep \"#define ZLIB_VERSION\" | awk '{print $3}'"
ignore_errors: true
register: zlib_version
roles:
- { role: zlib, when: "zlib_version.stdout.find ('\"1.2.8\"') == -1" }