Stosh Oldham
Apr 2 · 3 min read

Ansible has a list of capabilities at least a mile long. For everything you can imagine doing, it seems there is a module that can help. However, when it comes to what we think of as really basic tasks, such as file manipulation, the Ansible solution is not always obvious. Did you know that, as of this writing, there is not a file move module in Ansible? Keep reading, and I will show you a few tricks for everyday file manipulation tasks using Ansible.

Moving a file on a system

Moving files is a fundamental task on any workstation or server. By the time this article is written, I will have moved at least two or three files around on my workstation. As basic as the task of moving a file is, there is not a dedicated module to performing file moves on target systems. There is a copy module, which can make a copy of a file. But that is not quite the same. There are scenarios where you just need to relocate the file as opposed to duplicate it. So how do you do it in Ansible? You have to fall back to the command module.

Here is the ad-hoc command:

ansible -a 'mv /home/user/foo /home/user/bar'

Alternatively in YAML:

- name: move a file
command: mv /home/user/foo /home/user/bar

Downloading a file over http

A frequent scenario you run into in any kind of systems administration or engineering role is the need to pull a file down to a host over http. You generally get a lot of software from your package manager. But when it comes to vendor software or any software you intend to customize, you generally must pull it down off an httpd server (be it local or even on the open web). Additionally, an httpd server is a great way to keep common configuration files for your Ansible-managed environment. Your playbooks can simply pull the most updated configuration files down on execution!

The key to downloading files over http in ansible does not come in the form of curl or wget. We use the get_url module.

- name: download new issue file
dest: /etc/issue

The url argument is where your file is coming from and the dest argument is where it is going on your target system. You may use http or https with get_url. It is also capable of performing basic authentication! But that is a story for another time.

Create a symbolic link

Another task that you might often find a need for is the creation of a symbolic link (or symlink). Having links to existing files can be handy. In some cases, programs rely on symlinks (think Systemd). There is not necessarily a ‘symlink’ module in Ansible however there is a module that can create symbolic links for you. We can find the functionality in our good friend, the file module:

ansible -m file -a 'state=link src=/path/to/file name=/path/to/link'

The key here is passing the state argument the value ‘link’. This instructs the module to create a symbolic link. You will notice we do have to use a few extra arguments to make the symbolic link. The src argument is how we provide the path to our original file. The argument is only used with link files. The name argument will be the final name of your link. If you would rather use a hard link, the approach is pretty similar. You would just use state=hard instead.

Looking for more on Ansible? Check out some of my other articles:

Hopefully you found these Ansible tips helpful! If you are interested in more great tips and tricks for beginners, check out my course, Ansible Quick Start.

Linux Academy

Hands-on cloud and Linux training

Stosh Oldham

Written by

Linux Academy

Hands-on cloud and Linux training

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade