Ansible - yszheda/wiki GitHub Wiki

localhost

sudo

config

with_items

APT

Multiple commands

- command: "{{ item }} chdir=/src/package/"
  with_items:
  - ./configure
  - /usr/bin/make
  - /usr/bin/make install 
key: |
  This text
  has multiple
  lines

copy

- copy:
    src: "{{ role_path }}/files/foo.conf"
    dest: /etc/foo.conf
- name: Your copy task
  copy: src={{ item.src }} dest={{ item.dest }}
  with_items:
    - { src: 'containerizers', dest: '/etc/mesos/containerizers' }
    - { src: 'another_file', dest: '/etc/somewhere' }
    - { src: 'dynamic', dest: '{{ var_path }}' }
  # more files here

run_once

Condition

Check file/dir stat

tasks:
  - name: Check that the somefile.conf exists
    stat:
      path: /etc/file.txt
    register: stat_result

  - name: Create the file, if it doesnt exist already
    file:
      path: /etc/file.txt
      state: touch
    when: stat_result.stat.exists == False

Multiple conditions

Check process

- name: Check if Apache is running
  shell: ps aux | grep apache2 | grep -v grep
  ignore_errors: yes
  changed_when: false
  register: service_apache_status

- name: Report status of Apache
  fail:
    msg: |
      Service apache2 is not running.
      Return code from `grep`:
      {{ service_apache_status.rc }}
  when: service_apache_status.rc != 0

user

Examples

docker

Trouble-shooting

Failed to connect to the host via ssh

# wrong
# 172.17.0.2 ansible_connection=ssh ansible_port=32768 ansible_ssh_pass=pass ansible_ssh_user=root
# correct
localhost:32768 ansible_connection=ssh ansible_ssh_pass=pass ansible_user=root