xml文件与Ansible中的python脚本之间的交互

问题描述

当前,我有一个剧本,它创建一个.xml文件一个python脚本接收.xml文件并对其进行处理。我打算使用j2模板以 {{ansible_date_time.iso8601_basic_short}}。xml 的形式创建文件,以便跟踪新文件。但是,由于路径在shell命令和python脚本中进行了硬编码,因此该剧本目前确实是静态的。

有什么建议吗?

---
- name: testing
  hosts: localhost
  tasks:
    - name: execute some shell command that produces an xml output file in current dir
      shell: XXX

    - name: Takes in xml output file produced earlier and passes into script.py 
      script: python_script.py

解决方法

如果我理解您的要求,为什么不使用在stdout上打印XML内容的shell脚本,以便您可以将其注册到变量中并在variable.stdout之后使用它? 喜欢:

- name: generate xml
  shell:
    echo '<xml></xml>'
  register: myxml

- name: create xml file
  copy:
    content: "{{ myxml.stdout }}"
    dest: "{{ ansible_date_time.iso8601_basic_short }}.xml"