Odpowiedź: nie można uzyskać zawartości zmiennej


1

Wydaje mi się, że nie mogę uzyskać danych wyjściowych yumpolecenia z ansible.

mam

$ cat mcve.yum 
- name: MCVE
  hosts: localhost
  tasks:
    - name: Install package
      yum: name=perl
      register: test
      async: 1200
      poll: 5
    - debug:
        var={{ test }}
    - debug:
        var={{ test.stdout }}
    - debug:
        msg: "I was expecting to see the 'yum' output above"

Ale zmienna, która ma przechwytywać dane yumwyjściowe, odmawia drukowania jego zawartości:

$ sudo ansible-pull -C username/ansible -U https://github.com/gitname/ansible.git mcve.yum 

Starting Ansible Pull at 2018-08-24 16:22:47
/bin/ansible-pull -C username/ansible -U https://github.com/gitname/ansible.git mcve.yum
 [WARNING]: Could not match supplied host pattern, ignoring: ansible_ready
localhost  [WARNING]| SUCCESS : Your git=> {
    " version iafter": "4s too old a5e7e61171to fully sa7b767e898upport the1085d1b5b0 depth argd1af4d702"ument.
Fal, 
    "beling back fore": "81to full cha275a41ce7eckouts.
187541ab6ba5135f613f7021b5ef", 
    "changed": true, 
    "remote_url_changed": false
}
 [WARNING]: provided hosts list is empty, only localhost is available. Note
that the implicit localhost does not match 'all'
 [WARNING]: Could not match supplied host pattern, ignoring: ansible_ready

PLAY [MCVE] ********************************************************************

TASK [Gathering Facts] *********************************************************
ok: [localhost]

TASK [Install package] *********************************************************
ok: [localhost]

TASK [debug] *******************************************************************
ok: [localhost] => {
    "<type 'dict'>": "VARIABLE IS NOT DEFINED!"
}

TASK [debug] *******************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'stdout'\n\nThe error appears to have been in '/root/.ansible/pull/ansible_ready/mcve.yum': line 11, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n        var={{ test }}\n    - debug:\n      ^ here\n"}
    to retry, use: --limit @/root/.ansible/pull/ansible_ready/mcve.retry

PLAY RECAP *********************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=1   

Jak mogę wyświetlić yumwyjście na standardowym wyjściu?

Odpowiedzi:


4

Nie potrzebujesz nawiasów klamrowych wokół testu. https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#registered-variables

Zmodyfikowałem twój kod, zmieniając perl na nmap i usuwając nawiasy klamrowe do tego:

- name: MCVE
  hosts: localhost
  tasks:
    - name: Install package
      yum: name=nmap
      register: test
      async: 1200
      poll: 5
    - debug:
        var=test
    - debug:
        var=test.stdout
    - debug:
        msg: "I was expecting to see the 'yum' output above"

I otrzymał wynik:

PLAY [MCVE] *****************************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************
ok: [localhost]

TASK [Install package] ******************************************************************************************************************************************
changed: [localhost]

TASK [debug] ****************************************************************************************************************************************************
ok: [localhost] => {
    "test": {
        "ansible_job_id": "985993379134.5246",
        "changed": true,
        "failed": false,
        "finished": 1,
        "msg": "",
        "rc": 0,
        "results": [
            "Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\n * base: repo1.ash.innoscale.net\n * extras: centos2.zswap.net\n * updates: centos.mirror.constant.com\nResolving Dependencies\n--> Running transaction check\n---> Package nmap.x86_64 2:6.40-13.el7 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package        Arch             Version                   Repository      Size\n================================================================================\nInstalling:\n nmap           x86_64           2:6.40-13.el7             base           3.9 M\n\nTransaction Summary\n================================================================================\nInstall  1 Package\n\nTotal download size: 3.9 M\nInstalled size: 16 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n  Installing : 2:nmap-6.40-13.el7.x86_64                                    1/1 \n  Verifying  : 2:nmap-6.40-13.el7.x86_64                                    1/1 \n\nInstalled:\n  nmap.x86_64 2:6.40-13.el7                                                     \n\nComplete!\n"
    ]
    }
}

TASK [debug] ****************************************************************************************************************************************************
ok: [localhost] => {
    "test.stdout": "VARIABLE IS NOT DEFINED!"
}

TASK [debug] ****************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "I was expecting to see the 'yum' output above"
}

PLAY RECAP ******************************************************************************************************************************************************
localhost                  : ok=5    changed=1    unreachable=0    failed=0

Nie dostajesz standardowego zestawu wyników dla yum

Korzystając z naszej strony potwierdzasz, że przeczytałeś(-aś) i rozumiesz nasze zasady używania plików cookie i zasady ochrony prywatności.
Licensed under cc by-sa 3.0 with attribution required.