0
votes

Ansuble Register Variables

I have task list where I register output from a shell task. The shell command lists a directory and then pipes the output to grep. The task works and I get the output I wanted. Eariler in the playbook SVN source files are checked outed and transferred to {{ DestRemoteDir }} directory. The code in question builds a list from the output of the command. I take those lists to set various other facts later in the playbook. [ Mapping a list of Debian packages in a specific order of 2nd list with Ansible ] (I know of ansible's find module, but I found "ls -1" far easier to use.)

Playbook (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

Output

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"
    ]
}

Debug

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']"
    ]
}

Above both "stdout_lines": [ "IA-XD_List" ] and "stdout_lines": [ "iaxd_1.2.7.6-15_all.deb",...] are ready.

So from the debug task I see that the variables are there and defined (Or so I thought). But when I test to see if there if the shell command failed.

Error Message

The status clearly shows that Deb_List is undefined. why? I just debug thad value of that variable.

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'

Try to test the key data I want "Deb_list.stdout_lines", Ansible errors as the variable is not there.

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"
}

Either this is a "forest for the trees" (I can't see the obvious) error or an annoying quirk of Ansible that I need to kluge around.

1
Capitalization: when: Deb_list is undefined should be when: Deb_List is undefined.Jack
Yep, that is it.Richard E

1 Answers

0
votes

Yes, it was a "forest for the trees" (I can't see the obvious) error. After looking for the error, I change the code.

- 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

And now the output is far better:

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] => {}