Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible to use Ansible variables in deployments? #1750

Closed
mikkeschiren opened this issue Jul 30, 2019 · 4 comments
Closed

Possible to use Ansible variables in deployments? #1750

mikkeschiren opened this issue Jul 30, 2019 · 4 comments
Assignees
Labels
language/ansible Issue is related to an Ansible operator project

Comments

@mikkeschiren
Copy link

mikkeschiren commented Jul 30, 2019

I have a deployment which adds a cronjob to a deployment, and I want that cronjob to use variables from ansible - is that possible? Or could I in another way get variables from an operator?

Here is an example what I would like to do:

apiVersion: mthing.com/v1alpha1
kind: Foo
metadata:
  name: foo-com
  version: v0.1
  labels:
    app: web
---
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: '{{ meta.name }}-cron' // This should be meta.name from  mthing.com/v1alpha1
spec:
  schedule: "*/4 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: '{{ meta.name }}-cron'
            image: gcr.io/maynard-io-public/kubernetes-s3-mysql-backup
            imagePullPolicy: Always
            env:
              - name: MYVAR
                value: name: '{{ meta.name }}-var'

When trying this, the operator errors out and complains about '{{ meta.name }} - which it thinks is a string.

@gearoidibm
Copy link

Same as I'm seeing in #1701 ?

@jmrodri jmrodri added the language/ansible Issue is related to an Ansible operator project label Aug 2, 2019
@fabianvf
Copy link
Member

fabianvf commented Aug 2, 2019

Yes, any field in the spec of the custom resource being reconciled will be converted from camel to snake case and passed in to Ansible as an extra_var, and the meta field with name and namespace is passed in as well.

It looks like the sample you posted has a yaml formatting issue though,

value: name: '{{ meta.name }}-var'

Should be

value: '{{ meta.name }}-var'``

Additionally, make sure you are running the Ansible templating engine when reading from that file. The most common pattern is to use the template lookup plugin and pass it into the k8s module, like so:

- k8s:
    definition: "{{ lookup('template', 'cronjob.yaml.j2') }}"

@fabianvf fabianvf self-assigned this Aug 2, 2019
@mikkeschiren
Copy link
Author

mikkeschiren commented Aug 3, 2019

Ah - I see! This really opens up for possibilities

Ok, I tried it like this (I know this will not work because of mix of kubernetes syntax and ansible, but how should you do it?)
(file: foo.yaml)

apiVersion: mthing.com/v1alpha1
kind: Foo
metadata:
  name: foo-com
  version: v0.1
  labels:
    app: web

---
- name: Cron
  k8s:
    definition: "{{ lookup('template', 'cron.yaml.j2') }}"

But when I am try to deploy the file:

kubectl deploy -f foo.yaml

I am getting

error: error validating "foo.yaml": error validating data: invalid object to validate

@flickerfly
Copy link
Contributor

So I have a task (in roles/$name/tasks/main.yml) that looks like this:

- name: "Create {{ meta.name[0]|upper}}{{meta.name[1:] }} Service"
  k8s:
    definition: "{{ lookup('template', 'templates/service.yml.j2') }}"

and the template (in roles/$name/templates/services.yml.j2) looks like this:

apiVersion: v1
kind: Service
metadata:
  labels:
    app: '{{ meta.name }}'
  name: '{{ meta.name }}'
  namespace: '{{ meta.namespace }}'
spec:
  ports:
    - name: '{{ meta.name }}-http'
      port: '{{ httpport }}'
      protocol: TCP
      targetPort: '{{ httpport }}'
  selector:
    app: '{{ meta.name }}'
  sessionAffinity: None
  type: ClusterIP

The CR that it uses to gather the variables looks something like this and it added with kubectl create -f thisfile.yaml assuming the CRD has already been created. :

apiVersion: me.com/v1alpha1
kind: mykind
metadata:
  name: example-mykind
  namespace: mynamespace
spec:
  # Add fields here
  httpport: 8080

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
language/ansible Issue is related to an Ansible operator project
Projects
None yet
Development

No branches or pull requests

5 participants