Showing posts with label Ansible. Show all posts
Showing posts with label Ansible. Show all posts

Saturday, April 27, 2024

Automating Web Deployment with Ansible: A Step-by-Step Guide to Using Playbooks for Nginx

Automating Web Deployment with Ansible: A Step-by-Step Guide to Using Playbooks for Nginx

Ansible Playbook Example: Deploying a Web Application


---
- name: Deploy Web Application
  hosts: webservers
  become: yes
  vars:
    http_port: 80
    max_clients: 200
  tasks:
    - name: Install nginx web server
      apt:
        name: nginx
        state: present
        update_cache: yes

    - name: Upload the nginx configuration file
      template:
        src: templates/nginx.conf.j2
        dest: /etc/nginx/nginx.conf
      notify:
        - restart nginx

    - name: Ensure nginx is running
      service:
        name: nginx
        state: started
        enabled: true

    - name: Copy website files
      copy:
        src: /src/webfiles/
        dest: /var/www/html/
        owner: www-data
        group: www-data
        mode: 0644

  handlers:
    - name: restart nginx
      service:
        name: nginx
        state: restarted

Explanation of the Playbook:

  • Hosts: Targets the webservers group, which should be defined in your inventory file.
  • Become: Executes tasks with administrative (sudo) privileges.
  • Vars: Defines variables for use within the playbook. http_port and max_clients can be used in configuration templates.
  • Tasks:
    • Install nginx: Uses the apt module to install Nginx on Debian-based systems. Ensures the package cache is updated before installation.
    • Upload the nginx configuration file: Uses the template module to push a customized nginx configuration from a local template (nginx.conf.j2).
    • Ensure nginx is running: Ensures that the Nginx service is started and enabled to start on boot using the service module.
    • Copy website files: Transfers files from a local directory to the web server’s root directory. Sets the appropriate permissions and ownership.
  • Handlers:
    • restart nginx: Defined to restart Nginx whenever the nginx configuration file changes. This is triggered by the notify directive in the template task.

This playbook is a basic example for deploying a static web application using Nginx. It demonstrates the use of modules like apt, template, service, and copy, which are commonly used in real-world scenarios to manage web servers and deploy applications. Handlers are particularly useful for restarting services only when necessary, optimizing resource usage during playbook runs.

Mastering Ansible: 10 Essential Playbook Examples for Automating Your IT Infrastructure

Mastering Ansible: 10 Essential Playbook Examples for Automating Your IT Infrastructure


1. Ping Module

This playbook pings all servers in the all group.

- name: Test connectivity hosts: all tasks: - name: Ping all hosts ping:

2. Install a Package

Installs the "nginx" package on all Debian-based servers.

- name: Install Nginx hosts: all become: yes tasks: - name: Install nginx apt: name: nginx state: present

3. Copy File

Copies a local file to remote hosts.

- name: Copy file to hosts hosts: all tasks: - name: Copy sample file copy: src: /localpath/sample.txt dest: /remotepath/sample.txt

4. Create a User

Creates a user on all servers.

- name: Create a user hosts: all become: yes tasks: - name: Ensure user 'johndoe' is present user: name: johndoe state: present

5. Manage Services

Starts and enables the "httpd" service on CentOS servers.

- name: Manage Httpd Service hosts: all become: yes tasks: - name: Start and enable httpd service: name: httpd state: started enabled: true

6. Execute Commands

Runs a shell command on all servers.

- name: Execute a command hosts: all tasks: - name: List contents of directory command: ls -l /some/directory

7. Gather Facts

Gathers facts about the hosts without any specific task.

- name: Gather facts hosts: all tasks: - name: Gather facts from hosts setup:

8. Conditional Tasks

Executes a task based on a condition.

- name: Conditional task hosts: all tasks: - name: Install httpd on CentOS 7 yum: name: httpd state: latest when: ansible_distribution == 'CentOS' and ansible_distribution_major_version == '7'

9. Loop Example

Installs multiple packages using a loop.

- name: Install multiple packages hosts: all become: yes tasks: - name: Install packages apt: name: "{{ item }}" state: present loop: - vim - git

10. Template Module

Deploys a configuration file from a Jinja2 template.

- name: Deploy template hosts: all tasks: - name: Deploy a template file template: src: /localpath/template.j2 dest: /remotepath/config.conf


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.