Ansible: Store command's stdout in new variable?

前端 未结 6 647
暗喜
暗喜 2020-12-22 22:35

Inside my playbook I\'d like to create a variable holding the output of an external command. Afterwards I want to make use of that variable in a couple of templates.

6条回答
  •  执笔经年
    2020-12-22 23:10

    I'm a newbie in Ansible, but I would suggest next solution:

    playbook.yml

    ...
    vars:
      command_output_full:
        stdout: will be overriden below
      command_output: {{ command_output_full.stdout }}
    ...
    ...
    ...
    tasks:
      - name: Create variable from command
        command: "echo Hello"
        register: command_output_full
      - debug: msg="{{ command_output }}"
    

    It should work (and works for me) because Ansible uses lazy evaluation. But it seems it checks validity before the launch, so I have to define command_output_full.stdout in vars.

    And, of course, if it is too many such vars in vars section, it will look ugly.

提交回复
热议问题