Skip to content

Instantly share code, notes, and snippets.

@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"
}
...
@halberom
halberom / output
Last active July 25, 2022 12:50
ansible - example of passing registered var containing json to custom module
PLAY [foo] ********************************************************************
GATHERING FACTS ***************************************************************
ok: [localhost]
TASK: [raw stat /tmp/foo] *****************************************************
ok: [localhost -> 127.0.0.1]
TASK: [test_lib ] *************************************************************
ok: [localhost]
@halberom
halberom / 00_play.yml
Created May 12, 2017 09:33
ansible - example of using selectattr to filter on attrs that are defined and true
---
- hosts: localhost
connection: local
gather_facts: False
vars:
apache2_vhosts:
- name: "stage.example.org"
docroot: "/var/www/stage1"
- name: "stage2.example.org"
docroot: "/var/www/stage2"
@halberom
halberom / output
Last active May 5, 2022 07:38
ansible - example of setting a complex var with set_fact
PLAY [foo] ********************************************************************
GATHERING FACTS ***************************************************************
ok: [localhost]
TASK: [set_fact ] *************************************************************
ok: [localhost]
TASK: [debug ] ****************************************************************
ok: [localhost] => {
@halberom
halberom / 00_description
Last active April 14, 2022 19:23
ansible - example of using filters to change each item in a list
The problem:
I wanted to use the jinja 'map' filter to modify each item in a string, in this simple
example, adding '.conf' to each item.
The 'format' filter in jinja takes arguments (value, *args, **kwargs). Unfortunately,
it uses 'value' as the pattern. When called inside map 'value' is the current item in
the list, or in other words *args as far as format is concerned. So it's the wrong way
around.
@halberom
halberom / 00_play.yml
Last active December 11, 2021 05:08
ansible - convoluted json_query foo - reducing a nested dict to just bits we can use
---
- hosts: localhost
connection: local
gather_facts: False
tasks:
# - include_vars: jsonfile.json
#
# - debug:
# var: ansible_devices
#
@halberom
halberom / 01_gather_installed.py
Last active November 22, 2021 10:37
ansible - example of custom module to gather info on installed packages
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Note: only works on RedHat family
# This library should be saved to the 'library/' folder relative to your playbooks/inventory
import rpm
def _check_installed(module):
ts = rpm.TransactionSet()
@halberom
halberom / 0_play.yml
Last active November 5, 2021 18:13
ansible - example of calling a handler when a fact is set
---
- hosts: localhost
connection: local
gather_facts: False
tasks:
- debug: msg="call handler"
changed_when: True
notify: do something
handlers:
@halberom
halberom / play.yml
Last active October 9, 2021 04:07
ansible - example of template module and multiple files
---
- hosts: foo
tasks:
- name: register templates
local_action: shell ls {{ inventory_dir }}/path/to/templates
register: template_files
- template: src={{ item }} dest=/etc/httpd/conf.d/{{item}}
with_items: template_files.stdout_lines
@halberom
halberom / 01_play_undefined.yml
Last active August 27, 2021 05:54
ansible - example of loop_control loop_var
---
- name: unable to access outer var in inner loop (in mysites.yml)
hosts: localhost
connection: local
gather_facts: False
vars:
mylist:
- foo
- bar
tasks: