Remove characters after last occurrence of the character of a JSON key value
I am trying to automate the deployment and invoke lambda function using ansible playbook.To get the arn of the lambda function,I wrote a json query. But here, I need only arn(arn:aws:lambda:us-west-2:1234567890:function:dev-Hello-World) but the Ansible script generates arn with version number (arn:aws:lambda:us-west-2:1234567890:function:dev-Hello-World:50). I used set_fact and json query to get the arn. But, I want to remove the version number and colon at the end.
I tried to use regex and replace to remove the strings. I am new to JSON and programming.
- name: Get the Hello-world arn
set_fact:
populate_arn: "{{ Hello-World | json_query('results[0].configuration.function_arn')}}"
- debug:
var: populate_arn
Expected result:arn:aws:lambda:us-west-2:1234567890:function:dev-Hello-World
Actual result: arn:aws:lambda:us-west-2:1234567890:function:dev-Hello-World:50
ansible yaml json-query
add a comment |
I am trying to automate the deployment and invoke lambda function using ansible playbook.To get the arn of the lambda function,I wrote a json query. But here, I need only arn(arn:aws:lambda:us-west-2:1234567890:function:dev-Hello-World) but the Ansible script generates arn with version number (arn:aws:lambda:us-west-2:1234567890:function:dev-Hello-World:50). I used set_fact and json query to get the arn. But, I want to remove the version number and colon at the end.
I tried to use regex and replace to remove the strings. I am new to JSON and programming.
- name: Get the Hello-world arn
set_fact:
populate_arn: "{{ Hello-World | json_query('results[0].configuration.function_arn')}}"
- debug:
var: populate_arn
Expected result:arn:aws:lambda:us-west-2:1234567890:function:dev-Hello-World
Actual result: arn:aws:lambda:us-west-2:1234567890:function:dev-Hello-World:50
ansible yaml json-query
add a comment |
I am trying to automate the deployment and invoke lambda function using ansible playbook.To get the arn of the lambda function,I wrote a json query. But here, I need only arn(arn:aws:lambda:us-west-2:1234567890:function:dev-Hello-World) but the Ansible script generates arn with version number (arn:aws:lambda:us-west-2:1234567890:function:dev-Hello-World:50). I used set_fact and json query to get the arn. But, I want to remove the version number and colon at the end.
I tried to use regex and replace to remove the strings. I am new to JSON and programming.
- name: Get the Hello-world arn
set_fact:
populate_arn: "{{ Hello-World | json_query('results[0].configuration.function_arn')}}"
- debug:
var: populate_arn
Expected result:arn:aws:lambda:us-west-2:1234567890:function:dev-Hello-World
Actual result: arn:aws:lambda:us-west-2:1234567890:function:dev-Hello-World:50
ansible yaml json-query
I am trying to automate the deployment and invoke lambda function using ansible playbook.To get the arn of the lambda function,I wrote a json query. But here, I need only arn(arn:aws:lambda:us-west-2:1234567890:function:dev-Hello-World) but the Ansible script generates arn with version number (arn:aws:lambda:us-west-2:1234567890:function:dev-Hello-World:50). I used set_fact and json query to get the arn. But, I want to remove the version number and colon at the end.
I tried to use regex and replace to remove the strings. I am new to JSON and programming.
- name: Get the Hello-world arn
set_fact:
populate_arn: "{{ Hello-World | json_query('results[0].configuration.function_arn')}}"
- debug:
var: populate_arn
Expected result:arn:aws:lambda:us-west-2:1234567890:function:dev-Hello-World
Actual result: arn:aws:lambda:us-west-2:1234567890:function:dev-Hello-World:50
ansible yaml json-query
ansible yaml json-query
asked Jan 2 at 22:26


BharatBharat
154
154
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Foremost, as one can see from the Lambda API documentation, that ARN is the correct, stable, identifier for the function. If you remove the :50
qualifier, you will get whatever is the :$LATEST
which can absolutely produce erroneous results
That said, the output from json_query
is just a string, so you can continue your jinja2 pipeline with (as you mentioned) regex_replace
to strip the qualifier:
- set_fact:
populate_arn: "{{ Hello-World | json_query('results[0].configuration.function_arn') | regex_replace(':[^:]+$', '') }}"
The only reason i am removing the version is, I am using this arn to attach it to a cloudwatch event. When i do that the cloudwatch event gets attached the versioned arn instead of latest. So when ever i run a new version of my lambda code, i see multiple entries of lambda targets in the cloudwatch event.
– Bharat
Jan 4 at 19:13
Thanks Matthew, can you explain me the chars you used in the regex?
– Bharat
Jan 4 at 19:56
the:
is the literal character, the[^]
syntax means "the set of all characters not contained within the brackets", and the+
means 1 or more repeats of the characters before it. The$
"anchors" at the end of the string, to keep it from eating any:[^:]+
found within the arn, which would be a ton of them. A little searching for "regex syntax" would have gone a long way, you know
– Matthew L Daniel
Jan 4 at 20:29
Thanks Matthew for the detailed explanation.
– Bharat
Jan 7 at 21:13
Hey Matthew i tried the above regex which you given, it is removing the dev-hello-world. The function name. I checked online, the regex expression is correct. It removes the version number. Is there any help you can do with this thing when running ansible playbook? The above jquery spits the below output. "populate_arn": "arn:aws:lambda:us-west-2:12345678:function"
– Bharat
Jan 11 at 2:04
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54013972%2fremove-characters-after-last-occurrence-of-the-character-of-a-json-key-value%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Foremost, as one can see from the Lambda API documentation, that ARN is the correct, stable, identifier for the function. If you remove the :50
qualifier, you will get whatever is the :$LATEST
which can absolutely produce erroneous results
That said, the output from json_query
is just a string, so you can continue your jinja2 pipeline with (as you mentioned) regex_replace
to strip the qualifier:
- set_fact:
populate_arn: "{{ Hello-World | json_query('results[0].configuration.function_arn') | regex_replace(':[^:]+$', '') }}"
The only reason i am removing the version is, I am using this arn to attach it to a cloudwatch event. When i do that the cloudwatch event gets attached the versioned arn instead of latest. So when ever i run a new version of my lambda code, i see multiple entries of lambda targets in the cloudwatch event.
– Bharat
Jan 4 at 19:13
Thanks Matthew, can you explain me the chars you used in the regex?
– Bharat
Jan 4 at 19:56
the:
is the literal character, the[^]
syntax means "the set of all characters not contained within the brackets", and the+
means 1 or more repeats of the characters before it. The$
"anchors" at the end of the string, to keep it from eating any:[^:]+
found within the arn, which would be a ton of them. A little searching for "regex syntax" would have gone a long way, you know
– Matthew L Daniel
Jan 4 at 20:29
Thanks Matthew for the detailed explanation.
– Bharat
Jan 7 at 21:13
Hey Matthew i tried the above regex which you given, it is removing the dev-hello-world. The function name. I checked online, the regex expression is correct. It removes the version number. Is there any help you can do with this thing when running ansible playbook? The above jquery spits the below output. "populate_arn": "arn:aws:lambda:us-west-2:12345678:function"
– Bharat
Jan 11 at 2:04
|
show 1 more comment
Foremost, as one can see from the Lambda API documentation, that ARN is the correct, stable, identifier for the function. If you remove the :50
qualifier, you will get whatever is the :$LATEST
which can absolutely produce erroneous results
That said, the output from json_query
is just a string, so you can continue your jinja2 pipeline with (as you mentioned) regex_replace
to strip the qualifier:
- set_fact:
populate_arn: "{{ Hello-World | json_query('results[0].configuration.function_arn') | regex_replace(':[^:]+$', '') }}"
The only reason i am removing the version is, I am using this arn to attach it to a cloudwatch event. When i do that the cloudwatch event gets attached the versioned arn instead of latest. So when ever i run a new version of my lambda code, i see multiple entries of lambda targets in the cloudwatch event.
– Bharat
Jan 4 at 19:13
Thanks Matthew, can you explain me the chars you used in the regex?
– Bharat
Jan 4 at 19:56
the:
is the literal character, the[^]
syntax means "the set of all characters not contained within the brackets", and the+
means 1 or more repeats of the characters before it. The$
"anchors" at the end of the string, to keep it from eating any:[^:]+
found within the arn, which would be a ton of them. A little searching for "regex syntax" would have gone a long way, you know
– Matthew L Daniel
Jan 4 at 20:29
Thanks Matthew for the detailed explanation.
– Bharat
Jan 7 at 21:13
Hey Matthew i tried the above regex which you given, it is removing the dev-hello-world. The function name. I checked online, the regex expression is correct. It removes the version number. Is there any help you can do with this thing when running ansible playbook? The above jquery spits the below output. "populate_arn": "arn:aws:lambda:us-west-2:12345678:function"
– Bharat
Jan 11 at 2:04
|
show 1 more comment
Foremost, as one can see from the Lambda API documentation, that ARN is the correct, stable, identifier for the function. If you remove the :50
qualifier, you will get whatever is the :$LATEST
which can absolutely produce erroneous results
That said, the output from json_query
is just a string, so you can continue your jinja2 pipeline with (as you mentioned) regex_replace
to strip the qualifier:
- set_fact:
populate_arn: "{{ Hello-World | json_query('results[0].configuration.function_arn') | regex_replace(':[^:]+$', '') }}"
Foremost, as one can see from the Lambda API documentation, that ARN is the correct, stable, identifier for the function. If you remove the :50
qualifier, you will get whatever is the :$LATEST
which can absolutely produce erroneous results
That said, the output from json_query
is just a string, so you can continue your jinja2 pipeline with (as you mentioned) regex_replace
to strip the qualifier:
- set_fact:
populate_arn: "{{ Hello-World | json_query('results[0].configuration.function_arn') | regex_replace(':[^:]+$', '') }}"
answered Jan 3 at 23:34
Matthew L DanielMatthew L Daniel
9,48612729
9,48612729
The only reason i am removing the version is, I am using this arn to attach it to a cloudwatch event. When i do that the cloudwatch event gets attached the versioned arn instead of latest. So when ever i run a new version of my lambda code, i see multiple entries of lambda targets in the cloudwatch event.
– Bharat
Jan 4 at 19:13
Thanks Matthew, can you explain me the chars you used in the regex?
– Bharat
Jan 4 at 19:56
the:
is the literal character, the[^]
syntax means "the set of all characters not contained within the brackets", and the+
means 1 or more repeats of the characters before it. The$
"anchors" at the end of the string, to keep it from eating any:[^:]+
found within the arn, which would be a ton of them. A little searching for "regex syntax" would have gone a long way, you know
– Matthew L Daniel
Jan 4 at 20:29
Thanks Matthew for the detailed explanation.
– Bharat
Jan 7 at 21:13
Hey Matthew i tried the above regex which you given, it is removing the dev-hello-world. The function name. I checked online, the regex expression is correct. It removes the version number. Is there any help you can do with this thing when running ansible playbook? The above jquery spits the below output. "populate_arn": "arn:aws:lambda:us-west-2:12345678:function"
– Bharat
Jan 11 at 2:04
|
show 1 more comment
The only reason i am removing the version is, I am using this arn to attach it to a cloudwatch event. When i do that the cloudwatch event gets attached the versioned arn instead of latest. So when ever i run a new version of my lambda code, i see multiple entries of lambda targets in the cloudwatch event.
– Bharat
Jan 4 at 19:13
Thanks Matthew, can you explain me the chars you used in the regex?
– Bharat
Jan 4 at 19:56
the:
is the literal character, the[^]
syntax means "the set of all characters not contained within the brackets", and the+
means 1 or more repeats of the characters before it. The$
"anchors" at the end of the string, to keep it from eating any:[^:]+
found within the arn, which would be a ton of them. A little searching for "regex syntax" would have gone a long way, you know
– Matthew L Daniel
Jan 4 at 20:29
Thanks Matthew for the detailed explanation.
– Bharat
Jan 7 at 21:13
Hey Matthew i tried the above regex which you given, it is removing the dev-hello-world. The function name. I checked online, the regex expression is correct. It removes the version number. Is there any help you can do with this thing when running ansible playbook? The above jquery spits the below output. "populate_arn": "arn:aws:lambda:us-west-2:12345678:function"
– Bharat
Jan 11 at 2:04
The only reason i am removing the version is, I am using this arn to attach it to a cloudwatch event. When i do that the cloudwatch event gets attached the versioned arn instead of latest. So when ever i run a new version of my lambda code, i see multiple entries of lambda targets in the cloudwatch event.
– Bharat
Jan 4 at 19:13
The only reason i am removing the version is, I am using this arn to attach it to a cloudwatch event. When i do that the cloudwatch event gets attached the versioned arn instead of latest. So when ever i run a new version of my lambda code, i see multiple entries of lambda targets in the cloudwatch event.
– Bharat
Jan 4 at 19:13
Thanks Matthew, can you explain me the chars you used in the regex?
– Bharat
Jan 4 at 19:56
Thanks Matthew, can you explain me the chars you used in the regex?
– Bharat
Jan 4 at 19:56
the
:
is the literal character, the [^]
syntax means "the set of all characters not contained within the brackets", and the +
means 1 or more repeats of the characters before it. The $
"anchors" at the end of the string, to keep it from eating any :[^:]+
found within the arn, which would be a ton of them. A little searching for "regex syntax" would have gone a long way, you know– Matthew L Daniel
Jan 4 at 20:29
the
:
is the literal character, the [^]
syntax means "the set of all characters not contained within the brackets", and the +
means 1 or more repeats of the characters before it. The $
"anchors" at the end of the string, to keep it from eating any :[^:]+
found within the arn, which would be a ton of them. A little searching for "regex syntax" would have gone a long way, you know– Matthew L Daniel
Jan 4 at 20:29
Thanks Matthew for the detailed explanation.
– Bharat
Jan 7 at 21:13
Thanks Matthew for the detailed explanation.
– Bharat
Jan 7 at 21:13
Hey Matthew i tried the above regex which you given, it is removing the dev-hello-world. The function name. I checked online, the regex expression is correct. It removes the version number. Is there any help you can do with this thing when running ansible playbook? The above jquery spits the below output. "populate_arn": "arn:aws:lambda:us-west-2:12345678:function"
– Bharat
Jan 11 at 2:04
Hey Matthew i tried the above regex which you given, it is removing the dev-hello-world. The function name. I checked online, the regex expression is correct. It removes the version number. Is there any help you can do with this thing when running ansible playbook? The above jquery spits the below output. "populate_arn": "arn:aws:lambda:us-west-2:12345678:function"
– Bharat
Jan 11 at 2:04
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54013972%2fremove-characters-after-last-occurrence-of-the-character-of-a-json-key-value%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown