63

I am writing an ansible playbook right now that deploys a dockerized application in kubernetes. However, for molecularity purposes I would rather not hard code the files that need to be apply after doing kompose convert -f docker-compose.yaml --volumes hostPath Is there a way to apply all the files in a directory?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
James Ukilin
  • 851
  • 2
  • 12
  • 17

4 Answers4

140

You can apply all files in a folder with

kubectl apply -f <folder> 

You may also be interested in parameterization of your manifest files using Kustomize e.g. use more replicas in a prod-namespace than in a test-namespace. You can apply parameterized manifest files with

kubectl apply -k <folder>
Jonas
  • 121,568
  • 97
  • 310
  • 388
19

The above command to give the directory as an option to "-f" works perfectly if you want to apply all the files in the given directory.

The following should apply the yaml files in the current directory matching the given criteria (e.g. here all yamls starting with test)

kubectl apply $(ls test*.yaml | awk ' { print " -f " $1 } ')
pr-pal
  • 3,248
  • 26
  • 18
8

This worked for me >> kubectl apply -f .

6

An ansible-specific answer, using the ansible k8s module, would be:

      - name: Apply all manifests in a given folder
        k8s:
          state: present
          definition: "{{ lookup('template', '{{ item }}') }}"
          namespace: default
        with_fileglob:
          - "manifests/*.yaml"
          - "manifests/*.j2"

This would also allow you to template your manifests and parse them on-the-fly when applying the whole folder.

Please note that this ansible task expects that the manifests folder is in the same path as the running playbook is.

iomv
  • 2,409
  • 18
  • 28