local_action: shell error concatenate files

前端 未结 1 1323
挽巷
挽巷 2020-12-12 07:23

There is such an error in my playbook. Why and how to fix it? (getting the list of updates of remote-hosts, concatenate lists into one file)

- name: Save upd         


        
相关标签:
1条回答
  • 2020-12-12 07:43

    You use a glob ~/tmp/* and one of the files is ~/tmp/list_update, so effectively you order it to read and write to the same file with a redirection and system is refusing to do that.

    You also didn't specify run_once, so the merge files from different hosts in once task will run as there are hosts defined for the play.

    If you are sure ~/tmp/list_update didn't exist beforehand, adding run_once: true to that task would resolve the problem, but it's far from the best way.


    Long story short, you can achieve what you want in one task, for example

    • with a Jinja2 template:

      - copy:
          content: "{% for host in ansible_play_hosts %}{{ hostvars[host].update_deb_packs.stdout }}\n{% endfor %}"
          dest: ~/tmp/list_update
        delegate_to: localhost
        run_once: true
      

      You can add separators with host-value to indicate what data is coming from which host:

      content: "{% for host in ansible_play_hosts %}From host: {{ host }}\n{{ hostvars[host].update_deb_packs.stdout }}\n{% endfor %}"
      
    • using operations on lists:

      content: "{{ ansible_play_hosts | map('extract', hostvars, 'update_deb_packs') | map(attribute='stdout') | list }}"
      
    0 讨论(0)
提交回复
热议问题