Skip to content
Bits and pieces of Ansible playbooks, tasks, and roles.
Branch: master
Clone or download
Bill Cawthra
Bill Cawthra updated readme
Latest commit 8ec014d Feb 11, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
files simple json test Feb 11, 2018
inventory simple json test Feb 11, 2018
templates reorganized Mar 16, 2017
.gitignore moved files Feb 12, 2016
README.md updated readme Feb 11, 2018
ansible.cfg simple json test Feb 11, 2018
ec2_remote_facts.yml added ec2 remote facts Mar 31, 2017
json_example.yml renamed json playbook Feb 11, 2018
snippets.yml fixed jinja template syntax. removed unncessary lists. Feb 17, 2016

README.md

Ansible Snippets

A collection of Ansible snippets and examples. These are small examples cut from my Ansible playbooks repository.

I simplified this to one page to make it a little more clear.

Using a Default Variable

# sets a default fact, if the Ansible variable `var` doesn't exist.
- name: debug variable, default to `this is just text`.
  debug:
    msg: "{{ var | default('this is just text') }}"

- name: debug variable, default to another variable, `another_var`.
  debug:
    msg: "{{ var | default(another_var) }}"

Getting EC2 Remote Facts

# gathers facts and registers the output as `ec2_facts`
- name: Gather EC2 facts.
  ec2_remote_facts:
    region: "{{ region | default(us-east-1) }}"
  register: ec2_facts

# debug that output
- name: Debug
  debug:
    msg: "{{ aws_ec2_facts }}"

# print out all running instance IDs and their IP addresses.
- name: Get only running instance IP addresses.
  debug:
    msg: "Instance: {{ item.0 }} has IP address: {{ item.1 }}"
  with_together:
    - "{{ aws_ec2_facts.instances|selectattr('state', 'equalto', 'running')|map(attribute='tags.Name')|list }}"
    - "{{ aws_ec2_facts.instances|selectattr('state', 'equalto', 'running')|map(attribute='private_ip_address')|list }}"

Add EC2 Instances Found to an Ansible Group

{% raw %}
- name: Add instances to running Ansible group in memory (not persistent between playbook runs).
  add_host:
    groups: "{{ item.0 }}"
    hostname: "{{ item.1 }}"
  with_together:
    - "{{ ec2_facts.instances|selectattr('state', 'equalto', 'running')|map(attribute='tags.Name')|list }}"
    - "{{ ec2_facts.instances|selectattr('state', 'equalto', 'running')|map(attribute='private_ip_address')|list }}"
{% endraw %}

Splitting a Variable

# set the variable facts
- name: Set our example path and filenames.
  set_fact:
    path: /path/to/location
    file: software-2.0.0.tar

- name: Untar the file
  unarchive:
    src: "{{ file  }}"
    dest: "{{ path }}"

- name: Move the symbolic link.
  file:
    dest: "{{ localhost_split.software_tar_file_location }}{{ localhost_split.software_tar_file.split('-')[0] }}"
    src: "{{ localhost_split.software_tar_file_location }}{{ localhost_split.software_tar_file.split('.tar')[0] }}/"
    state: link

HipChat Notify

# Hipchat/templated message notification example
- name: Create notify message from template.
  template:
    src: notify.txt.j2
    dest: notify.txt
- name: HipChat notify.
  hipchat:
    api: "{{ hipchat_vars.api }}"
    color: "{{ hipchat_vars.color }}"
    from: "{{ hipchat_vars.from }}"
    msg: "{{ lookup('file', './notify.txt') }}"
    msg_format: html
    room: "{{ hipchat_vars.room }}"
    token: "{{ hipchat_vars.token }}"
- name: Delete temporary notify text.
  file:
    path: notify.txt
    state: absent

JSON Manipulation and Formatting

- name: Get simple value.
  set_fact:
    simple_value: "{{ (json.stdout | from_json).example_simple.name }}"

- name: Simple debug.
  debug:
    msg: "{{ simple_value }}"

- name: Get foo value.
  set_fact:
    foo_value: "{{ (json.stdout | from_json).example_list | map(attribute='foo') | list }}"

- name: Jinja list debug, printing out the list as comma seperated.
  debug:
    msg: "{% for each in foo_value %}{{ each }}{% if not loop.last %},{% endif %}{% endfor %}"
You can’t perform that action at this time.