Ansible provdies a rich set of built-in modules for automating system administration tasks. You can list all available modules with ansible-doc -l and view detailed documentation for any module using ansible-doc <module_name>.
ping
Verifies connectivity to target hosts.
ansible all -m ping
command
Executes commands on remote nodes without invoking a shell.
Key parameters:
chdir: Change directory before running the command.creates: Skip execution if the specified file exists.removes: Skip execution if the specified file does not exist.warn: Suppress warnings about potential security issues.
ansible all -m command -a "date"
ansible zxserver -a "chdir=/tmp ls"
ansible zxserver -a "creates=/tmp/flag.txt echo 'done'"
copy
Copies files or directories from the control node too managed hosts.
Common options:
src: Local path to source file or directory.content: Inline content to write too destination file.dest: Absolute path on the remote host.backup: Create a backup before overwriting.mode,owner,group: Set file permissions and ownership.
ansible all -m copy -a 'src=/etc/hosts dest=/tmp/hosts mode=644'
ansible zxserver -m copy -a 'content="Managed by Ansible" dest=/etc/motd'
ansible zxserver -m copy -a 'src=/app/config.ini dest=/etc/app.conf backup=yes'
yum
Manages packages on RHEL/CentOS systems.
Key parameters:
name: Package name or group.state: Desired state (present,absent,latest).update_cache: Refresh metadata before installing.
ansible all -m yum -a "name=htop state=present"
file
Manages files, directories, and symbolic links.
Important options:
path: Target path (required).state: Type of object (file,directory,link,absent,touch).recurse: Apply recursively to directories.src: Source path for symbolic links.
ansible zxserver -m file -a "path=/opt/data state=directory owner=appuser"
ansible zxserver -m file -a "path=/var/log/app.log state=touch mode=0600"
ansible zxserver -m file -a "src=/usr/bin/python3 dest=/usr/local/bin/python state=link"
ansible zxserver -m file -a "path=/tmp/tempfile state=absent"
user
Manages user accounts.
ansible grp1 -m user -a "name=jdoe shell=/bin/bash"
ansible grp1 -m user -a "name=jdoe state=absent remove=yes"
cron
Manages scheduled jobs in crontab.
Parameters include time fields (minute, hour, etc.), job, user, and special_time (e.g., reboot, daily).
ansible zxserver -m cron -a 'name="cleanup" hour=2 minute=0 job="/usr/local/bin/clean.sh"'
ansible zxserver -m cron -a 'name="onboot" special_time=reboot job="/sbin/modprobe mydriver"'
shell
Executes commands through the shell, enabling pipes, redirects, and environment variables.
ansible zxserver -m shell -a "echo 'Hello' > /tmp/greeting && chmod 600 /tmp/greeting"
service
Controls system services.
Key options:
name: Service name.state:started,stopped,restarted,reloaded.enabled: Enable/disable at boot.
ansible zxserver -m service -a "name=nginx state=started enabled=yes"
get_url
Downloads files from HTTP/HTTPS/FTP servers.
ansible zxserver -m get_url -a "url=https://example.com/config.yaml dest=/etc/myapp/config.yaml"