Passing a Variable in json Object

Priscilla Muriithi May 23, 2019

I need to create a Jira ticket using rest API. Is there a way to pass in a date variable to a custom due date field?

 

 "customfield_10111": { "value": "{{ cr_due_date }}" }, doesn't seem to work.

tasks:

- name: Ansible date fact example

command: "date +'%m-%d-%Y' -d '+3 days'"

register: result

- set_fact:

cr_due_date: "{{ result.stdout }}"

 

- name: create pre-approved CR uri: url: "{{ jira_cr_request_api_url }}" method: POST headers: Content-Type: "application/json" body: "{{ lookup('file','test_jira.json') }}" body_format: json user: "{{ jira_username }}" password: "{{ jira_password }}" validate_certs: no return_content: yes force_basic_auth: yes status_code: 201 follow_redirects: false register: jira_issue

{ "fields": { "project": { "key": "CR" }, "summary": "testing jira ticket automation", "description": "Creating of an issue using project keys and issue type names using the REST API", "issuetype": { "id": "10301" }, "customfield_13300": [{"value" : "AWS"}], "customfield_10111": { "value": "{{ cr_due_date }}" }, "customfield_13303": { "value": "Ops" }, "customfield_13901": {"value": "OPS" , "child": {"id": "13308"}} }}

2 answers

0 votes
Deepak Gupta December 2, 2021

Not sure if my situation below can help you, but I think its worth a try.

 

I wanted to define a variable and then use it in the jql search query while running this code via Jenkins build step. I was able to do this the following way. I have used two variables OlderThan and jira_sql. The JSON output will be recorded in file OpenTickets.txt

You may want to take some hints from here and adapt to your situation.

OlderThan=30

jira_jql="Project = PROJECTNAME AND status not in (closed, resolved, done) AND created < -${OlderThan}d ORDER BY created ASC"


if [[ ! -z "${jira_jql}" ]]
then
curl --location --request POST "https://jira.xyz.com/rest/api/2/search?pretty=true" \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic ZXRhbzpFdGFvQDEyMw==' \
--data '{
"jql": "'"${jira_jql}"'",
"startAt":0,
"maxResults":1000,
"fields":["key","issuetype","summary","priority","status","created","updated","assignee"]
}' > /tmp/OpenTickets.txt

PS: Posted my solution here so that may be it can help someone else like me.

0 votes
Kmjira August 25, 2020

Hi , Did you get the answer for this Question , If yes then kindly share the format .

Thanks

Suggest an answer

Log in or Sign up to answer