Creating new files is a pretty standard task in server scripts. In Ansible there are multiple methods to create new empty files. You can also set different permissions, different group permissions, set the owner of the file, create files with content in them etc. Let us go through some ways in which it can be […]
Ansible
Replacing Strings and Lines in Ansible
There are multiple ways in which you can replace a particular word, a line, all the words matching a specific pattern etc. in Ansible. Depending on the situation we can use the Ansible ‘replace’ module or ‘lineinfile’ module. In this post, we will see some examples of how to use these modules to get the desired […]
Working with Ansible yum module
Ansible has a specific module for managing yum packages. You can install, remove, upgrade or downgrade versions, etc. using this module. As with other package management modules in Ansible, the yum module also requires two parameters for the primary command. Name – The package you want to install. State – what should be the state of […]
Ansible Pip Module – Managing Python Packages
Ansible pip module is used when you need to manage Python libraries on the remote servers. There are two prerequisites if you need to use all the features in this module. 1 – The pip package should be installed on the remote server already. You can use the Ansible apt module or similar to install […]
Working with Environment Variables in Ansible
Environment Variables in the Local Machine/Ansible Control Machine First, let us see how to work with the environment variables on the server which acts as the ACM. Accessing the environment variables You can access the environment variables available on the local server via the ‘lookup’ plugins, which allow you to access the system […]
Working with Ansible facts – retrieving facts
What are Ansible facts? Ansible facts are pieces of information regarding the remote systems to which you have connected. It contains information like IP addresses, the OS installed, Ethernet devices, mac address, time/date related data, hardware information etc. These are very useful for scenarios where you need to take conditional operations based on […]
How to split strings and join them in Ansible
Split commands are useful when you need to divide a large line or a block of lines into a list. The dividing character will be specified in the command itself. So we can split when every ‘space’ character occurs, semicolon appears, a digit occurs etc. Join command is useful when you want values in a […]
How to add lines to a file in Ansible
Depending on your requirements, you might need to add a line to a file in different positions. Start of the file. End of the file. After a line/pattern. Before a line/pattern. Append a string to a line (before/after). Also, there might be other conditions you need to satisfy like, Add multiple lines. Add command line […]
How to uncomment/comment lines in files using Ansible
Uncommenting or commenting a line is usually a task of just adding character or removing a character at the front; Unless it is a block comment. Let us see a few ways of dealing with single line comments in Ansible. Commenting a line in Ansible using replace module Replace module in Ansible is used to […]
How to check if a file/directory exists in Ansible
To get the details of a file or directory in a Linux system, we can use the Ansible stat module. It works similar to the Linux ‘stat’ command. Of course, the module provides much more detail than whether a file exists or not. We can know when the file was last modified, what all permissions […]