Skip to content

Instantly share code, notes, and snippets.

from jinja2 import contextfilter
class FilterModule(object):
''' Extra filters '''
def filters(self):
return {
'deeplist': self.deeplist,
}
- hosts: web
serial: 1
tasks:
- include: includes/{{ inventory_class }}_web_update.yml
- hosts: core
serial: 1
tasks:
---
- name: Configure UFW for baseline access
ufw: rule=allow
from_ip="{{ item.ip }}"
to_port="{{ item.port }}"
state=enabled
with_items:
- { ip: "10.0.0.0/8", port: "any" }
- { ip: "172.16.0.0/12", port: "any" }
- { ip: "192.168.0.0/16", port: "any" }
- command: grep string file
ignore_errors: yes
register: result
# How to set this fact so that it is a boolean variable, not a string?
- set_fact:
got_string: "{{ (result|success)|bool }}"
Make a template file called, say, rules-generator.j2:
{% for cidr_ip in aws_trusted_networks %}
- proto: tcp
from_port: 22
to_port: 22
cidr_ip: {% cidr_ip %}
{% endfor %}
In your playbook, do:
---
- name: Ensure git is installed (Debian).
apt: name={{ item }} state=installed
with_items: git_packages
# Install git from source when git_install_from_source is true.
- include: install-from-source.yml
when: git_install_from_source == true
---
- hosts: all
connection: local
vars:
role: foo
descriptions:
foo: "this is the description for foo"
bar: "this is the description for bar"
- name: test file existence
stat: file=filename.txt
register: st
- name: do something
shell: ...
when: st.stat.exists
- name: do something not sudo
command: echo hello
sudo: no
Timothys-MacBook-Pro:ansible-test tgerla$ ansible-playbook -i localhost, test-vars.yml
PLAY [localhost] **************************************************************
GATHERING FACTS ***************************************************************
ok: [localhost]
TASK: [debug msg="VLAN for our IP is {{ip_vlan_mapping[ip]}}"] ****************
ok: [localhost] => {
"msg": "VLAN for our IP is 997"