Ansible: não é possível obter o conteúdo de uma variável

1

Parece que não consigo obter a saída de um comando yum de ansible.

Eu tenho

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

Mas, a variável que deveria capturar a saída yum , se recusa a imprimir seu conteúdo:

$ 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   

Como posso exibir a saída yum no stdout?

    
por boardrider 25.08.2018 / 01:34

1 resposta

3

Você não precisa das chaves ao redor do teste. link

Eu modifiquei o seu código, alterei o perl para o nmap e removi chaves para isso:

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

E recebeu a saída:

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

Você não obtém stdout no conjunto de resultados para yum

    
por 25.08.2018 / 03:31