[file] Ansible의 파일에 변수 쓰기

URI 모듈을 통해 JSON을 가져오고 수신 된 콘텐츠를 파일에 쓰고 싶습니다. 콘텐츠를 가져 와서 디버거에 출력 할 수 있으므로 콘텐츠가 수신되었음을 알 수 있지만 파일 작성에 대한 모범 사례를 모릅니다.



답변

매개 변수 copy와 함께 모듈을 사용할 수 있습니다 content.

- copy: content="{{ your_json_feed }}" dest=/path/to/destination/file

여기 문서 : 복사 모듈


답변

아주 작은 파일을 작성하지 않는 한 템플릿 을 사용해야합니다 .

예:

- name: copy upstart script
  template:
    src: myCompany-service.conf.j2
    dest: "/etc/init/myCompany-service.conf"


답변

Ramon의 답변에 따라 오류가 발생합니다. JSON의 공백이 작성하려고 시도한 문제는 플레이 북의 작업을 다음과 같이 변경하여 수정했습니다.

- copy:
    content: "{{ your_json_feed }}"
    dest: "/path/to/destination/file"

현재로서는 이것이 왜 필요한지 잘 모르겠습니다. 내 가장 좋은 추측은 Ansible에서 변수가 대체되고 결과 파일이 구문 분석되는 방식과 관련이 있다는 것입니다.


답변

dest이제 옵션으로 대상 파일을 직접 지정할 수 있습니다 . 아래 예에서 출력 json은/tmp/repo_version_file

- name: Get repository file repo_version model to set ambari_managed_repositories=false
  uri:
    url: 'http://<server IP>:8080/api/v1/stacks/HDP/versions/3.1/repository_versions/1?fields=operating_systems/*'
    method: GET
    force_basic_auth: yes
    user: xxxxx
    password: xxxxx
    headers:
      "X-Requested-By": "ambari"
      "Content-type": "Application/json"
    status_code: 200
    dest: /tmp/repo_version_file


답변