Skip to content

Instantly share code, notes, and snippets.

@Sam-R
Created October 17, 2019 14:23
Show Gist options
  • Save Sam-R/98e83acf6fa8513789bc47801aa9996d to your computer and use it in GitHub Desktop.
Save Sam-R/98e83acf6fa8513789bc47801aa9996d to your computer and use it in GitHub Desktop.
Ansible notes

Ansible notes

plays

tasks actions to take inside a play

registers capture output from a task

handlers handle errors and can execute an action

ansible-playbook -i inventory/hosts playbook.yml --ask-vault-pass

Project structure

/playbook.yml
/inventory
  /hosts
/roles
  /webserver
    /tasks
      /main.yml
    /files
  /database
    /tasks
      /main.yml

Loops

Iterate over all of the packages to install them:

---
- hosts: all
tasks:
    - name: Install list of packages
      apt: name={{item}} state=installed
        with_items:
          - apache2
          - vim
          - tmux
          - mosh

Also works with a variable: with_items: "{{ var-packages }}"

You can call with_items using the inventories file, and access sub-groups using []

# Display inventory using groups['webserver']
- debug:
msg: "{{ item }}"
with_items:
- "{{ groups['webserver'] }}"

Includes

Includes can be at play or task levels:

- include: play-level-include.yml
- name: play level include
hosts: all
tasks:

Vault

encrypt

ansible-vault encrypt vault.yml

decrypt

ansible-vault decrypt vault.yml

view

ansible-vault view vault.yml --ask-vault-pass

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment