Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

ansible - Where should I be organizing host-specific files/templates?

I'm trying to organize my playbooks according to the Directory Layout structure. The documentation doesn't seem to have a recommendation for host-specific files/templates.

I have 2 plays for a single site

  • example.com-provision.yml
  • example.com-deploy.yml

These files are located in the root of my structure. The provisioning playbook simply includes other roles

---
- hosts: example.com

  roles:
    - common
    - application
    - database

  become: true
  become_method: su
  become_user: root

The deployment playbook doesn't include roles, but has it's own vars and tasks sections. I have a couple template and copy tasks, and am wondering what the 'best practice' is for where to put these host-specific templates/files within this directory structure.

Right now I have them at ./roles/example.com/templates/ and ./roles/example.com/files/, but need to reference the files with their full path from my deployment playbook, like

- name: deployment | copy httpd config
  template:
    src: ./roles/example.com/templates/{{ host }}.conf.j2
    # ...

instead of

- name: deployment | copy httpd config
  template:
    src: {{ host }}.conf.j2
    # ...
question from:https://stackoverflow.com/questions/32830428/where-should-i-be-organizing-host-specific-files-templates

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Facing the same problem the cleanest way seems for me the following structure:

In the top-level directory (same level as playbooks) I have a files folder (and if I needed also a templates folder). In the files folder there is a folder for every host with it's own files where the folder's name is the same as the host name in inventory. (see the structure below: myhost1 myhost2)

.
├── files
│?? ├── common
│?? ├── myhost1
│   ├── myhost2
|
├── inventory
│?? ├── group_vars
│?? └── host_vars
├── roles
│?? ├── first_role
│?? └── second_role
└── my_playbook.yml

Now in any role you can access the files with files modules relatively:

# ./roles/first_role/main.yml

- name: Copy any host based file
  copy:
    src={{ inventory_hostname }}/file1
    dest= /tmp

Explanation:

The magic variable inventory_hostname is to get the host, see here The any file module (as for example copy) looks up the files directory in the respective role directory and the files directory in the same level as the calling playbook. Of course same applies to templates (but if you have different templates for the same role you should reconsider your design)

Semantically a host specific file does not belong into a role, but somewhere outside (like host_vars).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...