Related
I want to get the data of tag_name from this link
https://api.github.com/repos/checkstyle/checkstyle/releases
but as we have a lot of releases. How can I get a particular tag_name
I do
LATEST_RELEASE_TAG=$(curl -s https://api.github.com/repos/checkstyle/checkstyle/releases/latest \
| jq ".tag_name")
to get the latest tag_name but how to fetch the second tag_name?
eg:
[ {
"url": "https://api.github.com/repos/checkstyle/checkstyle/releases/73283356",
"assets_url": "https://api.github.com/repos/checkstyle/checkstyle/releases/73283356/assets",
"upload_url": "https://uploads.github.com/repos/checkstyle/checkstyle/releases/73283356/assets{?name,label}",
"html_url": "https://github.com/checkstyle/checkstyle/releases/tag/checkstyle-10.3.2",
"id": 73283356,
"author": {
"login": "romani",
"id": 812984,
"node_id": "MDQ6VXNlcjgxMjk4NA==",
"avatar_url": "https://avatars.githubusercontent.com/u/812984?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/romani",
"html_url": "https://github.com/romani",
"followers_url": "https://api.github.com/users/romani/followers",
"following_url": "https://api.github.com/users/romani/following{/other_user}",
"gists_url": "https://api.github.com/users/romani/gists{/gist_id}",
"starred_url": "https://api.github.com/users/romani/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/romani/subscriptions",
"organizations_url": "https://api.github.com/users/romani/orgs",
"repos_url": "https://api.github.com/users/romani/repos",
"events_url": "https://api.github.com/users/romani/events{/privacy}",
"received_events_url": "https://api.github.com/users/romani/received_events",
"type": "User",
"site_admin": false
},
"node_id": "RE_kwDOAL65M84EXjcc",
"tag_name": "checkstyle-10.3.2",
"target_commitish": "master",
"name": "",
"draft": false,
"prerelease": false,
"created_at": "2022-07-31T13:38:47Z",
"published_at": "2022-07-31T13:54:14Z",
"assets": [
{
"url": "https://api.github.com/repos/checkstyle/checkstyle/releases/assets/73245332",
"id": 73245332,
"node_id": "RA_kwDOAL65M84EXaKU",
"name": "checkstyle-10.3.2-all.jar",
"label": "",
"uploader": {
"login": "romani",
"id": 812984,
"node_id": "MDQ6VXNlcjgxMjk4NA==",
"avatar_url": "https://avatars.githubusercontent.com/u/812984?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/romani",
"html_url": "https://github.com/romani",
"followers_url": "https://api.github.com/users/romani/followers",
"following_url": "https://api.github.com/users/romani/following{/other_user}",
"gists_url": "https://api.github.com/users/romani/gists{/gist_id}",
"starred_url": "https://api.github.com/users/romani/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/romani/subscriptions",
"organizations_url": "https://api.github.com/users/romani/orgs",
"repos_url": "https://api.github.com/users/romani/repos",
"events_url": "https://api.github.com/users/romani/events{/privacy}",
"received_events_url": "https://api.github.com/users/romani/received_events",
"type": "User",
"site_admin": false
},
"content_type": "application/zip",
"state": "uploaded",
"size": 16208747,
"download_count": 505,
"created_at": "2022-07-31T13:54:16Z",
"updated_at": "2022-07-31T13:55:12Z",
"browser_download_url": "https://github.com/checkstyle/checkstyle/releases/download/checkstyle-10.3.2/checkstyle-10.3.2-all.jar"
}
],
"tarball_url": "https://api.github.com/repos/checkstyle/checkstyle/tarball/checkstyle-10.3.2",
"zipball_url": "https://api.github.com/repos/checkstyle/checkstyle/zipball/checkstyle-10.3.2",
"body": "https://checkstyle.org/releasenotes.html#Release_10.3.2\r\n\r\nBug fixes:\r\n\r\n #11736 - MissingJavadocType: Support qualified annotation names\r\n #11655 - Update google_checks.xml to have the SuppressionCommentFilter and SuppressWarningsHolder modules in the config by default (and by extension, SuppressWarningsFilter)\r\n\r\n<details>\r\n<summary>Other Changes:</summary>\r\n<br>\r\n Update releasenotes to use GitHub Pages execution\r\n<br />\r\n Allow SuppressWarningHolder to suppress the violation with NameCheck\r\n<br />\r\n Pitest: Kill all surviving mutations\r\n<br />\r\n Pitest: Activate \"ALL\" mutator group\r\n<br />\r\n Use Shellcheck to resolve violations code in Shell Script\r\n<br />\r\n pitest: increase mutation coverage for pitest-imports profile to 100% \r\n<br />\r\n Update GitHub Action for bump-license-year.sh\r\n<br />\r\n Solve fb-contrib errors\r\n<br />\r\n Remove pitest mutation checking HTML model\r\n<br />\r\n automate execution by Github action bump-license-year.sh on first day of month\r\n<br />\r\n update code base to have javadoc tag to explain noinspection tag content\r\n<br />\r\n doc: put example of enableExternalDtdLoad to xdoc\r\n<br />\r\n update doc for SuppressWarningsHolder\r\n<br />\r\n releasenotes script generated empty commit\r\n<br />\r\n Include CDG Accelerator Plugin to boost pitest performance\r\n<br />\r\n Enforce file size on Java inputs\r\n<br />\r\n Expand XPath IT Regression Testing\r\n<br />\r\n There are semantic problems in the Chinese error message of keyword `design.forExtension` .\r\n<br />\r\n prepare-settings.sh fails to create settings.xml\r\n<br />\r\n Use groovy version provided by `apt` in pitest.yml\r\n<br />\r\n tweet-releasenotes.sh does not check env variables\r\n<br />\r\n download of m2 cache from sourceforge.io is slow \r\n<br />\r\n infra: turns off create dependency reduced pom for shade plugin\r\n<br />\r\n Update Tests to use new 'verifyXxxxxx' method or 'execute' that use inlined config in Input files\r\n<br />\r\n Specify violation messages in input files.\r\n<br />\r\n pitest: increase mutation coverage for javadoc profile to 100%\r\n<br />\r\n</details>",
"reactions": {
"url": "https://api.github.com/repos/checkstyle/checkstyle/releases/73283356/reactions",
"total_count": 10,
"+1": 0,
"-1": 0,
"laugh": 0,
"hooray": 6,
"confused": 0,
"heart": 2,
"rocket": 2,
"eyes": 0
} }, {
"url": "https://api.github.com/repos/checkstyle/checkstyle/releases/70647006",
"assets_url": "https://api.github.com/repos/checkstyle/checkstyle/releases/70647006/assets",
"upload_url": "https://uploads.github.com/repos/checkstyle/checkstyle/releases/70647006/assets{?name,label}",
"html_url": "https://github.com/checkstyle/checkstyle/releases/tag/checkstyle-10.3.1",
"id": 70647006,
"author": {
"login": "romani",
"id": 812984,
"node_id": "MDQ6VXNlcjgxMjk4NA==",
"avatar_url": "https://avatars.githubusercontent.com/u/812984?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/romani",
"html_url": "https://github.com/romani",
"followers_url": "https://api.github.com/users/romani/followers",
"following_url": "https://api.github.com/users/romani/following{/other_user}",
"gists_url": "https://api.github.com/users/romani/gists{/gist_id}",
"starred_url": "https://api.github.com/users/romani/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/romani/subscriptions",
"organizations_url": "https://api.github.com/users/romani/orgs",
"repos_url": "https://api.github.com/users/romani/repos",
"events_url": "https://api.github.com/users/romani/events{/privacy}",
"received_events_url": "https://api.github.com/users/romani/received_events",
"type": "User",
"site_admin": false
},
"node_id": "RE_kwDOAL65M84ENfze",
"tag_name": "checkstyle-10.3.1",
"target_commitish": "master",
"name": "",
"draft": false,
"prerelease": false,
"created_at": "2022-06-27T13:50:30Z",
"published_at": "2022-06-27T14:07:12Z",
"assets": [
{
"url": "https://api.github.com/repos/checkstyle/checkstyle/releases/assets/69790751",
"id": 69790751,
"node_id": "RA_kwDOAL65M84EKOwf",
"name": "checkstyle-10.3.1-all.jar",
"label": "",
"uploader": {
"login": "romani",
"id": 812984,
"node_id": "MDQ6VXNlcjgxMjk4NA==",
"avatar_url": "https://avatars.githubusercontent.com/u/812984?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/romani",
"html_url": "https://github.com/romani",
"followers_url": "https://api.github.com/users/romani/followers",
"following_url": "https://api.github.com/users/romani/following{/other_user}",
"gists_url": "https://api.github.com/users/romani/gists{/gist_id}",
"starred_url": "https://api.github.com/users/romani/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/romani/subscriptions",
"organizations_url": "https://api.github.com/users/romani/orgs",
"repos_url": "https://api.github.com/users/romani/repos",
"events_url": "https://api.github.com/users/romani/events{/privacy}",
"received_events_url": "https://api.github.com/users/romani/received_events",
"type": "User",
"site_admin": false
},
"content_type": "application/zip",
"state": "uploaded",
"size": 15396315,
"download_count": 3577,
"created_at": "2022-06-27T14:07:13Z",
"updated_at": "2022-06-27T14:08:06Z",
"browser_download_url": "https://github.com/checkstyle/checkstyle/releases/download/checkstyle-10.3.1/checkstyle-10.3.1-all.jar"
}
],
"tarball_url": "https://api.github.com/repos/checkstyle/checkstyle/tarball/checkstyle-10.3.1",
"zipball_url": "https://api.github.com/repos/checkstyle/checkstyle/zipball/checkstyle-10.3.1",
"body": "https://checkstyle.org/releasenotes.html#Release_10.3.1",
"reactions": {
"url": "https://api.github.com/repos/checkstyle/checkstyle/releases/70647006/reactions",
"total_count": 8,
"+1": 0,
"-1": 0,
"laugh": 0,
"hooray": 7,
"confused": 0,
"heart": 1,
"rocket": 0,
"eyes": 0
} },
This has 2 tag_name and I want to fetch the data of the second tag_name. Now It's like I always want the 2nd number tag_name only.
-> I want to use this tag_name as the previous release so I want the latest tag_name as the latest tag and the 2nd tag "tag_name" as the previous tag. I want to use these in a shell script.
Fetching the Two Most Recent Release
You can have the Github releases API respond with the two most recent releases, and then use jq to access all .tag_name fields in the array objects and then read them into separate bash variables, like so:
#!/bin/bash
# You only care about the latest release and previous, so you can
# pass the per_page query parameter
github_rsp="$(curl -s https://api.github.com/repos/checkstyle/checkstyle/releases?per_page=2)"
# jq lets you access multiple objects in a list
# using different syntaxes. Here, ".[].tag_name" will just
# access every tag_name field in each object of the response array
# and then we put those into an array that we then join with ";'
# We then read jq's output into two separate shell variables
# using ";' as our separator
IFS=";" read latest previous < <(jq -r '[.[].tag_name] | join(";")' <<< $github_rsp)
echo " Latest version: $latest"
echo "Previous version: $previous"
With this as output
❯ ./parse.sh
Latest version: checkstyle-10.3.2
Previous version: checkstyle-10.3.1
Also see the following for even more info about how my script works, if you need explanation beyond my notes:
jq Object Value Iterator
Github REST API - List Releases
Fetching a Single Release
Since you asked about getting any arbitrary release, if you have a tag name, you can use the Github Release-by-Tag API, where you put a specific tag into the releases API URL.
#!/bin/bash
function get_release() {
local repo="$1"
local tag="$2"
local release_url="https://api.github.com/repos/${repo}/releases/tags/${tag}"
rsp=$(curl -s -H 'Accept: application/vnd.github+json' "$release_url")
echo "$rsp"
}
get_release "checkstyle/checkstyle" "checkstyle-10.1" | jq
And running this script I get...
❯ ./single-release.sh
{
"url": "https://api.github.com/repos/checkstyle/checkstyle/releases/62869604",
"assets_url": "https://api.github.com/repos/checkstyle/checkstyle/releases/62869604/assets",
"upload_url": "https://uploads.github.com/repos/checkstyle/checkstyle/releases/62869604/assets{?name,label}",
"html_url": "https://github.com/checkstyle/checkstyle/releases/tag/checkstyle-10.1",
"id": 62869604,
"author": {
"login": "romani",
"id": 812984,
"node_id": "MDQ6VXNlcjgxMjk4NA==",
"avatar_url": "https://avatars.githubusercontent.com/u/812984?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/romani",
"html_url": "https://github.com/romani",
"followers_url": "https://api.github.com/users/romani/followers",
"following_url": "https://api.github.com/users/romani/following{/other_user}",
"gists_url": "https://api.github.com/users/romani/gists{/gist_id}",
"starred_url": "https://api.github.com/users/romani/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/romani/subscriptions",
"organizations_url": "https://api.github.com/users/romani/orgs",
"repos_url": "https://api.github.com/users/romani/repos",
"events_url": "https://api.github.com/users/romani/events{/privacy}",
"received_events_url": "https://api.github.com/users/romani/received_events",
"type": "User",
"site_admin": false
},
"node_id": "RE_kwDOAL65M84Dv1Bk",
"tag_name": "checkstyle-10.1",
"target_commitish": "master",
"name": "",
"draft": false,
"prerelease": false,
"created_at": "2022-03-27T14:51:31Z",
"published_at": "2022-03-27T15:06:48Z",
"assets": [
{
"url": "https://api.github.com/repos/checkstyle/checkstyle/releases/assets/60754101",
"id": 60754101,
"node_id": "RA_kwDOAL65M84Dnwi1",
"name": "checkstyle-10.1-all.jar",
"label": "",
"uploader": {
"login": "romani",
"id": 812984,
"node_id": "MDQ6VXNlcjgxMjk4NA==",
"avatar_url": "https://avatars.githubusercontent.com/u/812984?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/romani",
"html_url": "https://github.com/romani",
"followers_url": "https://api.github.com/users/romani/followers",
"following_url": "https://api.github.com/users/romani/following{/other_user}",
"gists_url": "https://api.github.com/users/romani/gists{/gist_id}",
"starred_url": "https://api.github.com/users/romani/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/romani/subscriptions",
"organizations_url": "https://api.github.com/users/romani/orgs",
"repos_url": "https://api.github.com/users/romani/repos",
"events_url": "https://api.github.com/users/romani/events{/privacy}",
"received_events_url": "https://api.github.com/users/romani/received_events",
"type": "User",
"site_admin": false
},
"content_type": "application/zip",
"state": "uploaded",
"size": 13403044,
"download_count": 6227,
"created_at": "2022-03-27T15:06:49Z",
"updated_at": "2022-03-27T15:07:35Z",
"browser_download_url": "https://github.com/checkstyle/checkstyle/releases/download/checkstyle-10.1/checkstyle-10.1-all.jar"
}
],
"tarball_url": "https://api.github.com/repos/checkstyle/checkstyle/tarball/checkstyle-10.1",
"zipball_url": "https://api.github.com/repos/checkstyle/checkstyle/zipball/checkstyle-10.1",
"body": "https://checkstyle.org/releasenotes.html#Release_10.1",
"reactions": {
"url": "https://api.github.com/repos/checkstyle/checkstyle/releases/62869604/reactions",
"total_count": 4,
"+1": 1,
"-1": 0,
"laugh": 0,
"hooray": 3,
"confused": 0,
"heart": 0,
"rocket": 0,
"eyes": 0
}
}
I have an array of items:
[
["20180629", "14:49", "google", "iOS", "Safari", "1"],
["20180629", "12:22", "google", "Android", "Chrome", "2"],
["20180629", "17:20", "google", "iOS", "Safari", "1"],
["20180629", "16:30", "(direct)", "iOS", "Safari", "1"],
["20180629", "09:29", "(direct)", "Android", "Chrome", "2"]
]
I need to remove duplicate sub-arrays that have all the same items, except for the first two (timecodes). I narrowed my code down to this:
exclude_from_filter = [0,1]
array.each_with_index.map do |array, index|
array.reject.with_index { |e,i| exclude_from_filter.include? i }
end.uniq!
Which removes timecodes and then removes duplicate items:
[
["google", "iOS", "Safari", "1"],
["google", "Android", "Chrome", "2"],
["(direct)", "iOS", "Safari", "1"],
["(direct)", "Android", "Chrome", "2"]
]
However instead I would like to get rid of duplicate items, leaving one unique value intact:
[
["20180629", "14:49", "google", "iOS", "Safari", "1"],
["20180629", "12:22", "google", "Android", "Chrome", "2"],
["20180629", "16:30", "(direct)", "iOS", "Safari", "1"],
["20180629", "09:29", "(direct)", "Android", "Chrome", "2"]
]
And this is where I'm completely lost
If you just want to remove the duplicates based on last 4 values, use uniq with a block:
ary.uniq { |e| e[2..5] }
=> [["20180629", "14:49", "google", "iOS", "Safari", "1"],
["20180629", "12:22", "google", "Android", "Chrome", "2"],
["20180629", "16:30", "(direct)", "iOS", "Safari", "1"],
["20180629", "09:29", "(direct)", "Android", "Chrome", "2"]]
Or, to remove them based on all values except first two, do:
ary.uniq { |e| e[2..-1] }
which can also be written as (Sergio's suggestion):
ary.uniq { |date, time, *rest| rest }
I have a list
["usd", "aed", "afn", "all", "amd", "ang", "aoa", "ars", "aud", "awg", "azn", "bam", "bbd", "bdt", "bgn", "bif", "bmd", "bnd", "bob", "brl", "bsd", "bwp", "bzd", "cad", "cdf", "chf", "clp", "cny", "cop", "crc", "cve", "czk", "djf", "dkk", "dop", "dzd", "eek", "egp", "etb", "eur", "fjd", "fkp", "gbp", "gel", "gip", "gmd", "gnf", "gtq", "gyd", "hkd", "hnl", "hrk", "htg", "huf", "idr", "ils", "inr", "isk", "jmd", "jpy", "kes", "kgs", "khr", "kmf", "krw", "kyd", "kzt", "lak", "lbp", "lkr", "lrd", "lsl", "ltl", "lvl", "mad", "mdl", "mga", "mkd", "mnt", "mop", "mro", "mur", "mvr", "mwk", "mxn", "myr", "mzn", "nad", "ngn", "nio", "nok", "npr", "nzd", "pab", "pen", "pgk", "php", "pkr", "pln", "pyg", "qar", "ron", "rsd", "rub", "rwf", "sar", "sbd", "scr", "sek", "sgd", "shp", "sll", "sos", "srd", "std", "svc", "szl", "thb", "tjs", "top", "try", "ttd", "twd", "tzs", "uah", "ugx", "uyu", "uzs", "vnd", "vuv", "wst", "xaf", "xcd", "xof", "xpf", "yer", "zar", "zmw"]
A default_currency, let's say EUR
A last_currency used, let's say CAD
I want my list sorted with this criteria
last currency is used is first
default currency after
USD, EUR,CAD after
and the rest
Is there any way to achieve this ? (One liner if possible)
bolo
Try this:
last_currency = 'cad'
default_currency = 'eur'
primary_currencies = [last_currency, default_currency, 'usd', 'eur', 'cad'].uniq
all_currencies = ["usd", "aed", "afn", "all", "amd", "ang", "aoa", "ars", "aud", "awg", "azn", "bam", "bbd", "bdt", "bgn", "bif", "bmd", "bnd", "bob", "brl", "bsd", "bwp", "bzd", "cad", "cdf", "chf", "clp", "cny", "cop", "crc", "cve", "czk", "djf", "dkk", "dop", "dzd", "eek", "egp", "etb", "eur", "fjd", "fkp", "gbp", "gel", "gip", "gmd", "gnf", "gtq", "gyd", "hkd", "hnl", "hrk", "htg", "huf", "idr", "ils", "inr", "isk", "jmd", "jpy", "kes", "kgs", "khr", "kmf", "krw", "kyd", "kzt", "lak", "lbp", "lkr", "lrd", "lsl", "ltl", "lvl", "mad", "mdl", "mga", "mkd", "mnt", "mop", "mro", "mur", "mvr", "mwk", "mxn", "myr", "mzn", "nad", "ngn", "nio", "nok", "npr", "nzd", "pab", "pen", "pgk", "php", "pkr", "pln", "pyg", "qar", "ron", "rsd", "rub", "rwf", "sar", "sbd", "scr", "sek", "sgd", "shp", "sll", "sos", "srd", "std", "svc", "szl", "thb", "tjs", "top", "try", "ttd", "twd", "tzs", "uah", "ugx", "uyu", "uzs", "vnd", "vuv", "wst", "xaf", "xcd", "xof", "xpf", "yer", "zar", "zmw"]
primary_currencies + (all_currencies - primary_currencies)
Output:
["cad", "eur", "usd", "aed", "afn", "all", "amd", "ang", "aoa", "ars", "aud", "awg", "azn", "bam", "bbd", "bdt", "bgn", "bif", "bmd", "bnd", "bob", "brl", "bsd", "bwp", "bzd", "cdf", "chf", "clp", "cny", "cop", "crc", "cve", "czk", "djf", "dkk", "dop", "dzd", "eek", "egp", "etb", "fjd", "fkp", "gbp", "gel", "gip", "gmd", "gnf", "gtq", "gyd", "hkd", "hnl", "hrk", "htg", "huf", "idr", "ils", "inr", "isk", "jmd", "jpy", "kes", "kgs", "khr", "kmf", "krw", "kyd", "kzt", "lak", "lbp", "lkr", "lrd", "lsl", "ltl", "lvl", "mad", "mdl", "mga", "mkd", "mnt", "mop", "mro", "mur", "mvr", "mwk", "mxn", "myr", "mzn", "nad", "ngn", "nio", "nok", "npr", "nzd", "pab", "pen", "pgk", "php", "pkr", "pln", "pyg", "qar", "ron", "rsd", "rub", "rwf", "sar", "sbd", "scr", "sek", "sgd", "shp", "sll", "sos", "srd", "std", "svc", "szl", "thb", "tjs", "top", "try", "ttd", "twd", "tzs", "uah", "ugx", "uyu", "uzs", "vnd", "vuv", "wst", "xaf", "xcd", "xof", "xpf", "yer", "zar", "zmw"]
It won't rearrange the rest of currencies.
You don't need the custom sort here. Just slap the things together (sort the main array, though, if you want) and uniq will take care of the duplicates.
def sort_but_not_really_sort(default, last, important)
# TODO: pre-sort this
all_currencies = ["usd", "aed", "afn", "all", "amd", "ang", "aoa", "ars", "aud", "awg", "azn", "bam", "bbd", "bdt", "bgn", "bif", "bmd", "bnd", "bob", "brl", "bsd", "bwp", "bzd", "cad", "cdf", "chf", "clp", "cny", "cop", "crc", "cve", "czk", "djf", "dkk", "dop", "dzd", "eek", "egp", "etb", "eur", "fjd", "fkp", "gbp", "gel", "gip", "gmd", "gnf", "gtq", "gyd", "hkd", "hnl", "hrk", "htg", "huf", "idr", "ils", "inr", "isk", "jmd", "jpy", "kes", "kgs", "khr", "kmf", "krw", "kyd", "kzt", "lak", "lbp", "lkr", "lrd", "lsl", "ltl", "lvl", "mad", "mdl", "mga", "mkd", "mnt", "mop", "mro", "mur", "mvr", "mwk", "mxn", "myr", "mzn", "nad", "ngn", "nio", "nok", "npr", "nzd", "pab", "pen", "pgk", "php", "pkr", "pln", "pyg", "qar", "ron", "rsd", "rub", "rwf", "sar", "sbd", "scr", "sek", "sgd", "shp", "sll", "sos", "srd", "std", "svc", "szl", "thb", "tjs", "top", "try", "ttd", "twd", "tzs", "uah", "ugx", "uyu", "uzs", "vnd", "vuv", "wst", "xaf", "xcd", "xof", "xpf", "yer", "zar", "zmw"].sort
[last, default, *important, *all_currencies].uniq
end
default_currency = 'eur'
last_currency = 'cad'
important_currencies = ['usd', 'eur', 'cad']
sort_but_not_really_sort(default_currency, last_currency, important_currencies).take(5)
# => ["cad", "eur", "usd", "aed", "afn"]
Yes, it's possible to achieve this in a one liner.
a = [ 'usd', 'aed', ... 'zmw' ]
a.sort{|a,b| <sorting code goes here> }
sort takes a block, and within that block, a and b are two adacent elements in the list that is being sorted. Compare them, returning -1 if a should appear before b and 1 if b should appear before a, and 0 if they should remain unaffected.
Good luck.
I see that Ansible provide some pre-defined variables that we can use in playbooks and template files. For example, the host IP address is ansible_eth0.ipv4.address. Googleing and searching the docs I couldn't find a list of all available variables.
Would someone list them for me?
From the FAQ:
How do I see a list of all of the ansible_ variables?
Ansible by default gathers “facts” about the machines under management, and these facts can be accessed in playbooks and in templates. To see a list of all of the facts that are available about a machine, you can run the setup module as an ad hoc action:
ansible -m setup hostname
This will print out a dictionary of all of the facts that are available for that particular host. You might want to pipe the output to a pager.This does NOT include inventory variables or internal ‘magic’ variables. See the next question if you need more than just ‘facts’.
Here is the output for my vagrant virtual machine called scdev:
scdev | success >> {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"10.0.2.15",
"192.168.10.10"
],
"ansible_all_ipv6_addresses": [
"fe80::a00:27ff:fe12:9698",
"fe80::a00:27ff:fe74:1330"
],
"ansible_architecture": "i386",
"ansible_bios_date": "12/01/2006",
"ansible_bios_version": "VirtualBox",
"ansible_cmdline": {
"BOOT_IMAGE": "/vmlinuz-3.2.0-23-generic-pae",
"quiet": true,
"ro": true,
"root": "/dev/mapper/precise32-root"
},
"ansible_date_time": {
"date": "2013-09-17",
"day": "17",
"epoch": "1379378304",
"hour": "00",
"iso8601": "2013-09-17T00:38:24Z",
"iso8601_micro": "2013-09-17T00:38:24.425092Z",
"minute": "38",
"month": "09",
"second": "24",
"time": "00:38:24",
"tz": "UTC",
"year": "2013"
},
"ansible_default_ipv4": {
"address": "10.0.2.15",
"alias": "eth0",
"gateway": "10.0.2.2",
"interface": "eth0",
"macaddress": "08:00:27:12:96:98",
"mtu": 1500,
"netmask": "255.255.255.0",
"network": "10.0.2.0",
"type": "ether"
},
"ansible_default_ipv6": {},
"ansible_devices": {
"sda": {
"holders": [],
"host": "SATA controller: Intel Corporation 82801HM/HEM (ICH8M/ICH8M-E) SATA Controller [AHCI mode] (rev 02)",
"model": "VBOX HARDDISK",
"partitions": {
"sda1": {
"sectors": "497664",
"sectorsize": 512,
"size": "243.00 MB",
"start": "2048"
},
"sda2": {
"sectors": "2",
"sectorsize": 512,
"size": "1.00 KB",
"start": "501758"
},
},
"removable": "0",
"rotational": "1",
"scheduler_mode": "cfq",
"sectors": "167772160",
"sectorsize": "512",
"size": "80.00 GB",
"support_discard": "0",
"vendor": "ATA"
},
"sr0": {
"holders": [],
"host": "IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)",
"model": "CD-ROM",
"partitions": {},
"removable": "1",
"rotational": "1",
"scheduler_mode": "cfq",
"sectors": "2097151",
"sectorsize": "512",
"size": "1024.00 MB",
"support_discard": "0",
"vendor": "VBOX"
},
"sr1": {
"holders": [],
"host": "IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)",
"model": "CD-ROM",
"partitions": {},
"removable": "1",
"rotational": "1",
"scheduler_mode": "cfq",
"sectors": "2097151",
"sectorsize": "512",
"size": "1024.00 MB",
"support_discard": "0",
"vendor": "VBOX"
}
},
"ansible_distribution": "Ubuntu",
"ansible_distribution_release": "precise",
"ansible_distribution_version": "12.04",
"ansible_domain": "",
"ansible_eth0": {
"active": true,
"device": "eth0",
"ipv4": {
"address": "10.0.2.15",
"netmask": "255.255.255.0",
"network": "10.0.2.0"
},
"ipv6": [
{
"address": "fe80::a00:27ff:fe12:9698",
"prefix": "64",
"scope": "link"
}
],
"macaddress": "08:00:27:12:96:98",
"module": "e1000",
"mtu": 1500,
"type": "ether"
},
"ansible_eth1": {
"active": true,
"device": "eth1",
"ipv4": {
"address": "192.168.10.10",
"netmask": "255.255.255.0",
"network": "192.168.10.0"
},
"ipv6": [
{
"address": "fe80::a00:27ff:fe74:1330",
"prefix": "64",
"scope": "link"
}
],
"macaddress": "08:00:27:74:13:30",
"module": "e1000",
"mtu": 1500,
"type": "ether"
},
"ansible_form_factor": "Other",
"ansible_fqdn": "scdev",
"ansible_hostname": "scdev",
"ansible_interfaces": [
"lo",
"eth1",
"eth0"
],
"ansible_kernel": "3.2.0-23-generic-pae",
"ansible_lo": {
"active": true,
"device": "lo",
"ipv4": {
"address": "127.0.0.1",
"netmask": "255.0.0.0",
"network": "127.0.0.0"
},
"ipv6": [
{
"address": "::1",
"prefix": "128",
"scope": "host"
}
],
"mtu": 16436,
"type": "loopback"
},
"ansible_lsb": {
"codename": "precise",
"description": "Ubuntu 12.04 LTS",
"id": "Ubuntu",
"major_release": "12",
"release": "12.04"
},
"ansible_machine": "i686",
"ansible_memfree_mb": 23,
"ansible_memtotal_mb": 369,
"ansible_mounts": [
{
"device": "/dev/mapper/precise32-root",
"fstype": "ext4",
"mount": "/",
"options": "rw,errors=remount-ro",
"size_available": 77685088256,
"size_total": 84696281088
},
{
"device": "/dev/sda1",
"fstype": "ext2",
"mount": "/boot",
"options": "rw",
"size_available": 201044992,
"size_total": 238787584
},
{
"device": "/vagrant",
"fstype": "vboxsf",
"mount": "/vagrant",
"options": "uid=1000,gid=1000,rw",
"size_available": 42013151232,
"size_total": 484145360896
}
],
"ansible_os_family": "Debian",
"ansible_pkg_mgr": "apt",
"ansible_processor": [
"Pentium(R) Dual-Core CPU E5300 # 2.60GHz"
],
"ansible_processor_cores": "NA",
"ansible_processor_count": 1,
"ansible_product_name": "VirtualBox",
"ansible_product_serial": "NA",
"ansible_product_uuid": "NA",
"ansible_product_version": "1.2",
"ansible_python_version": "2.7.3",
"ansible_selinux": false,
"ansible_swapfree_mb": 766,
"ansible_swaptotal_mb": 767,
"ansible_system": "Linux",
"ansible_system_vendor": "innotek GmbH",
"ansible_user_id": "neves",
"ansible_userspace_architecture": "i386",
"ansible_userspace_bits": "32",
"ansible_virtualization_role": "guest",
"ansible_virtualization_type": "virtualbox"
},
"changed": false
}
The current documentation now has a complete chapter about Discovering variables: facts and magic variables.
ansible -m setup hostname
Only gets the facts gathered by the setup module.
Gilles Cornu posted a template trick to list all variables for a specific host.
Template (later called dump_variables):
HOSTVARS (ANSIBLE GATHERED, group_vars, host_vars) :
{{ hostvars[inventory_hostname] | to_yaml }}
PLAYBOOK VARS:
{{ vars | to_yaml }}
Playbook to use it:
- hosts: all
tasks:
- template:
src: templates/dump_variables
dest: /tmp/ansible_variables
- fetch:
src: /tmp/ansible_variables
dest: "{{inventory_hostname}}_ansible_variables"
After that you have a dump of all variables on every host, and a copy of each text dump file on your local workstation in your tmp folder. If you don't want local copies, you can remove the fetch statement.
This includes gathered facts, host variables and group variables.
Therefore you see ansible default variables like group_names, inventory_hostname, ansible_ssh_host and so on.
There are 3 sources of variables in Ansible:
Variables gathered from facts. You can get them by running command: ansible -m setup hostname
Built-in (pre-defined) Ansible variables (AKA 'magic' variables). They are documented in Ansible documentation: http://docs.ansible.com/playbooks_variables.html#magic-variables-and-how-to-access-information-about-other-hosts
Here is the list extracted from Ansible 1.9 documentation:
group_names
groups
inventory_hostname
ansible_hostname
inventory_hostname_short
play_hosts
delegate_to
inventory_dir
inventory_file
Variables passed to ansible via command line. But obviously you know what they are
I use this simple playbook:
---
# vars.yml
#
# Shows the value of all variables/facts.
#
# Example:
#
# ansible-playbook vars.yml -e 'hosts=localhost'
#
- hosts: localhost
tasks:
- fail: "You must specify a value for `hosts` variable - e.g.: ansible-playbook vars.yml -e 'hosts=localhost'"
when: hosts is not defined
- hosts: "{{ hosts }}"
tasks:
- debug: var=vars
- debug: var=hostvars[inventory_hostname]
There is lot of variables defined as Facts -- http://docs.ansible.com/ansible/playbooks_variables.html#information-discovered-from-systems-facts
"ansible_all_ipv4_addresses": [
"REDACTED IP ADDRESS"
],
"ansible_all_ipv6_addresses": [
"REDACTED IPV6 ADDRESS"
],
"ansible_architecture": "x86_64",
"ansible_bios_date": "09/20/2012",
"ansible_bios_version": "6.00",
"ansible_cmdline": {
"BOOT_IMAGE": "/boot/vmlinuz-3.5.0-23-generic",
"quiet": true,
"ro": true,
"root": "UUID=4195bff4-e157-4e41-8701-e93f0aec9e22",
"splash": true
},
"ansible_date_time": {
"date": "2013-10-02",
"day": "02",
"epoch": "1380756810",
"hour": "19",
"iso8601": "2013-10-02T23:33:30Z",
"iso8601_micro": "2013-10-02T23:33:30.036070Z",
"minute": "33",
"month": "10",
"second": "30",
"time": "19:33:30",
"tz": "EDT",
"year": "2013"
},
"ansible_default_ipv4": {
"address": "REDACTED",
"alias": "eth0",
"gateway": "REDACTED",
"interface": "eth0",
"macaddress": "REDACTED",
"mtu": 1500,
"netmask": "255.255.255.0",
"network": "REDACTED",
"type": "ether"
},
"ansible_default_ipv6": {},
"ansible_devices": {
"fd0": {
"holders": [],
"host": "",
"model": null,
"partitions": {},
"removable": "1",
"rotational": "1",
"scheduler_mode": "deadline",
"sectors": "0",
"sectorsize": "512",
"size": "0.00 Bytes",
"support_discard": "0",
"vendor": null
},
"sda": {
"holders": [],
"host": "SCSI storage controller: LSI Logic / Symbios Logic 53c1030 PCI-X Fusion-MPT Dual Ultra320 SCSI (rev 01)",
"model": "VMware Virtual S",
"partitions": {
"sda1": {
"sectors": "39843840",
"sectorsize": 512,
"size": "19.00 GB",
"start": "2048"
},
"sda2": {
"sectors": "2",
"sectorsize": 512,
"size": "1.00 KB",
"start": "39847934"
},
"sda5": {
"sectors": "2093056",
"sectorsize": 512,
"size": "1022.00 MB",
"start": "39847936"
}
},
"removable": "0",
"rotational": "1",
"scheduler_mode": "deadline",
"sectors": "41943040",
"sectorsize": "512",
"size": "20.00 GB",
"support_discard": "0",
"vendor": "VMware,"
},
"sr0": {
"holders": [],
"host": "IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)",
"model": "VMware IDE CDR10",
"partitions": {},
"removable": "1",
"rotational": "1",
"scheduler_mode": "deadline",
"sectors": "2097151",
"sectorsize": "512",
"size": "1024.00 MB",
"support_discard": "0",
"vendor": "NECVMWar"
}
},
"ansible_distribution": "Ubuntu",
"ansible_distribution_release": "precise",
"ansible_distribution_version": "12.04",
"ansible_domain": "",
"ansible_env": {
"COLORTERM": "gnome-terminal",
"DISPLAY": ":0",
"HOME": "/home/mdehaan",
"LANG": "C",
"LESSCLOSE": "/usr/bin/lesspipe %s %s",
"LESSOPEN": "| /usr/bin/lesspipe %s",
"LOGNAME": "root",
"LS_COLORS": "rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:",
"MAIL": "/var/mail/root",
"OLDPWD": "/root/ansible/docsite",
"PATH": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"PWD": "/root/ansible",
"SHELL": "/bin/bash",
"SHLVL": "1",
"SUDO_COMMAND": "/bin/bash",
"SUDO_GID": "1000",
"SUDO_UID": "1000",
"SUDO_USER": "mdehaan",
"TERM": "xterm",
"USER": "root",
"USERNAME": "root",
"XAUTHORITY": "/home/mdehaan/.Xauthority",
"_": "/usr/local/bin/ansible"
},
"ansible_eth0": {
"active": true,
"device": "eth0",
"ipv4": {
"address": "REDACTED",
"netmask": "255.255.255.0",
"network": "REDACTED"
},
"ipv6": [
{
"address": "REDACTED",
"prefix": "64",
"scope": "link"
}
],
"macaddress": "REDACTED",
"module": "e1000",
"mtu": 1500,
"type": "ether"
},
"ansible_form_factor": "Other",
"ansible_fqdn": "ubuntu2.example.com",
"ansible_hostname": "ubuntu2",
"ansible_interfaces": [
"lo",
"eth0"
],
"ansible_kernel": "3.5.0-23-generic",
"ansible_lo": {
"active": true,
"device": "lo",
"ipv4": {
"address": "127.0.0.1",
"netmask": "255.0.0.0",
"network": "127.0.0.0"
},
"ipv6": [
{
"address": "::1",
"prefix": "128",
"scope": "host"
}
],
"mtu": 16436,
"type": "loopback"
},
"ansible_lsb": {
"codename": "precise",
"description": "Ubuntu 12.04.2 LTS",
"id": "Ubuntu",
"major_release": "12",
"release": "12.04"
},
"ansible_machine": "x86_64",
"ansible_memfree_mb": 74,
"ansible_memtotal_mb": 991,
"ansible_mounts": [
{
"device": "/dev/sda1",
"fstype": "ext4",
"mount": "/",
"options": "rw,errors=remount-ro",
"size_available": 15032406016,
"size_total": 20079898624
}
],
"ansible_nodename": "ubuntu2.example.com",
"ansible_os_family": "Debian",
"ansible_pkg_mgr": "apt",
"ansible_processor": [
"Intel(R) Core(TM) i7 CPU 860 # 2.80GHz"
],
"ansible_processor_cores": 1,
"ansible_processor_count": 1,
"ansible_processor_threads_per_core": 1,
"ansible_processor_vcpus": 1,
"ansible_product_name": "VMware Virtual Platform",
"ansible_product_serial": "REDACTED",
"ansible_product_uuid": "REDACTED",
"ansible_product_version": "None",
"ansible_python_version": "2.7.3",
"ansible_selinux": false,
"ansible_ssh_host_key_dsa_public": "REDACTED KEY VALUE"
"ansible_ssh_host_key_ecdsa_public": "REDACTED KEY VALUE"
"ansible_ssh_host_key_rsa_public": "REDACTED KEY VALUE"
"ansible_swapfree_mb": 665,
"ansible_swaptotal_mb": 1021,
"ansible_system": "Linux",
"ansible_system_vendor": "VMware, Inc.",
"ansible_user_id": "root",
"ansible_userspace_architecture": "x86_64",
"ansible_userspace_bits": "64",
"ansible_virtualization_role": "guest",
"ansible_virtualization_type": "VMware"
The debug module can be used to analyze variables. Be careful running the following command. In our setup it generates 444709 lines with 16MB:
ansible -m debug -a 'var=hostvars' localhost
I am not sure but it might be necessary to enable facts caching.
If you need just one host use the host name as a key for the hostvars hash:
ansible -m debug -a 'var=hostvars.localhost' localhost
This command will display also group and host variables.
Note the official docs on connection configuration variables or "behavioral" variables - which aren't listed in host vars, appears to be List of Behavioral Inventory Parameters in the Inventory documentation.
P.S. The sudo option is undocumented there (yes its sudo not ansible_sudo as you'd expect ...) and probably a couple more aren't, but thats best doc I've found on em.
Some variables are not available on every host, e.g. ansible_domain and domain. If the situation needs to be debugged, I login to the server and issue:
user#server:~$ ansible -m setup localhost | grep domain
[WARNING]: provided hosts list is empty, only localhost is available
"ansible_domain": "prd.example.com",
I know this question has been answered already, but I feel like there are a whole other set of pre-defined variables not covered by the ansible_* facts. This documentation page covers the directives (variables that modify Ansible's behavior), which I was looking for when I came across this page.
This includes some common and some specific use-case directives:
become: Controls privilege escalation (sudo)
delegate_to: run task on another host (like running on localhost)
serial: allows you to run a play across a specific number/percentage of hosts before moving onto next set
https://github.com/f500/ansible-dumpall
FYI: this github project shows you how to list 90% of variables across all hosts. I find it more globally useful than single host commands. The README includes instructions for building a simple inventory report. It's even more valuable to run this at the end of a playbook to see all the Facts. To also debug Task behaviour use register:
The result is missing a few items:
- included YAML file variables
- extra-vars
- a number of the Ansible internal vars described here: Ansible Behavioural Params
I am wanting to force myself to use hjkl to navigate while practising VIM in Sublime text. I.e. I would like to map the cursor keys to do nothing in insert mode.
Add the following lines in your "Packages/User/Default.sublime-keymap".
[
{"keys": ["up"], "command": "pass",
"context": [{ "key": "setting.command_mode", "operand": false }]},
{"keys": ["down"], "command": "pass",
"context": [{ "key": "setting.command_mode", "operand": false }]},
{"keys": ["left"], "command": "pass",
"context": [{ "key": "setting.command_mode", "operand": false }]},
{"keys": ["right"], "command": "pass",
"context": [{ "key": "setting.command_mode", "operand": false }]}
]
(If you already have the file, insert {"keys" ... }]} lines before the last ] in your file.)