2. Modules

Modules

  • Modules are executable plugins that get the job done

  • They take key value as arguments

  • They can be invoked using command line or included in ansible playbook


Sample Commands

Ping all inventory hosts

ansible all -m ping

Flush IP table rules on all hosts in the inventory using sudo privileges

ansible -i inventory all -m command -a "iptables -F" --become --ask-become-pass

List files on web-servers inventory

ansible webservers -m command -a "ls"

Extact docs on particular module

ansible-doc setup

Sample Playbooks

apt_key module

- name: MongoDB - Import public key
  apt_key:
    keyserver: hkp://keyserver.ubuntu.com:80
    id: EA312927

apt_repository module

- name: MongoDB - Add repository
  apt_repository:
    filename: '/etc/apt/sources.list.d/mongodb-org-3.2.list'
    repo: 'deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2 multiverse'
    state: present
    update_cache: yes

apt module

- name: MongoDB - Install MongoDB
  apt:
    name: mongodb-org
    state: present
    update_cache: yes

shell module

- name: Start mongod
  shell: "mongod &"

Last updated