Commenting a line in a file based on finding a pattern in that line.For example comment the line containing the word backup in a cron job
ansible 'servers' -m replace -a 'path=/etc/cron.d/backup_schedule regexp="(.*backup.*)" replace="#\1"'
Check the hypervisor on remote machines
# ansible 'servers' -m setup -a "filter=ansible_virtualization_*"
List remote files
# ansible 'servers' -m command -a 'ls -l /var/www/html'
Copy files
# ansible 'servers' -m copy -a 'src=/home/sudeep/test.php dest=/var/www/html/test.php owner=sudeep group=sudeep'
List all variables a partiucular host can see
# ansible -m debug -a 'var=hostvars[inventory_hostname]' foo.com
Schedule a reboot
# ansible 'foo.com -a '/sbin/shutdown -r +5'
Checking the remote git version on multiple servers
# ansible 'servers' -m command -a "git status args: chdir=/var/www/html/sudeep"
List all tasks in a playbook
# ansible-playbook --list-tasks playbooks/app-server.yml
Get an excerpt from a file
# ansible 'servers' -a 'grep -A 2 port /etc/supervisord.conf'
Remove a port from firewall where the firewall is configured using firewalld
# ansible 'server' -m command -a "firewall-cmd --remove-port='8080/tcp' --zone=internal --permanent "
Reload firewalld
#ansible 'server' -m command -a 'systemctl reload firewalld'
List all opened ports in public zone
#ansible 'server' -m command -a 'firewall-cmd --list-ports --zone=public'
List all services in public zone
#ansible 'server' -m command -a 'firewall-cmd --list-services --zone=public'
List all sources in public zone
#ansible 'server' -m command -a 'firewall-cmd --list-sources --zone=public'
Adding a server to proxysql with a default_hostgroup of 80, writing the changes to disk and loading it to runtime.
ansible 'myserver.example.com' -m proxysql_backend_servers -a 'hostgroup_id=80 hostname=172.16.1.55 load_to_runtime=True login_host=proxysql.example.com login_password=Password123 login_port=6032 login_user=proxysql-admin save_to_disk=True state=present status=ONLINE comment="My new mysql database server"'
We can send the output of the ansible command(or any command) both to a file as well as stdout.See the example below
ansible 'servers' -m command -a "git status args: chdir=/var/www/html/sudeep" 2>&1 | tee ~/outfile