Skip to content

Instantly share code, notes, and snippets.

@halberom
halberom / 00_play.yml
Created June 26, 2017 13:35
ansible - example of generating complex fact using jinja, and passing to role
---
- hosts: localhost
connection: local
gather_facts: no
vars:
foo: bar
cops_es_nginx_foo: bar
tasks:
- set_fact:
@halberom
halberom / opt1_template.j2
Last active April 12, 2024 11:33
ansible - example of template if else
{# style 1 - long form #}
{% if filepath == '/var/opt/tomcat_1' %}
{% set tomcat_value = tomcat_1_value %}
{% else %}
{% set tomcat_value = tomcat_2_value %}
{% endif %}
{# style 2 - short form #}
{% set tomcat_value = tomcat_1_value if (filepath == '/var/opt/tomcat_1') else tomcat_2_value %}
@halberom
halberom / 00_play.yml
Created May 17, 2017 09:35
ansible - example of using map + regex_replace to modify all items in a list
---
- hosts: localhost
connection: local
gather_facts: False
vars:
mylist:
- group_1
- group_2
tasks:
@halberom
halberom / extras.py
Last active November 8, 2023 16:23
ansible - example of merging lists of dicts
# ansible_plugins/filter_plugins/extras.py
def merge_dicts(value, dict1):
# return a merged dict
result = {}
result = value
result.update(dict1)
return result
def merge_lists_of_dicts(list1, list2):
# return a merged list
@halberom
halberom / efs_targets.j2
Last active May 18, 2023 10:22
ansible - example of using lookup and a template to generate dynamic list entries for modules
#jinja2:trim_blocks: True, lstrip_blocks: True
targets:
{% for privnet in all_private_subnets %}
- subnet_id: "{{ privnet }}"
security_groups: [ "{{ sg.group_id }}" ]
{% endfor %}
{% for pubnet in all_public_subnets %}
- subnet_id: "{{ pubnet }}"
security_groups: [ "{{ sg.group_id }}" ]
{% endfor %}
@halberom
halberom / Makefile
Last active October 13, 2022 13:59
CICD semantic versioning foo - bump a repository version based on past tags instead of VERSION file
.PHONY: bump
# version bump, assumes we're in treeder/bump in CircleCI
bump:
@git config --global user.email "-"
@git config --global user.name "CircleCi"
@(git tag --sort=-creatordate | grep -E '^\d+\.\d+\.\d+$$' || echo '0.0.0') | head -n 1 > VERSION
@/script/bump $$(git log -1 --pretty=%B)
@git tag $$(cat VERSION)
@git push origin $$(cat VERSION)
@halberom
halberom / mail_body.j2
Created September 7, 2017 09:51
ansible - example of using mail module to send one email for all hosts
# templates/mail_body.j2
{% for host in play_hosts %}
The {{ host }} says {{ hostvars[host]['result']['stdout'] }}
{% endfor %}
@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 / 00_play.yml
Last active September 6, 2022 08:03
ansible - example of parsing a url
---
- hosts: localhost
gather_facts: False
connection: local
vars:
myvar: 'http://www.example.domain.com:9090'
tasks:
- name: not as good as a custom filter
debug:
@halberom
halberom / output
Created June 5, 2014 10:36
ansible - using lookup to get a date timestamp into a variable
...
TASK: [debug var=mydate] ******************************************************
ok: [localhost] => {
"mydate": "20140605101824"
}
...