Template File Transfer

20 views
Skip to first unread message

Ian Burrows

unread,
Nov 19, 2018, 3:25:47 AM11/19/18
to Ansible Project
Hi, I am very new to Ansible and I have a question regarding the template module. 

I am receiving an error running a playbook where ansible cannot find or access the j2 file. 

If I transfer the file manually into /tmp/kitchen/ where it says it is looking, then it runs fine. I am using Test Kitchen with kitchen-ansible, Vagrant and Inspec on Windows. Is the template file not supposed to be sent to the host with kitchen? Maybe this is a Test Kitchen issue of not generating the files on the target?

Thank you for any guidance.

J Hawkesworth

unread,
Nov 19, 2018, 3:50:57 AM11/19/18
to Ansible Project
Hard to guess what's going on here without seeing your playbook and template file, but my guess would be the template (.j2) file isn't in one of the places that ansible expects to find it.

If your template is in a role (best place for it) then there are some rules about where ansible will look for templates (and other files) described here:

I can't speak for 'kitchen' as I haven't used for it, but the `template` module should copy (templated) files to the destination that is specified - but if it can't find the template then it wont of course.

Hope this helps,
Jon

Ian Burrows

unread,
Nov 19, 2018, 5:58:47 AM11/19/18
to Ansible Project
Thanks, my directory structure is as follows:

/
.kitchen.yml
roles/common/
--/tasks/playbook.yml
--/templates/create_motd.j2
test/

//.kitchen.yml
---
  driver:
    name: vagrant

  provisioner:
    name: ansible_playbook
    hosts: all
    require_ansible_repo: true
    require_chef_for_busser: false
    require_ruby_for_busser: false
    ansible_verbose: true
    ansible_verbosity: 3
    playbook: ../cloud_ansible/roles/common/tasks/playbook.yml
    roles_path: ../cloud_ansible/roles/common/

  platforms:
    - name: ubuntu-16.04

  verifier:
    name: inspec

  suites:
    - name: cloud_computing

//roles/common/tasks/playbook.yml

- name: Add configurations for Ubuntu
  hosts: all
  roles:
    - common
  vars:
    v1: 'test'
    group: ['sname1','sname2','sname3']
    v2: 'test2'
    change_me: 'change me'
    
  tasks:
    - name: Create Template for MOTD
      template:
        src: create_motd.j2
        dest: /etc/motd

//roles/common/templates/create_motd.j2

{{v1}}
{% for item in group[ %}
{{item}}
{% endfor %}
{{v2}}
Last tool-run was: {{ansible_date_time.date}}
{{change_me}}

J Hawkesworth

unread,
Nov 19, 2018, 9:00:05 AM11/19/18
to Ansible Project
Hmm, as I say I don't know what kitchen expects, however it strikes me as unusual to have your playbook stored in the tasks folder of your role.  Generally playbooks list one or more roles that should be applied to the hosts in the current play, so it seems odd to have the playbook stored in with the role.

Can you try just using 'bare' ansible without it being wrapped in kitchen?

Might be worth running ansible-playbook with -vvvvv to get debugging information including the paths where it is looking for things.

Hope this helps,

Jon
Reply all
Reply to author
Forward
0 new messages