Skip to content

Instantly share code, notes, and snippets.

@halberom
halberom / htmlfile.html
Created December 6, 2017 11:16
ansible - example of parsing html table into dict
<html><title>Find Location of an IP</title>
<body><font face=\"Arial\">
<H1>Please provide an IP or a LOCATION :</H1>
<p>Search Location, DS site code, Country, Network types or Comments</p>
<form method=\"GET\" action=\"launch_search_IP.pl\">
<p> <input name=\"ip_to_search\" width=\"100%\" height=\"100%\" ></p>
<input type=\"submit\" value=\"Search\"></form>
<p> Here are results for your search of <b> 192.168.0.1 </b> :
<br>
<br>
@halberom
halberom / desired_format
Last active November 9, 2017 11:39
ansible - example of pulling elements from list of dicts, into a list
FROM:
[{"id": 1, "field2": "23@46"}, {"id": 2, "field2": "33333@45"}, {"id": 3, "field2": "23444@4634234234"}]
TO:
[23, 1, 46, 33333, 2, 45, 23444, 3, 4634234234]
@halberom
halberom / ansible.cfg
Last active December 14, 2017 15:25
ansible - NOT RECOMMENDED - example of incredibly nasty/unsafe custom filter to provide list comprehension
[defaults]
filter_plugins = ./plugins/filter
@halberom
halberom / play.yml
Last active November 8, 2017 11:43
ansible - example of different ways of getting elements from a list of dicts or nested dict structure
---
- hosts: localhost
gather_facts: False
connection: local
vars:
networks:
- { name: "public", type: public }
- { name: "private", type: private }
- { name: "prod-cluster", cidr: "192.172.0.0/24", reverse: "0.172.192.in-addr.arpa", type: cluster }
alternative:
@halberom
halberom / output
Created October 31, 2017 11:15
ansible - example of splitting on \
PLAY [localhost] ***************************************************************************************************************************************************************************************************************************************************************
TASK [Split user name from account var] ****************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "I want just the username - someone"
}
PLAY RECAP *********************************************************************************************************************************************************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0
@halberom
halberom / dict.py
Last active October 24, 2017 10:35
ansible - bug with lookup dict
# edit lib/ansible/plugins/lookup/dict.py to look like
class LookupModule(LookupBase):
def run(self, terms, variables=None, **kwargs):
# Expect any type of Mapping, notably hostvars
if not isinstance(terms, collections.Mapping):
raise AnsibleError("with_dict expects a dict, got type {0}, terms contains {1}".format(type(terms), terms))
@halberom
halberom / play.yml
Created October 20, 2017 10:04
ansible - example of using dynamic group to run serial:1 over multiple plays
---
- hosts: all
connection: local
gather_facts: False
serial: 1
tasks:
- add_host:
name: "{{ inventory_hostname }}"
groups: "only_one"
@halberom
halberom / foo.json
Last active October 18, 2017 09:39
ansible - example of pulling out all az:subnets pairs from facts
[{
"subnets": [
{
"availability_zone": "eu-west-1c",
"available_ip_address_count": 29,
"cidr_block": "<REDACTED>",
"default_for_az": "false",
"id": "subnet-<id>",
"map_public_ip_on_launch": "false",
"state": "available",
@halberom
halberom / gist:b2f8f051edb34abe7c002fdff9a5ce56
Created October 10, 2017 11:43
ansible - different indents deliberate?
$ grep 'NetworkConfig(indent=' lib/ansible/modules/network/eos/eos_config.py
candidate = NetworkConfig(indent=2)
return NetworkConfig(indent=2, contents=contents)
config = NetworkConfig(indent=2, contents=contents)
config = NetworkConfig(indent=3, contents=config_text)
running_config = NetworkConfig(indent=1, contents=output[0], ignore_lines=diff_ignore_lines)
startup_config = NetworkConfig(indent=1, contents=output[1], ignore_lines=diff_ignore_lines)
running_config = NetworkConfig(indent=1, contents=contents, ignore_lines=diff_ignore_lines)
base_config = NetworkConfig(indent=1, contents=contents, ignore_lines=diff_ignore_lines)
@halberom
halberom / output
Last active October 6, 2017 11:36
ansible - jsonifying yaml
$ ansible-playbook play.yml
PLAY [localhost] ***************************************************************************************************************************************************************************************************************************************************************
TASK [debug] *******************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "[{\"foo\": \"bar\", \"abc\": \"xyz\"}, {\"foo\": 123, \"abc\": 456}]"
}
TASK [command] *****************************************************************************************************************************************************************************************************************************************************************