Priya
Priya

Reputation: 303

Ansible template giving error "Could not find or access template file "

I am building simple ansible script. I am using template module. My ansible playbook looks like:

- hosts: "{{ deployment_environment }}"
  remote_user: "{{ user }}"
  gather_facts: no
  
  vars_files:
    - params.yml
  vars:
    artifactory_url: "url"
    artifact_name: "filebeat"
    release_url: "{{ artifactory_url }}/{{ artifact_name }}/{{ filebeat_version }}"
    logstash_url: "{{ logstash_url }}"
    log_path: "{{ logpath.split(',') }}"

  tasks:
    - set_fact:
        env_param: "{{ deployment_environment }}"

    
    - name: Download filebeat.j2 file from artifactory
      uri:
        url: "{{ release_url }}/filebeat.j2"
        method: GET
        validate_certs: no
        force_basic_auth: true
        return_content: no
        force: no
        user: "{{ arti_username }}"
        password: "{{ arti_pass }}"
        dest: "{{ env_select[env_param].deployment_path }}/filebeat"
        creates: "{{ env_select[env_param].deployment_path }}/filebeat/filebeat.j2"
        
   
    - name: Creates directory
      file:
        path: "{{ env_select[env_param].deployment_path }}/filebeat/templates"
        state: directory
   
    - name: Move filebeat.j2 file.
      copy: 
        remote_src: True 
        src: "{{ env_select[env_param].deployment_path }}/filebeat/filebeat.j2" 
        dest: "{{ env_select[env_param].deployment_path }}/filebeat/templates"
        mode: '0777'
    
    - name: create filebeat.yml file using template
      template:
        src: "{{ env_select[env_param].deployment_path }}/filebeat/templates/filebeat.j2" 
        dest: "{{ env_select[env_param].deployment_path }}/filebeat/filebeat-{{ filebeat_semver }}-linux-x86_64/filebeat.yml"
        mode: '0644'
        

When I am running this playbook, I am getting error:

TASK [create filebeat.yml file using template.] *********************************************** FAILED! =>
{
    "changed": false,
    "msg": "Could not find or access '/repo/filebeat/templates/filebeat.j2' on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote_src option"
}

I even tried using remote_src: yes, but its giving same error. What can I do?

Upvotes: 2

Views: 4941

Answers (1)

Frenchy
Frenchy

Reputation: 17027

you cant access to the node manager, so the only solution i see is to render a template from variable with a customfilter, that depends how is the content of your file.j2

1) use module slurp to fetch the content of j2 file in memory of node manager
2) render the template with a customfilter (must know the content of your file.j2)
3) copy the result to the destination file

But there is a question, how you could create and launch playbook without to have access at node manager?

Upvotes: 1

Related Questions