When I've been tying Numeric values from a variable file, they've been resolved as string not numbers.
anuragtamrakar opened this issue · 2 comments
In this playbook I've used k8s module with inline definition of kubernetes resources.
Here I'm providing values to the kubernetes template from variables which are defined in some files.
After running playbook
The full traceback is:
WARNING: The below traceback may not be related to the actual failure.
File "/tmp/ansible_k8s_payload_EpkuYe/ansible_k8s_payload.zip/ansible/module_utils/k8s/raw.py", line 279, in perform_action
k8s_obj = resource.create(definition, namespace=namespace).to_dict()
File "/usr/lib/python2.7/site-packages/openshift/dynamic/client.py", line 79, in inner
raise api_exception(e)
fatal: [localhost]: FAILED! => {
"changed": false,
"error": 400,
"invocation": {
"module_args": {
"api_key": null,
"api_version": "v1",
"append_hash": false,
"ca_cert": null,
"client_cert": null,
"client_key": null,
"context": null,
"force": false,
"host": null,
"kind": null,
"kubeconfig": null,
"merge_type": null,
"name": null,
"namespace": null,
"password": null,
"resource_definition": {
"apiVersion": "apps/v1beta1",
"kind": "Deployment",
"metadata": {
"labels": {
"app": "node-app-player"
},
"name": "node-app-player-deployment",
"namespace": "node-app-dev"
},
"spec": {
"replicas": "4",
"selector": {
"matchLabels": {
"app": "node-app-player"
}
},
"strategy": {
"rollingUpdate": {
"maxSurge": "1",
"maxUnavailable": "25%"
},
"type": "RollingUpdate"
},
"template": {
"metadata": {
"labels": {
"app": "node-app-player"
}
},
"spec": {
"containers": [
{
"env": [
{
"name": "DATABASE_NAME",
"value": "demodb"
},
{
"name": "DATABASE_HOST",
"value": "10.50.8.168"
},
{
"name": "DATABASE_USER",
"value": "demouser"
},
{
"name": "DATABASE_PASSWORD",
"value": "demopassword"
},
{
"name": "APPLICATION_PORT",
"value": "6000"
},
{
"name": "APPLICATION_XXX",
"value": "LZwodd"
},
{
"name": "APPLICATION_YYY",
"value": "d23ojidj23"
},
{
"name": "APPLICATION_ZZZ",
"value": "6eVwefweqf"
},
{
"name": "APPLICATION_JJJ",
"value": "pwlxf3"
},
{
"name": "APPLICATION_MMM",
"value": "wdwqd"
}
],
"image": "nexus.pitneycloud.com:8443/node-app:latest",
"name": "node-app-player-container",
"ports": [
{
"containerPort": "6000"
}
],
"readinessProbe": {
"httpGet": {
"path": "/",
"port": "6000"
},
"initialDelaySeconds": "5",
"periodSeconds": "5",
"successThreshold": "1"
},
"restartPolicy": "OnFailure"
}
],
"imagePullSecrets": [
{
"name": "nexusregcred"
}
]
}
}
}
},
"src": null,
"state": "present",
"username": null,
"validate": null,
"validate_certs": null,
"wait": false,
"wait_condition": null,
"wait_timeout": 120
}
},
"msg": "Failed to create object: {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"Deployment in version \"v1beta1\" cannot be handled as a Deployment: v1beta1.Deployment.Spec: v1beta1.DeploymentSpec.Replicas: readUint32: unexpected character: \ufffd, error found in #10 byte of ...|plicas\": \"4\\n...\\n\",|..., bigger context ...|{\"kind\": \"Deployment\", \"spec\": {\"replicas\": \"4\\n...\\n\", \"strategy\": {\"rollingUpdate\": {\"maxSurg|...","reason":"BadRequest","code":400}\n",
"reason": "Bad Request",
"status": 400
}
PLAY RECAP ******************************************************************************************************
localhost : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
Same playbook I tried to execute with hard-coded numerical values it was successful.
What I've observed is variables having numeric values are also resolved as string and having quotes before and after like "replicas": "5" but It should like "replicas": 5
I also tried to use {{ variables.replica | int }} but It didn't worked
hmm, I'm not totally sure why you're seeing this, but I bet if you moved the definition of the resource into a jinja2 template and used the template lookup (ie, definition: "{{ lookup('template', <template-name>) }}'
), then you could have replicas: {{ variables.replicas | int }}
(no quotes before {{
required because it's inside a template rather than an Ansible yaml file) and it would properly process the value.
I also notice you have the default for maxSurge
set to '1'
, which would trigger this error (should be 1
), which I think would trigger this as well.
Hi fabianvf,
It worked,
Thankyou so much!