Monday, April 8, 2024

Ansible Mastery: 100 Essential Commands and Techniques for Efficient Automation

Ansible Mastery: 100 Essential Commands and Techniques for Efficient Automation

Content:

  1. ansible --version: Check Ansible version.
  2. ansible all -m ping: Test connectivity to all hosts.
  3. ansible all -a "whoami": Execute the whoami command on all hosts.
  4. ansible-playbook playbook.yml: Run a playbook.
  5. ansible-inventory --list: List all hosts in your inventory.
  6. ansible-playbook playbook.yml --syntax-check: Check syntax of a playbook.
  7. ansible-playbook playbook.yml --limit "hostname": Target a specific host in a playbook run.
  8. ansible-playbook playbook.yml --check: Dry run a playbook.
  9. ansible all -m setup: Gather facts from all hosts.
  10. ansible all -b -a "apt update": Update package lists on all Debian/Ubuntu hosts.
  11. ansible all -m copy: Copy files to all hosts.
  12. ansible all -m file: Delete files from all hosts.
  13. ansible all -m user: Ensure a user exists on all hosts.
  14. ansible all -m service: Ensure a service is started on all hosts.
  15. ansible-vault create secret.yml: Create a new encrypted file.
  16. ansible-vault edit secret.yml: Edit an encrypted file.
  17. ansible-vault decrypt secret.yml: Decrypt a file.
  18. ansible-vault encrypt secret.yml: Encrypt a file.
  19. ansible-doc -l: List all available Ansible modules.
  20. ansible-doc copy: View documentation for the copy module.
  21. ansible all -m shell: Execute shell commands on all hosts.
  22. ansible-playbook --step playbook.yml: Execute a playbook in step-by-step mode.
  23. ansible all -m apt: Install nginx on all Debian/Ubuntu hosts.
  24. ansible all -m yum: Install httpd on all RHEL/CentOS hosts.
  25. ansible all -m group: Ensure a group exists on all hosts.
  26. ansible-galaxy install username.rolename: Install a specific role from Ansible Galaxy.
  27. ansible-console: Start an interactive Ansible console.
  28. ansible-config list: List all configuration settings.
  29. ansible-config view: View the current configuration file.
  30. ansible all -m ping --forks=50: Execute the ping module with 50 parallel forks.
  31. ansible-playbook playbook.yml -e "var=value": Run playbook with extra variables.
  32. ansible all -m setup -a "filter=ansible_os_family": Gather OS family facts from all hosts.
  33. ansible-inventory --graph: Display a graph of your inventory.
  34. ansible-playbook playbook.yml --tags "tagname": Run only tasks with a specific tag in a playbook.
  35. ansible-playbook playbook.yml --skip-tags "tagname": Skip tasks with a specific tag in a playbook.
  36. ansible all -m debug -a "var=hostvars": Debug by printing host variables.
  37. ansible-vault rekey secret.yml: Change the encryption password for a vault file.
  38. ansible all -m setup --tree /tmp/facts: Save gathered facts into files under /tmp/facts.
  39. ansible-galaxy role init myrole: Initialize a new role structure.
  40. ansible-galaxy collection install community.general: Install a collection from Ansible Galaxy.
  41. ansible-playbook playbook.yml --vault-id @prompt: Run a playbook with vault, prompting for the password.
  42. ansible localhost -m debug -a "msg='Hello World'": Print "Hello World" message.
  43. ansible all -m stat -a "path=/path/to/file": Get file statistics for all hosts.
  44. ansible all -m setup -a "gather_subset=!all": Gather limited set of facts from all hosts.
  45. ansible all -b -m package -a "name=git state=present": Install git on all hosts using the package module.
  46. ansible-playbook playbook.yml --force-handlers: Force handlers to run even if a task fails.
  47. ansible all -m ping --become: Test connectivity using become (sudo).
  48. ansible all -m setup -a "filter=ansible_distribution": Gather distribution facts from all hosts.
  49. ansible-playbook playbook.yml --list-tasks: List all tasks in a playbook.
  50. ansible-playbook playbook.yml --list-hosts: List all hosts targeted by a playbook.
  51. ansible all -m win_command -a "ipconfig /all": Execute ipconfig /all on Windows hosts.
  52. ansible all -m win_ping: Test connectivity to Windows hosts.
  53. ansible all -m setup -a "filter=ansible_processor*": Gather processor facts.
  54. ansible-playbook playbook.yml --tags "configure": Run tasks tagged with "configure".
  55. ansible localhost -m setup -a "filter=ansible_processor_cores": Get processor core info from localhost.
  56. ansible all -b -m yum -a "name=nginx state=latest": Ensure latest NGINX on RHEL/CentOS.
  57. ansible all -b -m apt -a "pkg=apache2 state=absent": Remove Apache2 on Debian/Ubuntu.
  58. ansible all -m ping -e 'ansible_python_interpreter=/usr/bin/python3': Use Python3 for ping.
  59. ansible all -b -m service -a "name=ssh state=restarted": Restart SSH service on all nodes.
  60. ansible all -m debug -a "msg='{{ ansible_facts['os_family'] }}'": Show OS family via debug.
  61. ansible all -b -m file -a "dest=/example/file state=file mode=0664": Ensure file exists with permissions.
  62. ansible all -b -m shell -a "useradd -m john": Add user 'john' with home directory.
  63. ansible-playbook playbook.yml --limit "webservers:!phoenix": Exclude 'phoenix' from 'webservers'.
  64. ansible all -m ping --become --become-user root: Ping as root.
  65. ansible all -m setup -a "filter=ansible_memory_mb": Gather memory facts.
  66. ansible-playbook playbook.yml --no-cows: No cowsay.
  67. ansible-console -i inventory.yml: Interactive console with inventory.
  68. ansible all -m get_url -a "url=https://example.com/file dest=/tmp/file": Download file.
  69. ansible all -b -m shell -a "iptables -L": List iptables rules.
  70. ansible-galaxy collection update ansible.builtin: Update ansible.builtin collection.
  71. ansible all -b -m modprobe -a "name=dm_mirror state=present": Load dm_mirror module.
  72. ansible-playbook playbook.yml --connection=local: Use local connection.
  73. ansible all -b -m shell -a "echo 'Hello World' > /tmp/hello.txt": Echo text to file.
  74. ansible all -b -m setup -a "filter=ansible_processor_cores": Processor cores info.
  75. ansible all -b -m selinux -a "state=permissive": Set SELinux permissive.
  76. ansible all -b -m timezone -a "name=UTC": Set timezone to UTC.
  77. ansible all -b -m git -a "repo=https://github.com/example/repo.git dest=/opt/repo": Clone git repo.
  78. ansible all -b -m shell -a "/usr/bin/systemctl restart nginx": Restart nginx with systemctl.
  79. ansible all -m ping --forks=20: Ping with 20 forks.
  80. ansible all -b -m file -a "path=/tmp/testdir state=absent": Remove directory.
  81. ansible all -m setup -a "gather_subset=all": Gather all facts.
  82. ansible all -m debug -a "var=hostvars[inventory_hostname]": Debug host vars.
  83. ansible-galaxy role uninstall username.rolename: Uninstall a role.
  84. ansible all -b -m npm -a "name=express global=yes state=present": Install npm package globally.
  85. ansible all -b -m apt_repository -a "repo='ppa:ansible/ansible' state=present": Add apt repository.
  86. ansible all -b -m sysctl -a "name=net.ipv4.ip_forward value=1 state=present": Enable IP forwarding.
  87. ansible-playbook playbook.yml --inventory-file=/path/to/inventory: Specify inventory file.
  88. ansible all -m win_command -a "ipconfig /all": Run ipconfig /all on Windows hosts.
  89. ansible-playbook playbook.yml --tags "deploy": Run tasks tagged 'deploy'.
  90. ansible all -m shell -a "lsb_release -a": Run lsb_release -a on all hosts.
  91. ansible all -m setup -a "filter=ansible_lvm": Gather LVM facts.
  92. ansible all -b -m package -a "name=elasticsearch state=present": Install Elasticsearch.
  93. ansible-playbook playbook.yml --ask-pass: Prompt for SSH password.
  94. ansible all -b -m reboot: Reboot all hosts.
  95. ansible all -m setup -a "gather_subset=!all,hardware": Exclude hardware facts.
  96. ansible all -b -m file -a "dest=/example/file state=file mode=0664": Ensure file exists with permissions.
  97. ansible all -b -m shell -a "useradd -m john": Add user 'john' with home directory.
  98. ansible-playbook playbook.yml --limit "webservers:!phoenix": Exclude 'phoenix' from 'webservers'.
  99. ansible all -m ping --become --become-user root: Ping as root.
  100. ansible all -m setup -a "filter=ansible_memory_mb": Gather memory facts.

No comments:

Post a Comment