Ansible is an open supply IT configuration administration and automation platform. It makes use of human-readable YAML templates so customers can program repetitive duties to occur robotically with out having to be taught a sophisticated programming language.
Ansible is agentless, which implies the nodes it manages don’t require any software program to be put in on them. This eliminates potential safety vulnerabilities and makes total administration smoother.
Ansible modules are standalone scripts that can be utilized inside an Ansible playbook. A playbook consists of a play, and a play consists of duties. These ideas could seem complicated for those who’re new to Ansible, however as you start writing and dealing extra with playbooks, they are going to grow to be acquainted.
There are some modules which are often utilized in automating on a regular basis duties; these are those that we are going to cowl on this article.
Ansible has three principal recordsdata that it’s worthwhile to contemplate:
- Host/stock file: Contains the entry of the nodes that must be managed
- Ansible.cfg file: Located by default at /and many others/ansible/ansible.cfg, it has the mandatory privilege escalation choices and the situation of the stock file
- Main file: A playbook that has modules that carry out varied duties on a number listed in a list or host file
Module 1: Package administration
There is a module for hottest bundle managers, corresponding to DNF and APT, to allow you to put in any bundle on a system. Functionality relies upon fully on the bundle supervisor, however normally these modules can set up, improve, downgrade, take away, and listing packages. The names of related modules are straightforward to guess. For instance, the DNF module is dnf_module, the previous YUM module (required for Python 2 compatibility) is yum_module, whereas the APT module is apt_module, the Slackpkg module is slackpkg_module, and so forth.
- identify: set up the newest model of Apache and MariaDB
This installs the Apache net server and the MariaDB SQL database.
- identify: Install an inventory of packages
This installs the listing of packages and helps obtain a number of packages.
Module 2: Service
After putting in a bundle, you want a module to begin it. The service module allows you to begin, cease, and reload put in packages; this is available in fairly helpful.
- identify: Start service foo, primarily based on operating course of /usr/bin/foo
This begins the service foo.
- identify: Restart community service for interface eth0
This restarts the community service of the interface eth0.
Module three: Copy
The copy module copies a file from the native or distant machine to a location on the distant machine.
- identify: Copy a brand new "ntp.conf file into place, backing up the unique if it differs from the copied model
dest: /and many others/ntp.conf
- identify: Copy file with proprietor and permission, utilizing symbolic illustration
dest: /and many others/foo.conf
Module four: Debug
The debug module prints statements throughout execution and will be helpful for debugging variables or expressions with out having to halt the playbook.
- identify: Display all variables/info identified for a number
This shows all of the variable info for a number that’s outlined within the stock file.
- identify: Write some content material in a file /tmp/foo.txt
content material: |
Awesome sunshine right this moment.
- identify: Debug display_file_content
This registers the content material of the copy module output and shows it solely if you specify verbosity as 2. For instance:
ansible-playbook demo.yaml -vv
Module 5: File
The file module manages the file and its properties.
- It units attributes of recordsdata, symlinks, or directories.
- It additionally removes recordsdata, symlinks, or directories.
- identify: Change file possession, group and permissions
path: /and many others/foo.conf
This creates a file named foo.conf and units the permission to 0644.
- identify: Create a listing if it doesn't exist
path: /and many others/some_directory
This creates a listing named some_directory and units the permission to 0755.
Module 6: Lineinfile
The lineinfile module manages traces in a textual content file.
- It ensures a selected line is in a file or replaces an present line utilizing a back-referenced common expression.
- It’s primarily helpful if you wish to change only a single line in a file.
- identify: Ensure SELinux is ready to imposing mode
path: /and many others/selinux/config
This units the worth of SELINUX=imposing.
- identify: Add a line to a file if the file doesn't exist, with out passing regexp
path: /and many others/resolv.conf
line: 192.168.1.99 foo.lab.web foo
This provides an entry for the IP and hostname within the resolv.conf file.
Module 7: Git
The git module manages git checkouts of repositories to deploy recordsdata or software program.
# Example Create git archive from repo
This clones a repo with a separate Git listing.
Module eight: Cli_command
The cli_command module, first out there in Ansible 2.7, offers a platform-agnostic means of pushing text-based configurations to community gadgets over the network_cli connection plugin.
- identify: commit with remark
config: set system host-name foo
commit_comment: this can be a check
This units the hostname for a change and exits with a commit message.
- identify: configurable backup path
config: " lookup('template', 'basic/config.j2') "
This backs up a config to a distinct vacation spot file.
Module 9: Archive
The archive module creates a compressed archive of a number of recordsdata. By default, it assumes the compression supply exists on the goal.
- identify: Compress listing /path/to/foo/ into /path/to/foo.tgz
- identify: Create a bz2 archive of a number of recordsdata, rooted at /path
Module 10: Command
One of probably the most primary however helpful modules, the command module takes the command identify adopted by an inventory of space-delimited arguments.
- identify: return motd to registered var
command: cat /and many others/motd
- identify: Change the working listing to somedir/ and run the command as db_owner if /path/to/database doesn't exist.
command: /usr/bin/make_database.sh db_user db_name
grow to be: sure
There are tons of modules out there in Ansible, however these ten are probably the most primary and highly effective ones you should utilize for an automation job. As your necessities change, you possibly can study different helpful modules by getting into ansible-doc <module-name> on the command line or confer with the official documentation.