├── ansible.cfg ├── inventory └── localhost ├── templates └── notify.txt.j2 ├── files └── example.json ├── ec2_remote_facts.yml ├── json_example.yml ├── .gitignore ├── snippets.yml └── README.md /ansible.cfg: -------------------------------------------------------------------------------- 1 | [defaults] 2 | inventory = ./inventory/localhost 3 | retry_files_enabled = False 4 | -------------------------------------------------------------------------------- /inventory/localhost: -------------------------------------------------------------------------------- 1 | [localhost] 2 | 127.0.0.1 ansible_python_interpreter="/usr/bin/env python" 3 | -------------------------------------------------------------------------------- /templates/notify.txt.j2: -------------------------------------------------------------------------------- 1 | Created on {{ ansible_date_time.time }} {{ ansible_date_time.date }} 2 | -------- 3 | {% for x in your_list_of_results %} 4 | {{ x }} 5 | {% endfor %} 6 | -------- 7 | 8 | {{ some_other_variable }} 9 | 10 | and lots of other variables if need be... 11 | -------------------------------------------------------------------------------- /files/example.json: -------------------------------------------------------------------------------- 1 | { 2 | "example_simple": { 3 | "name": "simple", 4 | "foo": "value", 5 | "item": "this" 6 | }, 7 | "example_list": [ 8 | { 9 | "name": "first", 10 | "foo": "bar", 11 | "item": "thud" 12 | }, 13 | { 14 | "name": "second", 15 | "foo": "grunt", 16 | "item": "baz" 17 | } 18 | ] 19 | } 20 | -------------------------------------------------------------------------------- /ec2_remote_facts.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - hosts: localhost 3 | connection: local 4 | gather_facts: yes 5 | tasks: 6 | - name: Gather EC2 remote facts. 7 | ec2_remote_facts: 8 | region: "{{ region | default('us-east-1') }}" 9 | filters: 10 | instance-state-name: stopped 11 | register: ec2_remote_facts 12 | - name: Debug. 13 | debug: 14 | msg: "{{ ec2_remote_facts }}" 15 | -------------------------------------------------------------------------------- /json_example.yml: -------------------------------------------------------------------------------- 1 | - hosts: localhost 2 | connection: local 3 | gather_facts: no 4 | tasks: 5 | - name: Read JSON file (can also be a variable). 6 | shell: cat files/example.json 7 | register: json 8 | 9 | - name: Get simple value. 10 | set_fact: 11 | simple_value: "{{ (json.stdout | from_json).example_simple.name }}" 12 | 13 | - name: Simple debug. 14 | debug: 15 | msg: "{{ simple_value }}" 16 | 17 | - name: Get foo value. 18 | set_fact: 19 | foo_value: "{{ (json.stdout | from_json).example_list | map(attribute='foo') | list }}" 20 | 21 | - name: Jinja list debug, printing out the list as comma seperated. 22 | debug: 23 | msg: "{% for each in foo_value %}{{ each }}{% if not loop.last %},{% endif %}{% endfor %}" 24 | 25 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled source # 2 | ################### 3 | *.com 4 | *.class 5 | *.dll 6 | *.exe 7 | *.o 8 | *.pyc 9 | *.so 10 | 11 | # Packages # 12 | ############ 13 | # it's better to unpack these files and commit the raw source 14 | # git has its own built in compression methods 15 | *.7z 16 | *.box 17 | *.dmg 18 | *.gz 19 | *.iso 20 | *.jar 21 | *.rar 22 | #*.tar 23 | *.zip 24 | 25 | # Logs and databases # 26 | ###################### 27 | *.log 28 | *.sql 29 | *.sqlite 30 | 31 | # OS generated files # 32 | ###################### 33 | .DS_Store 34 | .DS_Store? 35 | ._* 36 | .Spotlight-V100 37 | .Trashes 38 | ehthumbs.db 39 | Thumbs.db 40 | 41 | # OS X VMware directories # 42 | ########################### 43 | *.vmwarevm 44 | 45 | # Vagrant files # 46 | ################ 47 | .vagrant 48 | 49 | # Python Environment # 50 | ###################### 51 | env/* 52 | 53 | # Ansible Vault Files # 54 | ####################### 55 | vault.yml 56 | 57 | # demo folder # 58 | ############### 59 | working/* 60 | -------------------------------------------------------------------------------- /snippets.yml: -------------------------------------------------------------------------------- 1 | --- 2 | ##### 3 | # Snippets playbook 4 | ##### 5 | - hosts: localhost 6 | connection: local 7 | gather_facts: yes 8 | 9 | roles: 10 | # AWS ec2_remote_facts. 11 | - role: aws.ec2_remote_facts 12 | tags: 13 | - ec2_remote_facts 14 | vault_vars: "{{ hostvars['127.0.0.1']['vault'] }}" # When using AWS, there are often vars in different hosts. I use this notation a lot... 15 | # To set this up, create the directory 'working', copy the files/software.1.2.3.tar file there. Create symlink in that directory. I should create a setup task... 16 | - role: localhost.split 17 | tags: 18 | - split 19 | localhost_split: "{{ hostvars['127.0.0.1']['localhost_split'] }}" # yes i know this notation is excessive :) 20 | 21 | post_tasks: 22 | # Hipchat/templated message notification example 23 | - name: Create notify message from template. 24 | template: 25 | src: files/notify.txt.j2 26 | dest: ./notify.txt 27 | - name: HipChat notify. 28 | hipchat: 29 | api: "{{ hipchat_vars.api }}" 30 | color: "{{ hipchat_vars.color }}" 31 | from: "{{ hipchat_vars.from }}" 32 | msg: "{{ lookup('file', './notify.txt') }}" 33 | msg_format: html 34 | room: "{{ hipchat_vars.room }}" 35 | token: "{{ hipchat_vars.token }}" 36 | - name: Delete temporary notify text. 37 | file: 38 | path: notify.txt 39 | state: absent 40 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Ansible Snippets 2 | 3 | A collection of Ansible snippets and examples. These are small examples cut from [my Ansible playbooks repository](https://github.com/bonovoxly/playbook). 4 | 5 | I simplified this to one page to make it a little more clear. 6 | 7 | # Using a Default Variable 8 | 9 | ~~~yml 10 | # sets a default fact, if the Ansible variable `var` doesn't exist. 11 | - name: debug variable, default to `this is just text`. 12 | debug: 13 | msg: "{{ var | default('this is just text') }}" 14 | 15 | - name: debug variable, default to another variable, `another_var`. 16 | debug: 17 | msg: "{{ var | default(another_var) }}" 18 | ~~~ 19 | 20 | # Getting EC2 Remote Facts 21 | 22 | ~~~yml 23 | # gathers facts and registers the output as `ec2_facts` 24 | - name: Gather EC2 facts. 25 | ec2_remote_facts: 26 | region: "{{ region | default(us-east-1) }}" 27 | register: ec2_facts 28 | 29 | # debug that output 30 | - name: Debug 31 | debug: 32 | msg: "{{ aws_ec2_facts }}" 33 | 34 | # print out all running instance IDs and their IP addresses. 35 | - name: Get only running instance IP addresses. 36 | debug: 37 | msg: "Instance: {{ item.0 }} has IP address: {{ item.1 }}" 38 | with_together: 39 | - "{{ aws_ec2_facts.instances|selectattr('state', 'equalto', 'running')|map(attribute='tags.Name')|list }}" 40 | - "{{ aws_ec2_facts.instances|selectattr('state', 'equalto', 'running')|map(attribute='private_ip_address')|list }}" 41 | ~~~ 42 | 43 | # Add EC2 Instances Found to an Ansible Group 44 | 45 | ~~~yml 46 | {% raw %} 47 | - name: Add instances to running Ansible group in memory (not persistent between playbook runs). 48 | add_host: 49 | groups: "{{ item.0 }}" 50 | hostname: "{{ item.1 }}" 51 | with_together: 52 | - "{{ ec2_facts.instances|selectattr('state', 'equalto', 'running')|map(attribute='tags.Name')|list }}" 53 | - "{{ ec2_facts.instances|selectattr('state', 'equalto', 'running')|map(attribute='private_ip_address')|list }}" 54 | {% endraw %} 55 | ~~~ 56 | 57 | # Splitting a Variable 58 | 59 | ~~~yml 60 | # set the variable facts 61 | - name: Set our example path and filenames. 62 | set_fact: 63 | path: /path/to/location 64 | file: software-2.0.0.tar 65 | 66 | - name: Untar the file 67 | unarchive: 68 | src: "{{ file }}" 69 | dest: "{{ path }}" 70 | 71 | - name: Move the symbolic link. 72 | file: 73 | dest: "{{ localhost_split.software_tar_file_location }}{{ localhost_split.software_tar_file.split('-')[0] }}" 74 | src: "{{ localhost_split.software_tar_file_location }}{{ localhost_split.software_tar_file.split('.tar')[0] }}/" 75 | state: link 76 | ~~~ 77 | 78 | # HipChat Notify 79 | 80 | ~~~yml 81 | # Hipchat/templated message notification example 82 | - name: Create notify message from template. 83 | template: 84 | src: notify.txt.j2 85 | dest: notify.txt 86 | - name: HipChat notify. 87 | hipchat: 88 | api: "{{ hipchat_vars.api }}" 89 | color: "{{ hipchat_vars.color }}" 90 | from: "{{ hipchat_vars.from }}" 91 | msg: "{{ lookup('file', './notify.txt') }}" 92 | msg_format: html 93 | room: "{{ hipchat_vars.room }}" 94 | token: "{{ hipchat_vars.token }}" 95 | - name: Delete temporary notify text. 96 | file: 97 | path: notify.txt 98 | state: absent 99 | ~~~ 100 | 101 | # JSON Manipulation and Formatting 102 | 103 | ``` 104 | - name: Get simple value. 105 | set_fact: 106 | simple_value: "{{ (json.stdout | from_json).example_simple.name }}" 107 | 108 | - name: Simple debug. 109 | debug: 110 | msg: "{{ simple_value }}" 111 | 112 | - name: Get foo value. 113 | set_fact: 114 | foo_value: "{{ (json.stdout | from_json).example_list | map(attribute='foo') | list }}" 115 | 116 | - name: Jinja list debug, printing out the list as comma seperated. 117 | debug: 118 | msg: "{% for each in foo_value %}{{ each }}{% if not loop.last %},{% endif %}{% endfor %}" 119 | ``` 120 | --------------------------------------------------------------------------------