Skip to content

Instantly share code, notes, and snippets.

@vikbert
Last active June 21, 2018 09:36
Show Gist options
  • Select an option

  • Save vikbert/5086ed794d7d59359e87a0b8be687870 to your computer and use it in GitHub Desktop.

Select an option

Save vikbert/5086ed794d7d59359e87a0b8be687870 to your computer and use it in GitHub Desktop.
[ansible] the lightweight tutorial of ansible #ansible

Koza ansible commands

tasks

- apt: pkg=vim state=present		# task one-line
- apt:
    pkg: vim
    state: present
- hosts:all
  user: root
  sudo: no
  vars:
    aaa: "bbb"
  tasks:
    - 

tasks failures

- name: my task
  command: my_command
  register: result
  failed_when: "'FAILED' in result.stderr"
  ignore_errors: yes
  change_when: "result.rc != 2"

include

tasks:
  - include: db.yml
  
handlers:
  - include: db.yml user=timo

handlers

handlers:
  - name: start apache2
    action: service name=apache2 state=started
    
tasks:
  - namne: install apache
    action: apt pkg=apache2 state=latest
    notify: start apache2

vars

- host: development
  vars_files:
    - vars.yml
  vars:
    project_root: /var/www/virtual
  tasks:
    - name: Create the SSH directory
      file:
        - state: directory
        - path: "{{project_root}}/home/.ssh"
      only_if: "$vm == 0"

Env vars

vars:
  local_name: "{{ lookup('env', 'HOME') }}"

roles

roles/
  common/
    tasks/
    handlers/
    files/			# `copy` will refer to this folder
    templates/		# `template` will refer to this folder
    meta/			# role dependencies here
    vars/
    defaults/
      main.yml
- host: xxx
  roles:
    - db
    - web
# roles/db/tasks/*.yml
# roles/web/handlers/*.yml

become

to become another user, the default value of become_user is root

- name: ensure the nginx is running as the vagrant user
  service:
    name: nginx
    state: started
  become: true
  become_method: sudo
  become_user: vagrant

user

# Add the user 'vagrant' with a specific uid and a primary group of 'admin'
- user:
    name: vagrant
    comment: "John Doe"
    uid: 1040
    group: admin

# Add the user 'james' with a bash shell, appending the group 'admins' and 'developers' to the user's groups
- user:
    name: james
    shell: /bin/bash
    groups: admins,developers
    append: yes

# Remove the user 'john'
- user:
    name: john
    state: absent
    remove: yes

# Create a 2048-bit SSH key for user jsmith in ~jsmith/.ssh/id_rsa
- user:
    name: jsmith
    generate_ssh_key: yes
    ssh_key_bits: 2048
    ssh_key_file: .ssh/id_rsa

# added a consultant whose account you want to expire
- user:
    name: james18
    shell: /bin/zsh
    groups: developers
    expires: 1422403387

Common used ansible modules

  • pre_tasks:

  • register:

  • assert:

  • with_items:

  • roles:

  • handlers:

  • service:

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