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.
Example 1:
- identify: set up the newest model of Apache and MariaDB
dnf:
identify:
- httpd
- mariadb-server
state: newest
This installs the Apache net server and the MariaDB SQL database.
Example 2:
- identify: Install an inventory of packages
yum:
identify:
- nginx
- postgresql
- postgresql-server
state: current
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.
Example 1:
- identify: Start service foo, primarily based on operating course of /usr/bin/foo
service:
identify: foo
sample: /usr/bin/foo
state: began
This begins the service foo.
Example 2:
- identify: Restart community service for interface eth0
service:
identify: community
state: restarted
args: 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.
Example 1:
- identify: Copy a brand new "ntp.conf file into place, backing up the unique if it differs from the copied model
copy:
src: /mine/ntp.conf
dest: /and many others/ntp.conf
proprietor: root
group: root
mode: '0644'
backup: sure
Example 2:
- identify: Copy file with proprietor and permission, utilizing symbolic illustration
copy:
src: /srv/myfiles/foo.conf
dest: /and many others/foo.conf
proprietor: foo
group: foo
mode: u=rw,g=r,o=r
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.
Example 1:
- identify: Display all variables/info identified for a number
debug:
var: hostvars[inventory_hostname]
verbosity: four
This shows all of the variable info for a number that’s outlined within the stock file.
Example 2:
- identify: Write some content material in a file /tmp/foo.txt
copy:
dest: /tmp/foo.txt
content material: |
Good Morning!
Awesome sunshine right this moment.
register: display_file_content
- identify: Debug display_file_content
debug:
var: display_file_content
verbosity: 2
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.
Example 1:
- identify: Change file possession, group and permissions
file:
path: /and many others/foo.conf
proprietor: foo
group: foo
mode: '0644'
This creates a file named foo.conf and units the permission to 0644.
Example 2:
- identify: Create a listing if it doesn't exist
file:
path: /and many others/some_directory
state: listing
mode: '0755'
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.
Example 1:
- identify: Ensure SELinux is ready to imposing mode
lineinfile:
path: /and many others/selinux/config
regexp: '^SELINUX='
line: SELINUX=imposing
This units the worth of SELINUX=imposing.
Example 2:
- identify: Add a line to a file if the file doesn't exist, with out passing regexp
lineinfile:
path: /and many others/resolv.conf
line: 192.168.1.99 foo.lab.web foo
create: sure
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 1:
# Example Create git archive from repo
- git:
repo: https://github.com/ansible/ansible-examples.git
dest: /src/ansible-examples
archive: /tmp/ansible-examples.zip
Example 2:
- git:
repo: https://github.com/ansible/ansible-examples.git
dest: /src/ansible-examples
separate_git_dir: /src/ansible-examples.git
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.
Example 1:
- identify: commit with remark
cli_config:
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.
Example 2:
- identify: configurable backup path
cli_config:
config: " lookup('template', 'basic/config.j2') "
backup: sure
backup_options:
filename: backup.cfg
dir_path: /house/person
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.
Example 1:
- identify: Compress listing /path/to/foo/ into /path/to/foo.tgz
archive:
path: /path/to/foo
dest: /path/to/foo.tgz
Example 2:
- identify: Create a bz2 archive of a number of recordsdata, rooted at /path
archive:
path:
- /path/to/foo
- /path/wong/foo
dest: /path/file.tar.bz2
format: bz2
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.
Example 1:
- identify: return motd to registered var
command: cat /and many others/motd
register: mymotd
Example 2:
- 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
become_user: db_owner
args:
chdir: somedir/
creates: /path/to/database
Conclusion
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.