首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ansible:即使在调试后显示其内容时,已注册变量仍未定义

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

Stack Overflow用户
提问于 2020-02-25 12:03:28
回答 1查看 589关注 0票数 0

Ansuble寄存器变量

我有一个任务列表,其中我注册了一个shell任务的输出。shell命令列出一个目录,然后将输出传送到grep。任务成功了,我得到了我想要的输出。播放簿SVN源文件中的耳机将被选中,并传输到{ DestRemoteDir }}目录。所述代码从命令的输出生成一个列表。我把这些清单用来在后面的剧本中列出其他各种事实。[ Mapping a list of Debian packages in a specific order of 2nd list with Ansible ](我知道ansible的find模块,但我发现"ls -1“更容易使用)。

剧本(任务/main.yml)

代码语言:javascript
复制
## 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

输出

代码语言:javascript
复制
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.deb\niaxdautocfg_1.0.0.0_all.deb\nlibjpegdeb7u1_amd64.deb\nopenjdk-6-jre_610-1~deb7u1_amd64.deb\nopenjdk-6-jre-headless_610-1~deb7u1_amd64.deb\nopenjdk-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"
    ]
}

调试

代码语言:javascript
复制
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":“iaxd_List”和"stdout_lines":"iaxd_1.2.7.6-15_all.deb",.准备好了。

因此,从调试任务中,我看到了变量的存在,并定义了这些变量(至少我是这么认为的)。但是当我测试shell命令是否失败时。

错误消息

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

代码语言:javascript
复制
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",因为变量不存在,所以不存在错误。

代码语言:javascript
复制
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 undefined\n\nThe error appears to have been in '/home/ansible/ansible_server_working/roles/server_iaxd_install/tasks/main.yml': line 69, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Print if undefined Deb_list.stdout_lines\n  ^ here\n"
}

这要么是一个“树木森林”(我看不出明显的)错误,要么是一个恼人的怪癖,我需要在周围。

EN

回答 1

Stack Overflow用户

发布于 2020-02-25 15:37:57

是的,这是“森林换树”(我看不出明显的)错误。查找错误后,我更改代码。

代码语言:javascript
复制
- 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

现在的产出要好得多:

代码语言:javascript
复制
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] => {}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60394216

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档