Skip to content

Instantly share code, notes, and snippets.

@thydel
Last active March 9, 2017 11:05
Show Gist options
  • Save thydel/f3cbc54b05ed5d6dbecb7e6f4c86a6cf to your computer and use it in GitHub Desktop.
Save thydel/f3cbc54b05ed5d6dbecb7e6f4c86a6cf to your computer and use it in GitHub Desktop.
Init playbook dir helper
#!/usr/bin/env ansible-playbook
---
- hosts: localhost
gather_facts: True
vars:
use_filter_plugins: False
use_ssh_config: False
ansible_managed_for_copy: Ansible managed
roles_dir: roles
galaxy_requirement_file: galaxy.yml
tasks:
- file:
path: '{{ item }}'
state: directory
with_items: [ .cache/ansible, inventory, '{{ roles_dir }}', log, tmp ]
name: Create empty dirs
- copy:
content: |
# -*- Mode: conf; -*-
# {{ ansible_managed_for_copy }}
[defaults]
# column -t
hostfile = inventory
roles_path = {{ roles_dir }}
{% if use_filter_plugins %}
filter_plugins = plugins/filter
{% endif %}
log_path = log/ansible.log
gathering = smart
fact_caching = jsonfile
fact_caching_connection = .cache/ansible
fact_caching_timeout = 86400
retry_files_enabled = True
retry_files_save_path = .retry
jinja2_extensions = jinja2.ext.do
ansible_managed = Ansible managed: {file} modified by {uid} on {host}
[ssh_connection]
{% if use_ssh_config %}
ssh_args = -F ssh-config
{% endif %}
pipelining = True
dest: '{{ playbook_dir }}/ansible.cfg'
mode: 0444
name: Create ansible config
- copy:
content: |
# -*- Mode: conf; -*-
# {{ ansible_managed_for_copy }}
{% raw %}
local ansible_connection=local ansible_become_pass="{{ lookup('pipe', 'pass node/local') }}"
{% endraw %}
dest: '{{ playbook_dir }}/inventory/local'
mode: 0444
name: Create mininal local inventory
- block:
- stat: { path: '{{ galaxy_requirement_file }}' }
register: galaxy_requirement
- set_fact:
galaxy_requirement_data: |
{{ lookup('file', galaxy_requirement_file) | from_yaml }}
when: galaxy_requirement.stat.exists
- command: ansible-galaxy install {{ item.src }}
args: { creates: '{{ roles_dir }}/{{ item.src }}/README.md' }
with_items: '{{ galaxy_requirement_data }}'
loop_control: { label: '{{ item.src }}' }
when: galaxy_requirement_data is defined
name: install roles from galaxy
- file:
src: "{{ item.src }}"
path: "{{ playbook_dir}}/{{ roles_dir }}/{{ item.name }}"
state: link
with_items: '{{ galaxy_requirement_data }}'
loop_control: { label: '{{ item.name }}' }
when: galaxy_requirement_data is defined
name: link usage name to galaxy name
- name: fill gitignore
tags: gitignore
block:
- lineinfile:
path: '{{ playbook_dir}}/.gitignore'
line: '{{ item }}'
with_flattened:
- [ '*~' ]
- [ '# touched' ]
- [ .stone ]
- [ '# mkdired' ]
- [ .cache, .retry, .hide ]
- [ roles, log, tmp, ext, site ]
- [ '# generated' ]
- [ README.html, CHANGELOG.html, plugins/filter/*.pyc ]
- [ ansible.cfg, ssh-config ]
- lineinfile:
path: '{{ playbook_dir}}/.gitignore'
line: '{{ item }}'
with_flattened:
- [ '# imported', vars/ext/, inventory/*.ext, 'inventory/group_vars/*/*-ext.yml' ]
when: use_data is defined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment