> For the complete documentation index, see [llms.txt](https://notes.nomanaziz.me/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://notes.nomanaziz.me/devops/infrastructure-as-a-code-iac/ansible/2.-modules.md).

# 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 &"
```

***
