英文:
Ansible task fails, but same command works manually
问题
这是您提供的Ansible任务和输出的翻译:
任务名称:配置Tomcat本地APR库
这个任务是在Ansible任务中运行的命令,如您所看到的,它正在配置Tomcat本地APR库:
- name: 配置Tomcat本地APR库
become_user: root
become: true
shell: "./configure --with-ssl=/usr/local/ssl --with-java-home=/usr/lib/jvm/java-11-amazon-corretto"
chdir: "/opt/tomcat/bin/tomcat-native-1.2.23-src/native"
register: configureoutput
这给我以下输出:
任务 [../../../../roles/ghi.install_apr_tomcat:配置Tomcat本地APR库] ***
已更改:[aps01]
已更改:[aps02]
任务 [../../../../roles/ghi.install_apr_tomcat:调试] ************************
好:[aps01] => {
"configureoutput": {
"changed": true,
"cmd": "./configure --with-ssl=/usr/local/ssl --with-java-home=/usr/lib/jvm/java-11-amazon-corretto",
"delta": "0:00:00.712919",
"end": "2023-02-17 21:14:13.094244",
"failed": false,
"rc": 0,
"start": "2023-02-17 21:14:12.381325",
"stderr": "./configure: line 3139: cd: /bin/apr-1-config//usr/lib64/apr-1/build: Not a directory\ncp: cannot stat '/apr_rules.mk': No such file or directory",
"stderr_lines": [
"./configure: line 3139: cd: /bin/apr-1-config//usr/lib64/apr-1/build: Not a directory",
"cp: cannot stat '/apr_rules.mk': No such file or directory"
],
"stdout": "checking build system type... x86_64-pc-linux-gnu\nchecking host system type... x86_64-pc-linux-gnu\nchecking target system type... x86_64-pc-linux-gnu\nchecking for a BSD-compatible install... /bin/install -c\nchecking for working mkdir -p... yes\nTomcat Native Version: 1.2.36\nchecking for chosen layout... tcnative\nchecking for APR... yes\nconfigure: APR 1.7.2 detected.\n setting CC to \"gcc\"\n setting CPP to \"gcc -E\"\nchecking JAVA_HOME... /usr/lib/jvm/java-11-amazon-corretto\n adding \"-I/usr/lib/jvm/java-11-amazon-corretto/include\" to TCNATIVE_PRIV_INCLUDES\nchecking for JDK os include directory... linux\n adding \"-I/usr/lib/jvm/java-11-amazon-corretto/include/linux\" to TCNATIVE_PRIV_INCLUDES\nchecking for gcc... gcc\nchecking whether the C compiler works... yes\nchecking for C compiler default output file name... a.out\nchecking for suffix of executables... \nchecking whether we are cross compiling... no\nchecking for suffix of object files... o\nchecking whether the compiler supports GNU C... yes\nchecking whether gcc accepts -g... yes\nchecking for gcc option to enable C11 features... none needed\nchecking for OpenSSL library... using openssl from /usr/local/ssl/lib and /usr/local/ssl/include\nchecking OpenSSL library version >= 1.0.2... ok\nchecking for OpenSSL DSA support... no\n adding \"-I/usr/local/ssl/include\" to TCNATIVE_PRIV_INCLUDES\n setting TCNATIVE_LDFLAGS to \"-L/usr/local/ssl/lib -Wl,-rpath,/usr/local/ssl/lib -lssl -lcrypto\"\n adding \"-DHAVE_OPENSSL\" to CFLAGS\n setting TCNATIVE_LIBS to \"\"\n setting TCNATIVE_LIBS to \" -L/bin/apr-1-config//usr/lib64 -lapr-1 -lpthread -ldl\"\n adding \"-DTCNATIVE_LINUX\" to CFLAGS\nchecking for apr_pollset_wakeup in -lapr-1... no\nconfigure: creating ./config.status\nconfig.status: creating Makefile\nconfig.status: executing default commands",
"stdout_lines": [
"checking build system type... x86_64-pc-linux-gnu",
"checking host system type... x86_64-pc-linux-gnu",
"checking target system type... x86_64-pc-linux-gnu",
"checking for a BSD-compatible install... /bin/install -c",
"checking for working mkdir -p... yes",
"Tomcat Native Version: 1.2.36",
"checking for chosen layout... tcnative",
"checking for APR... yes",
"configure: APR 1.7.2 detected.",
" setting CC to \"gcc\"",
" setting CPP to \"gcc -E\"",
"checking JAVA_HOME... /usr/lib/jvm/java-11-amazon-corretto",
" adding \"-I/usr/lib/jvm/java-11-amazon-corretto/include\" to TCNATIVE_PRIV_INCLUDES",
"checking for JDK os include directory... linux",
" adding \"-I/usr/lib/jvm/java-11-amazon-corretto/include/linux\" to TCNATIVE_PRIV_INCLUDES",
"checking for gcc... gcc",
"checking whether the C compiler works... yes",
"checking for C compiler default output file name... a.out",
"checking for suffix of executables... ",
"checking whether we are cross compiling... no",
"checking for suffix of object files... o",
"checking whether the compiler supports GNU C... yes",
"checking whether gcc accepts -g... yes",
"checking for gcc option to enable C11 features... none needed",
"checking for OpenSSL library... using openssl from /usr/local/ssl/lib and /usr/local/ssl/include",
"checking OpenSSL library version >= 1.0.2... ok",
"checking for OpenSSL DSA support... no",
" adding \"-I/usr/local/ssl/include\" to TCNATIVE_PRIV_INCLUDES",
" setting TCNATIVE_LDFLAGS to \"-L/usr/local/ssl/lib -Wl,-rpath,/usr/local/ssl/lib -lssl -lcrypto\"",
" adding \"-DHAVE_OPENSSL\" to CFLAGS",
" setting TCNATIVE_LIBS to \"\"",
" setting TCNATIVE_LIBS to \" -L/bin/apr-1-config//usr/lib64 -lapr-1 -lpthread -ldl\"",
" adding \"-DTCNATIVE_LINUX\" to CFLAGS",
"checking for apr_pollset_wakeup in -lapr-1... no",
"configure: creating ./config.status",
"config.status: creating
<details>
<summary>英文:</summary>
I'm running command in Ansible task, as you can clearly see its configuring tomcat-native:
- name: configure tomcat apr native library
become_user: root
become: true
shell: "./configure --with-ssl=/usr/local/ssl --with-java-home=/usr/lib/jvm/java-11-amazon-corretto"
chdir: "/opt/tomcat/bin/tomcat-native-1.2.23-src/native"
register: configureoutput
Which gives me the following output:
TASK [../../../../roles/ghi.install_apr_tomcat : configure tomcat apr native library] ***
changed: [aps01]
changed: [aps02]
TASK [../../../../roles/ghi.install_apr_tomcat : debug] ************************
ok: [aps01] => {
"configureoutput": {
"changed": true,
"cmd": "./configure --with-ssl=/usr/local/ssl --with-java-home=/usr/lib/jvm/java-11-amazon-corretto",
"delta": "0:00:00.712919",
"end": "2023-02-17 21:14:13.094244",
"failed": false,
"rc": 0,
"start": "2023-02-17 21:14:12.381325",
"stderr": "./configure: line 3139: cd: /bin/apr-1-config//usr/lib64/apr-1/build: Not a directory\ncp: cannot stat '/apr_rules.mk': No such file or directory",
"stderr_lines": [
"./configure: line 3139: cd: /bin/apr-1-config//usr/lib64/apr-1/build: Not a directory",
"cp: cannot stat '/apr_rules.mk': No such file or directory"
],
"stdout": "checking build system type... x86_64-pc-linux-gnu\nchecking host system type... x86_64-pc-linux-gnu\nchecking target system type... x86_64-pc-linux-gnu\nchecking for a BSD-compatible install... /bin/install -c\nchecking for working mkdir -p... yes\nTomcat Native Version: 1.2.36\nchecking for chosen layout... tcnative\nchecking for APR... yes\nconfigure: APR 1.7.2 detected.\n setting CC to "gcc"\n setting CPP to "gcc -E"\nchecking JAVA_HOME... /usr/lib/jvm/java-11-amazon-corretto\n adding "-I/usr/lib/jvm/java-11-amazon-corretto/include" to TCNATIVE_PRIV_INCLUDES\nchecking for JDK os include directory... linux\n adding "-I/usr/lib/jvm/java-11-amazon-corretto/include/linux" to TCNATIVE_PRIV_INCLUDES\nchecking for gcc... gcc\nchecking whether the C compiler works... yes\nchecking for C compiler default output file name... a.out\nchecking for suffix of executables... \nchecking whether we are cross compiling... no\nchecking for suffix of object files... o\nchecking whether the compiler supports GNU C... yes\nchecking whether gcc accepts -g... yes\nchecking for gcc option to enable C11 features... none needed\nchecking for OpenSSL library... using openssl from /usr/local/ssl/lib and /usr/local/ssl/include\nchecking OpenSSL library version >= 1.0.2... ok\nchecking for OpenSSL DSA support... no\n adding "-I/usr/local/ssl/include" to TCNATIVE_PRIV_INCLUDES\n setting TCNATIVE_LDFLAGS to "-L/usr/local/ssl/lib -Wl,-rpath,/usr/local/ssl/lib -lssl -lcrypto"\n adding "-DHAVE_OPENSSL" to CFLAGS\n setting TCNATIVE_LIBS to ""\n setting TCNATIVE_LIBS to " -L/bin/apr-1-config//usr/lib64 -lapr-1 -lpthread -ldl"\n adding "-DTCNATIVE_LINUX" to CFLAGS\nchecking for apr_pollset_wakeup in -lapr-1... no\nconfigure: creating ./config.status\nconfig.status: creating Makefile\nconfig.status: executing default commands",
"stdout_lines": [
"checking build system type... x86_64-pc-linux-gnu",
"checking host system type... x86_64-pc-linux-gnu",
"checking target system type... x86_64-pc-linux-gnu",
"checking for a BSD-compatible install... /bin/install -c",
"checking for working mkdir -p... yes",
"Tomcat Native Version: 1.2.36",
"checking for chosen layout... tcnative",
"checking for APR... yes",
"configure: APR 1.7.2 detected.",
" setting CC to "gcc"",
" setting CPP to "gcc -E"",
"checking JAVA_HOME... /usr/lib/jvm/java-11-amazon-corretto",
" adding "-I/usr/lib/jvm/java-11-amazon-corretto/include" to TCNATIVE_PRIV_INCLUDES",
"checking for JDK os include directory... linux",
" adding "-I/usr/lib/jvm/java-11-amazon-corretto/include/linux" to TCNATIVE_PRIV_INCLUDES",
"checking for gcc... gcc",
"checking whether the C compiler works... yes",
"checking for C compiler default output file name... a.out",
"checking for suffix of executables... ",
"checking whether we are cross compiling... no",
"checking for suffix of object files... o",
"checking whether the compiler supports GNU C... yes",
"checking whether gcc accepts -g... yes",
"checking for gcc option to enable C11 features... none needed",
"checking for OpenSSL library... using openssl from /usr/local/ssl/lib and /usr/local/ssl/include",
"checking OpenSSL library version >= 1.0.2... ok",
"checking for OpenSSL DSA support... no",
" adding "-I/usr/local/ssl/include" to TCNATIVE_PRIV_INCLUDES",
" setting TCNATIVE_LDFLAGS to "-L/usr/local/ssl/lib -Wl,-rpath,/usr/local/ssl/lib -lssl -lcrypto"",
" adding "-DHAVE_OPENSSL" to CFLAGS",
" setting TCNATIVE_LIBS to """,
" setting TCNATIVE_LIBS to " -L/bin/apr-1-config//usr/lib64 -lapr-1 -lpthread -ldl"",
" adding "-DTCNATIVE_LINUX" to CFLAGS",
"checking for apr_pollset_wakeup in -lapr-1... no",
"configure: creating ./config.status",
"config.status: creating Makefile",
"config.status: executing default commands"
]
}
}
But the same command completes successfully when I run it manually (Makefile is created only when configure was successful):
[root@ip-############ native]# ./configure --with-ssl=/usr/local/ssl --with-java-home=/usr/lib/jvm/java-11-amazon-corretto
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
checking for a BSD-compatible install... /usr/bin/install -c
checking for working mkdir -p... yes
Tomcat Native Version: 1.2.36
checking for chosen layout... tcnative
checking for APR... yes
configure: APR 1.7.2 detected.
setting CC to "gcc"
setting CPP to "gcc -E"
checking JAVA_HOME... /usr/lib/jvm/java-11-amazon-corretto
adding "-I/usr/lib/jvm/java-11-amazon-corretto/include" to TCNATIVE_PRIV_INCLUDES
checking for JDK os include directory... linux
adding "-I/usr/lib/jvm/java-11-amazon-corretto/include/linux" to TCNATIVE_PRIV_INCLUDES
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether the compiler supports GNU C... yes
checking whether gcc accepts -g... yes
checking for gcc option to enable C11 features... none needed
checking for OpenSSL library... using openssl from /usr/local/ssl/lib and /usr/local/ssl/include
checking OpenSSL library version >= 1.0.2... ok
checking for OpenSSL DSA support... no
adding "-I/usr/local/ssl/include" to TCNATIVE_PRIV_INCLUDES
setting TCNATIVE_LDFLAGS to "-L/usr/local/ssl/lib -Wl,-rpath,/usr/local/ssl/lib -lssl -lcrypto"
adding "-DHAVE_OPENSSL" to CFLAGS
setting TCNATIVE_LIBS to ""
setting TCNATIVE_LIBS to " /usr/lib64/libapr-1.la -lpthread -ldl"
adding "-DTCNATIVE_LINUX" to CFLAGS
checking for apr_pollset_wakeup in -lapr-1... yes
adding "-DHAVE_POLLSET_WAKEUP" to CFLAGS
configure: creating ./config.status
config.status: creating Makefile
config.status: executing default commands
[root@ip-############ native]# ls -la
total 336
drwxrwxr-x 7 tomcat tomcat 4096 Feb 17 21:47 .
drwxrwxr-x 8 tomcat tomcat 304 Feb 8 15:52 ..
drwxrwxr-x 3 tomcat tomcat 257 Feb 17 21:47 build
-rwxrwxr-x 1 tomcat tomcat 2946 Feb 8 15:52 buildconf
-rw-rw-r-- 1 tomcat tomcat 1098 Feb 8 15:52 build.conf
-rw-rw-r-- 1 tomcat tomcat 6191 Feb 8 15:52 BUILDING
-rw-rw-r-- 1 tomcat tomcat 3587 Feb 8 15:52 build-outputs.mk
-rw-rw-r-- 1 tomcat tomcat 8769 Feb 8 15:52 config.layout
-rw-r--r-- 1 root root 10260 Feb 17 21:47 config.log
-rwxr-xr-x 1 root root 146 Feb 17 21:47 config.nice
-rwxr-xr-x 1 root root 26231 Feb 17 21:47 config.status
-rwxrwxr-x 1 tomcat tomcat 170805 Feb 8 15:52 configure
-rw-rw-r-- 1 tomcat tomcat 7269 Feb 8 15:52 configure.in
drwxrwxr-x 2 tomcat tomcat 78 Feb 8 15:52 include
-rw-rw-r-- 1 tomcat tomcat 6845 Feb 8 15:52 libtcnative.dsp
-rw-rw-r-- 1 tomcat tomcat 1214 Feb 8 15:52 libtcnative.dsw
-rw-rw-r-- 1 tomcat tomcat 17499 Feb 8 15:52 LICENSE.bin.win
-rw-r--r-- 1 root root 2766 Feb 17 21:47 Makefile
-rw-rw-r-- 1 tomcat tomcat 2376 Feb 8 15:52 Makefile.in
-rw-rw-r-- 1 tomcat tomcat 5413 Feb 8 15:52 NMAKEmakefile
-rw-rw-r-- 1 tomcat tomcat 9745 Feb 8 15:52 NMAKEmakefile.inc
-rw-rw-r-- 1 tomcat tomcat 695 Feb 8 15:52 NOTICE.bin.win
drwxrwxr-x 4 tomcat tomcat 31 Feb 8 15:52 os
drwxrwxr-x 2 tomcat tomcat 4096 Feb 8 15:52 src
drwxrwxr-x 4 tomcat tomcat 64 Feb 8 15:52 srclib
-rw-rw-r-- 1 tomcat tomcat 6833 Feb 8 15:52 tcnative.dsp
-rw-rw-r-- 1 tomcat tomcat 2833 Feb 8 15:52 tcnative.spec
And the most fun thing is: my task was working on February 10-th (merged into main branch), then somebody tried to deploy this code on February 15-th and it failed. Commit hash is the same, no MR/PR was merged into main branch.
Does anyone have detective skillzZz?
edit:
One more important thing to notice:
Ansible stderr says -
```"stderr": "./configure: line 3139: cd: /bin/apr-1-config//usr/lib64/apr-1/build: Not a directory\ncp: cannot stat '/apr_rules.mk': No such file or directory",```
if we look into that file, we'll see
[root@ip-172-24-8-48 ~]# sed -n 3139p /opt/tomcat/bin/tomcat-native-1.2.36-src/native/configure
APR_BUILD_DIR="cd $APR_BUILD_DIR && pwd
"
So, the root of the problem might be it. For some reason Ansible calculating value for APR_BUILD_DIR in a wrong way. I don't get it. It's official tars from Tomcat website and I'm installing them in a required way.
</details>
# 答案1
**得分**: 2
A: 出现的错误是:
> "stderr": "./configure: 第 3139 行: cd: /bin/apr-1-config//usr/lib64/apr-1/build: 不是一个目录\ncp: 无法查找'/apr_rules.mk':没有那个文件或目录",
你应该找出为什么缺少 `/apr_rules.mk`。
<hr>
<sup>
[chdir](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/shell_module.html#parameter-chdir) 不是一个 [关键字](https://docs.ansible.com/ansible/latest/reference_appendices/playbooks_keywords.html#playbook-keywords),它是模块 *shell* 的参数。如果你运行了这个任务:
```yaml
- name: configure tomcat apr native library
become_user: root
become: true
shell: "./configure --with-ssl=/usr/local/ssl --with-java-home=/usr/lib/jvm/java-11-amazon-corretto"
chdir: "/opt/tomcat/bin/tomcat-native-1.2.23-src/native"
register: configureoutput
你必须已经看到了这个错误:
错误!冲突的操作语句:shell、chdir
正确的语法是将 chdir 嵌套作为 shell 的参数:
- name: configure tomcat apr native library
become_user: root
become: true
shell: "./configure --with-ssl=/usr/local/ssl --with-java-home=/usr/lib/jvm/java-11-amazon-corretto"
chdir: "/opt/tomcat/bin/tomcat-native-1.2.23-src/native"
register: configureoutput
英文:
Q: "cannot stat '/apr_rules.mk': No such file or directory"
A: The error is:
> "stderr": "./configure: line 3139: cd: /bin/apr-1-config//usr/lib64/apr-1/build: Not a directory\ncp: cannot stat '/apr_rules.mk': No such file or directory",
You should find out why /apr_rules.mk
is missing.
<hr>
<sup>
chdir is not a keyword it is parameter of the module shell. If you ran the task
- name: configure tomcat apr native library
become_user: root
become: true
shell: "./configure --with-ssl=/usr/local/ssl --with-java-home=/usr/lib/jvm/java-11-amazon-corretto"
chdir: "/opt/tomcat/bin/tomcat-native-1.2.23-src/native"
register: configureoutput
you must have seen this error:
> ERROR! conflicting action statements: shell, chdir
The correct syntax is chdir nested as a parameter of shell
- name: configure tomcat apr native library
become_user: root
become: true
shell: "./configure --with-ssl=/usr/local/ssl --with-java-home=/usr/lib/jvm/java-11-amazon-corretto"
chdir: "/opt/tomcat/bin/tomcat-native-1.2.23-src/native"
register: configureoutput
</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论