Ansible:注册变量未定义,即使在调试显示其内容后



Anuble寄存器变量

我有一个任务列表,用于注册shell任务的输出。shell命令列出一个目录,然后通过管道将输出输出到grep。任务成功了,我得到了我想要的输出。剧本SVN源文件中的Earler被检查路由并传输到{{DestRemoteDir}}目录。有问题的代码根据命令的输出构建一个列表。我用这些列表来设定剧本后面的各种其他事实。[用Ansible按第二个列表的特定顺序映射Debian包的列表](我知道Ansible的查找模块,但我发现"ls-1"更容易使用。(

行动手册(tasks/main.yml(

## use List -1 to find the file names for the deb files.| grep
## It stays, The tasks work as expected.
- name: Find the needed deb files
shell: "ls -1 {{ DestRemoteDir }} | grep .*.deb"  # Local dir
register: Deb_List
ignore_errors: yes
delegate_to: 127.0.0.1 #localhosts
- name: Find the needed IXAD list file
shell: "ls -1 {{ DestRemoteDir }} | grep IA-XD"  # Local dir
register: IXAD_List_Source
ignore_errors: yes
delegate_to: 127.0.0.1 #localhosts
- name: Print the Deblist for IX-AD
debug:
msg:
- "Deb file list: {{ Deb_List.stdout_lines }}"
- "IXAD file {{ IXAD_List_Source.stdout_lines }}"
- name: Print if undefined Deb_list
debug:
msg: "Deb_List is not real"
when: Deb_list is undefined
- name: Print if undefined Deb_list.stdout_lines
debug:
msg: "Deb_List.stdout_lines is not real"
when: Deb_list.stdout_lines is undefined

输出

changed: [AnsibleTest2 -> 127.0.0.1] => {
"changed": true,
"cmd": "ls -1 /home/ansible/IA-XD | grep .*.deb",
***
"invocation": {
"module_args": {
***
},
"rc": 0,
"stderr": "",
"stderr_lines": [],
"stdout": "iaxd_1.2.7.6-15_all.debniaxdautocfg_1.0.0.0_all.debnlibjpegdeb7u1_amd64.debnopenjdk-6-jre_610-1~deb7u1_amd64.debnopenjdk-6-jre-headless_610-1~deb7u1_amd64.debnopenjdk-610-1~deb7u1_all.deb",
"stdout_lines": [
"iaxd_1.2.7.6-15_all.deb",
"iaxdautocfg_1.0.0.0_all.deb",
"libjpeg7u1_amd64.deb",
"openjdk-6-jre_610-1~deb7u1_amd64.deb",
"openjdk-6-jre-headless_610-1~deb7u1_amd64.deb",
"openjdk-6-jre-lib_610-1~deb7u1_all.deb"
]
}
changed: [AnsibleTest2 -> 127.0.0.1] => {
"changed": true,
"cmd": "ls -1 /home/ansible/IA-XD | grep IA-XD",
"invocation": {
"module_args": {
"_raw_params": "ls -1 /home/ansible/IA-XD | grep IA-XD",
***
}
},
"rc": 0,
"stderr": "",
"stderr_lines": [],
"stdout": "IA-XD_List",
"stdout_lines": [
"IA-XD_List"
]
}

调试

ok: [AnsibleTest2] => {
"msg": [
"Deb file list: [u'iaxd_1.2.7.6-15_all.deb',u'iaxdautocfg_1.0.0.0_all.deb',u'libjpegdeb7u1_amd64.deb',u'openjdk-6-jre_610-1~deb7u1_amd64.deb',u'openjdk-6-jre-headless_610-1~deb7u1_amd64.deb',u'openjdk-610-1~deb7u1_all.deb']",
"IXAD file [u'IA-XD_List']"
]
}

上面的"stdout_lines":["IA-XD_List"]和"stdout-lines":"iaxd_1.2.7.6-15_all.deb",…]都准备好了。

因此,从调试任务中,我看到变量已经存在并定义好了(或者我是这么想的(。但是当我测试shell命令是否失败时。

错误消息

状态清楚地显示Deb_List未定义为什么我只是调试那个变量的值。

TASK [server_iaxd_install : Print if undefined Deb_list] *********************************************************************
task path: /home/ansible/ansible_server_working/roles/server_iaxd_install/tasks/main.yml:64
Tuesday 25 February 2020  11:50:12 +0100 (0:00:00.222)       0:00:07.890 ******
ok: [AnsibleTest2] => {
"msg": "Deb_List is not real"
}
Read vars_file '/home/ansible/.ssh/SudoPassKey'

试着测试我想要的关键数据"Deb_list.stdout_lines",Ansible错误,因为变量不在那里。

TASK [server_iaxd_install : Print if undefined Deb_list.stdout_lines] ********************************************************
task path: /home/ansible/ansible_server_working/roles/server_iaxd_install/tasks/main.yml:69
Tuesday 25 February 2020  11:50:12 +0100 (0:00:00.197)       0:00:08.087 ******
fatal: [AnsibleTest2]: FAILED! => {
"msg": "The conditional check 'Deb_list.stdout_lines is undefined' failed. The error was: error while evaluating conditional (Deb_list.stdout_lines is undefined): 'Deb_list' is undefinednnThe error appears to have been in '/home/ansible/ansible_server_working/roles/server_iaxd_install/tasks/main.yml': line 69, column 3, but maynbe elsewhere in the file depending on the exact syntax problem.nnThe offending line appears to be:nnn- name: Print if undefined Deb_list.stdout_linesn  ^ heren"
}

要么这是一个"以林换树"的错误(我看不出明显的错误(,要么是Ansible令人讨厌的怪癖,我需要四处闲逛。

是的,这是一个"森林换树木"的错误(我看不出明显的(。查找错误后,我更改了代码。

- name: Print if undefined Deb_List
debug:
msg: "Deb_List is not real"
when: Deb_List is undefined
- name: Print if undefined Deb_List.stdout_lines
debug:
msg: "Deb_List.stdout_lines is not real"
when: Deb_List.stdout_lines is undefined

现在的输出要好得多:

TASK [server_iaxd_install : Print if undefined Deb_List] *********************************************************************
task path: /home/ansible/ansible_server_working/roles/server_iaxd_install/tasks/main.yml:77
Tuesday 25 February 2020  16:00:34 +0100 (0:00:00.242)       0:00:08.013 ******
skipping: [AnsibleTest2] => {}
Read vars_file '/home/ansible/.ssh/SudoPassKey'
TASK [server_iaxd_install : Print if undefined Deb_List.stdout_lines] ********************************************************
task path: /home/ansible/ansible_server_working/roles/server_iaxd_install/tasks/main.yml:82
Tuesday 25 February 2020  16:00:34 +0100 (0:00:00.065)       0:00:08.078 ******
skipping: [AnsibleTest2] => {}

最新更新