# 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

```sh
ansible all -m ping
```

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

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

#### List files on web-servers inventory

```sh
ansible webservers -m command -a "ls"
```

#### Extact docs on particular module

```sh
ansible-doc setup
```

***

### Sample Playbooks

#### apt\_key module

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

#### apt\_repository module

```yaml
- 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

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

#### shell module

```yaml
- name: Start mongod
  shell: "mongod &"
```

***


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://notes.nomanaziz.me/devops/infrastructure-as-a-code-iac/ansible/2.-modules.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
