我是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
我的Ansible代码
- 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" !!!
TASK: [Debug result] **********************************************************
ok: [192.168.111.81] => {
"msg": "Hello world!"
}
如何检查shell输出并相应地运行命令。
我已更改debug: var=zlib_version.stdout
。它打印
TASK: [Debug result] **********************************************************
ok: [192.168.111.81] => {
"var": {
"zlib_version.stdout": "\"1.2.8\""
}
}
但是,现在我需要写两次zlib_version.stdout.find ("\"1.2.8\"") == -1
。是否有一种简单的方法可以使Ansible在匹配zlib_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
答案 0 :(得分:5)
shell命令的输出可通过df$mean2 <- with(df, (is.na(mean1)==F&(is.na(SD)==F))*mean1+ (is.na(mean1)==T & is.na(SD)==T & is.na(median)==F &
is.na(range_min)==F & is.na(range_max)==F & is.na(IQR_25)==F &
is.na(IQR_75)==F)*20)
属性获得。因此stdout
将为您提供命令的输出。
zlib_version.stdout
可以创建一个简单的role以避免两次写入条件。像这样:
- 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