- Published on
Ansible first look
- Authors
- Name
- Yuchen Wei
View help documentation:
ansible-doc -s <module_name>
Command module (default module, used when -m is not specified):
ansible-doc -s command # View help documentation
ansible web -m command -a "pwd" # Execute pwd remotely
ansible web -m command -a "ls" # Execute ls remotely
ansible web -m command -a "chdir=/test pwd" # Change directory and execute command
ansible web -m command -a "creates=/test pwd" # pwd won't execute because /test exists
ansible web -m command -a "creates=/test2 pwd" # pwd executes because /test2 does not exist
ansible web -m command -a "removes=/test2 pwd" # pwd doesn't execute because /test2 does not exist
ansible web -m command -a "removes=/test pwd" # pwd executes because /test exists
ansible web -m command -a "chdir=/test create=/test1.file pwd" # pwd executes because test1.file does not exist in /test
However, the command module cannot execute some commands, such as those involving wildcards or environment variables.

Shell module:
Similar to the command module for basic commands but supports most Linux commands.

To change the default module to shell:
vi /etc/ansible/ansible.cfg

Script module:
Execute a local script remotely on a host group.
cd /ansible
cat > shell.file <<EOF
#!/bin/bash
echo My hostname is `hostname`
EOF
ansible all -m script -a '/ansible/shell.file'

Copy module:
# Copy the local /ansible/shell.file file to the /opt directory of all hosts and name it 1.file.
ansible all -m copy -a "src=/ansible/shell.file dest=/opt/1.file"
# Copy a directory to a specified directory, including only the files and subdirectories, not the directory itself.
ansible all -m copy -a "src=/ansible dest=/opt/"
Fetch module:
# Copy files from remote hosts to the Ansible control node.
# Opposite of copy, currently does not support directories.
ansible all -m fetch -a "src=/opt/shell.file dest=/root/dir1"

File module:
# Create a directory named testdir.
ansible all -m file -a "path=/testdir state=directory"
# Create a file testfile1 with specified permissions.
ansible all -m file -a "path=/tmp/testfile1 state=touch mode=777"
# Set permissions for 1.yaml.
ansible all -m file -a "path=/root/1.yaml group=bin mode=777"
# Create a symbolic link testfile2 to the local machine's /var/log/message.
ansible all -m file -a "path=/tmp/testfile2 src=/var/log/message state=link"
# Delete the symbolic link.
ansible all -m file -a "path=/tmp/testfile2 state=absent"
# Delete a directory.
ansible all -m file -a "path=/testdir state=absent"
Unarchive module:
# Transfer a compressed package from the Ansible host to a remote host
# and decompress it into the specified directory with copy=yes (default).
ansible all -m unarchive -a 'src=/data/test1.tar.gz dest=/tmp/ owner=root'
# Decompress a compressed package on the remote host into the specified directory.
ansible all -m unarchive -a 'src=/tmp/test2.tar.gz dest=/tmp/ owner=root copy=no'
Archive module:
# Compress files
ansible all -m archive -a 'path=/var/log/message dest=/data/log.tar.gz format=gz'
Cron module:
# Create a cron job.
ansible all -m cron -a 'hour=2 minute=30 weekday=1-5 name="backup mysql" job=/root/mysql_backup.sh'
# Check for scheduled tasks on the remote host.
ansible all -m shell -a 'crontab -l'
# Disable a task.
ansible all -m cron -a 'hour=2 minute=30 weekday=1-5 name="backup mysql" job=/root/mysql_backup.sh disable=yes'
# Enable a task.
ansible all -m cron -a 'hour=2 minute=30 weekday=1-5 name="backup mysql" job=/root/mysql_backup.sh disable=no'
Yum module
# Install httpd
ansible all -m service -a "name=httpd state=started"
# Remove httpd
ansible all -m service -a "name=httpd state=stop"