ANSIBLE Modules

In this section, we will walkthrough common modules with their implementation and explanation

EXAMPLE 1: Copy file from a local machine to a remote server

One of the basic operations of copying a file from a local machine to a remote location can be achieved with help of 'copy' module. In a similar manner, 'fetch' module can be used to copy a file from a remote to a location on the localhost. For Windows target machines, we can use 'win_copy' module.

        - hosts: host_group
          tasks:
            - name: Copy a file from local to remote server
              copy:
                src: ~/root/test_file.txt
                dest: /Desktop
                mode: u=rwx,g=rw,o=r
        
source: www.mydailytutorials.com/how-to-copy-files-and- directories-in-ansible-using-copy-and-fetch-modules/

The above example describes simple module for copying a file from one system to another with specific mode and permissions for the users, groups and others.



EXAMPLE 2: Access Control List - Set and Retrieve file ACL information

The module basically sets or configures and retrieves the file's ACL information.

        - hosts: host_group
          tasks:
            - name: Grant user Ravish read access to a file
            acl:
                path: /etc/acl.conf
                entity: Ravish
                etype: user
                permissions: rw
                state: present
        
source: tuxknight-notes.readthedocs.io/en/latest/ansible/ansible-module-doc.html

In the above example the 'acl' is simply used for configuring the access control permissions for a user to achieve read and write access on a particular file or folder. The etype, that is, the entity type has been set as a user here, however, this can be set as a group too with certain permissions.



EXAMPLE 3: Manage IAM users, groups, roles and keys

The 'iam' modules enables the configuration and management of IAM users, user API keys, groups, roles.

        - hosts: hosts_block
          tasks:
            - name: Create IAM role with custom trust relationship
            iam:
                iam_type: role
                name: AAALambdaTestRole
                state: present
            trust_policy:
                Version: '2012-10-17'
            Statement:
            - Action: sts:AssumeRole
                Effect: Allow
                Principal:
                    Service: lambda.amazonaws.com
        
source: docs.ansible.com/ansible/2.4/iam_module.html

In the above example, the iam module creates a custom trust policy for Amazon's Lambda service. Different parameters like state, version, action controls are configured.



EXAMPLE 4: net_system - Manage the system attributes on network devices

The 'net_system' module allows to define the set of rules and tasks for management of network devices.

        - hosts: host_block
          tasks:
            - name: configure name servers
            net_system:
                name_servers:
                    - 218.86.11.16
                    - 8.8.8.8
        
source: docs.ansible.com/ansible/devel/modules/net_system_module.html

In the above example, the module is used to cofigure the domain name servers on the target nodes present in the network, each of the device in concerned is configured with set of two name resolver IP addresses.


REFERENCES:
1. http://docs.ansible.com/ansible/latest/modules/list_of_all_modules.html