Most Used Ansible Modules

Ansible is a radically simple IT automation engine that automates cloud provisioningconfiguration managementapplication deploymentintra-service orchestration, and many other IT needs.

It uses no agents and no additional custom security infrastructure, so it’s easy to deploy – and most importantly, it uses a very simple language (YAML, in the form of Ansible Playbooks) that allow you to describe your automation jobs in a way that approaches plain English.

Ansible has three main files that you need to consider:

  • Host/inventory file: Contains the entry of the nodes that need to be managed
  • Ansible.cfg file: Located by default at /etc/ansible/ansible.cfg
  • Main file: A playbook that has modules that perform various tasks on a host listed in an inventory or host file

Module : Package management (YUM, DNF, APT)

Here are you can found all modules on Ansible document website. Various packaging modules available in Ansible as it support various OS, Language, you can found it here

This installs the Apache web server and the MariaDB SQL database.

- nameinstall the latest version of Apache and MariaDB
  dnf:
    name:
     - httpd
     - mariadb-server
    statelatest

This installs the list of packages and helps download multiple packages.

- nameInstall a list of packages
  yum:
    name:
     - nginx
     - postgresql
     - postgresql-server
    statepresent

Module : Service

Controls services on remote hosts. Supported init systems include BSD init, OpenRC, SysV, Solaris SMF, systemd, upstart.

This start Apache service

- name: Start service httpd, if not started
  service:
    name: httpd
    state: started

This restarts the network service of the interface eth0.

- nameRestart network service for interface eth0
  service
:
    name
network
    state
restarted
    args
eth0

Module : Template

- name: Template a file to /etc/files.conf
  template:
    src: /mytemplates/foo.j2
    dest: /etc/file.conf
    owner: bin
    group: wheel
    mode: '0644'

Module : Copy

The copy module copies a file from the local or remote machine to a location on the remote machine.

- nameCopy file with owner and permission, using symbolic representation
  copy:
    src/src/path/tofile
    dest/dest/patch/tofile
    ownerfoo
    groupfoo
    modeu=rw,g=r,o=r

Module : Debug

The debug module prints statements during execution and can be useful for debugging variables or expressions without having to halt the playbook.

- nameDisplay all variables/facts known for a host
  debug
:
    var
hostvars[inventory_hostname]
    verbosity
4

This displays all the variable information for a host that is defined in the inventory file.

This registers the content of the copy module output and displays it only when you specify verbosity as 2. For example:

ansible-playbook demo.yaml -vv

Module : File

The file module manages the file and its properties.

  • It sets attributes of files, symlinks, or directories.
  • It also removes files, symlinks, or directories.

The below creates a file named foo.conf and sets the permission to 0644.

- nameChange file ownership, group and permissions
  file
:
    path
/etc/foo.conf
    owner
foo
    group
foo
    mode
'0644'

Module : Lineinfile

The lineinfile module manages lines in a text file.

  • It ensures a particular line is in a file or replaces an existing line using a back-referenced regular expression.
  • It’s primarily useful when you want to change just a single line in a file.

This sets the value of SELINUX=enforcing.

- nameEnsure SELinux is set to enforcing mode
  lineinfile
:
    path
/etc/selinux/config
    regexp
'^SELINUX='
    line
SELINUX=enforcing

This adds an entry for the IP and hostname in the resolv.conf file.

- nameAdd a line to a file if the file does not exist, without passing regexp
  lineinfile
:
    path
/etc/resolv.conf
    line
192.168.1.99 foo.lab.net foo
    create
yes

Module : Git

The git module manages git checkouts of repositories to deploy files or software.

# Example Create git archive from repo
- git
:
    repo
https://github.com/ansible/ansible-examples.git
    dest
/src/ansible-examples
    archive
/tmp/ansible-examples.zip

This clones a repo with a separate Git directory.

- git:
    repo
https://github.com/ansible/ansible-examples.git
    dest
/src/ansible-examples
    separate_git_dir
/src/ansible-examples.git

Module : Cli_command

The cli_command module, first available in Ansible 2.7, provides a platform-agnostic way of pushing text-based configurations to network devices over the network_cli connection plugin.

This sets the hostname for a switch and exits with a commit message.

- namecommit with comment
  cli_config
:
    config
set system host-name foo
    commit_comment
this is a test

This backs up a config to a different destination file.

- nameconfigurable backup path
  cli_config
:
    config
"{{ lookup('template', 'basic/config.j2') }}"
    backup
yes
    backup_options
:
      filename
backup.cfg
      dir_path
/home/user

Module : Archive

The archive module creates a compressed archive of one or more files. By default, it assumes the compression source exists on the target.

- nameCompress directory /path/to/foo/ into /path/to/foo.tgz
  archive
:
    path
/path/to/foo
    dest
/path/to/foo.tgz
- nameCreate a bz2 archive of multiple files, rooted at /path
  archive
:
    path
:
   - /path/to/foo
    - /path/wong/foo
    dest
/path/file.tar.bz2
    format
bz2

Module : Command

One of the most basic but useful modules, the command module takes the command name followed by a list of space-delimited arguments.

- namereturn motd to registered var
  command
cat /etc/motd
  register
mymotd
- nameChange the working directory to somedir/ and run the command as db_owner if /path/to/database does not exist.
  command
/usr/bin/make_database.sh db_user db_name
  become
yes
  become_user
db_owner
  args
:
    chdir
somedir/
    creates
/path/to/database

This Post Has One Comment

Leave a Reply

Close Menu