This role helps with assembling, copying, moving, deleting, editing, uploading and downloading files, directories and links, changing ownership and permissions, cloning git and subversion repositories, extracting archives, accessing web services and more from Ansible variables.
Role variable files_config
defines a list of tasks which will be run by this role. Each task calls an Ansible module
similar to tasks in roles or playbooks except that only few keywords such as when
are supported.
For example, to ensure the inventory name for the current Ansible host being iterated over in the play is in
/etc/hosts
define variable files_config
in group_vars
or host_vars
as such:
files_config:
- ansible.builtin.lineinfile:
path: /etc/hosts
regexp: '\.1\s+{{ hostname }}'
line: "{{ '{ip} {fqdn} {hostname}'.format(ip='127.0.0.1', fqdn=fqdn, hostname=hostname) }}"
owner: root
group: root
mode: '0644'
when: distribution_id | first in ['CentOS', 'Red Hat Enterprise Linux']
or distribution_id in [['Debian', '10']]
- ansible.builtin.lineinfile:
path: /etc/hosts
regexp: '\.1\s+{{ hostname }}'
line: "{{ '{ip} {fqdn} {hostname}'.format(ip='127.0.1.1', fqdn=fqdn, hostname=hostname) }}"
owner: root
group: root
mode: '0644'
when: distribution_id | first not in ['CentOS', 'Red Hat Enterprise Linux']
and distribution_id not in [['Debian', '10']]
- ansible.builtin.lineinfile:
path: /etc/hosts
regexp: '::1\s+{{ hostname }}'
line: '::1 {{ fqdn }} {{ hostname }}'
owner: root
group: root
mode: '0644'
Tested OS images
- Cloud image (
amd64
) of Debian 10 (Buster) - Cloud image (
amd64
) of Debian 11 (Bullseye) - Cloud image (
amd64
) of Debian 12 (Bookworm) - Cloud image (
amd64
) of Debian 13 (Trixie) - Cloud image (
amd64
) of CentOS 7 (Core) - Cloud image (
amd64
) of CentOS 8 (Stream) - Cloud image (
amd64
) of CentOS 9 (Stream) - Cloud image (
amd64
) of Fedora Cloud Base 40 - Cloud image (
amd64
) of Ubuntu 18.04 LTS (Bionic Beaver) - Cloud image (
amd64
) of Ubuntu 20.04 LTS (Focal Fossa) - Cloud image (
amd64
) of Ubuntu 22.04 LTS (Jammy Jellyfish) - Cloud image (
amd64
) of Ubuntu 24.04 LTS (Noble Numbat)
Available on Ansible Galaxy in Collection jm1.cloudy.
This role uses module(s) from collections community.general
and jm1.ansible
. To install these collections you may follow the steps described in README.md
using the provided requirements.yml
.
Name | Default value | Required | Description |
---|---|---|---|
files_config |
[] |
false | List of tasks to run 1 2 3 |
None.
- hosts: all
become: true
vars:
# Variables are listed here for convenience and illustration.
# In a production setup, variables would be defined e.g. in
# group_vars and/or host_vars of an Ansible inventory.
# Ref.:
# https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html
# https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html
files_config:
- name: Clone a git repository
ansible.builtin.git:
repo: https://github.com/ansible/ansible-examples.git
dest: /src/ansible-examples
roles:
- name: Manage files, directories, links and more
role: jm1.cloudy.files
tags: ["jm1.cloudy.files"]
For instructions on how to run Ansible playbooks have look at Ansible's Getting Started Guide.
GNU General Public License v3.0 or later
See LICENSE.md to see the full text.
Jakob Meng @jm1 (github, galaxy, web)
Footnotes
-
Useful Ansible modules in this context could be
assemble
,blockinfile
,capabilities
,copy
,fetch
,file
,get_url
,git
,lineinfile
,replace
,slurp
,subversion
,template
,unarchive
,uri
andxattr
. ↩ -
Tasks will be executed with
jm1.ansible.execute_module
which supports keywordwhen
only. ↩ -
Tasks will be executed with
jm1.ansible.execute_module
which supports modules and action plugins only. Some Ansible modules such asansible.builtin.meta
andansible.builtin.{include,import}_{playbook,role,tasks}
are core features of Ansible, in fact not implemented as modules and thus cannot be called fromjm1.ansible.execute_module
. Doing so causes Ansible to raise errors such asMODULE FAILURE\nSee stdout/stderr for the exact error
. In addition, Ansible does not support free-form parameters for arbitrary modules, so for example, change from- debug: msg=""
to- debug: { msg: "" }
. ↩