1001Ferramentas
๐Ÿ…ฐ๏ธGenerators

Ansible Task Generator

Generate Ansible YAML task with common modules (apt, copy, template, service, file).


  

Ansible tasks in depth: agentless config management, modules, idempotency and playbook patterns

Ansible is an open-source automation engine for configuration management and orchestration. Michael DeHaan released it in 2012; Red Hat acquired the project in 2015. Its big differentiator is being agentless: it connects to managed hosts over SSH (or WinRM on Windows) and runs Python (or PowerShell) snippets ad-hoc โ€” no daemon to install or keep alive. The DSL is plain YAML, the execution model is push (control node drives the targets), and every well-written module is idempotent (running twice produces the same result).

The vocabulary: a playbook is an ordered list of plays; each play maps a group of hosts (from the inventory) to a sequence of tasks; each task invokes a module. Optional pieces: handlers (notified on change), roles (reusable bundles of tasks/handlers/templates/vars), collections (the modern packaging unit, e.g. community.general, kubernetes.core).

Task syntax

- name: Install and start nginx
  hosts: web
  become: true
  tasks:
    - name: Install nginx
      ansible.builtin.apt:
        name: nginx
        state: present
        update_cache: true
      when: ansible_os_family == "Debian"

    - name: Drop config
      ansible.builtin.template:
        src: nginx.conf.j2
        dest: /etc/nginx/nginx.conf
        mode: "0644"
      notify: Restart nginx

  handlers:
    - name: Restart nginx
      ansible.builtin.service:
        name: nginx
        state: restarted

The fully-qualified module name (ansible.builtin.apt instead of bare apt) is the modern style โ€” it pins which collection ships the module and avoids surprises after upgrades.

Core modules you will use every day

  • apt, yum, dnf, package โ€” install packages.
  • file, copy, template โ€” manage files (template uses Jinja2 to render variables).
  • service, systemd โ€” start/stop/enable services.
  • user, group, authorized_key โ€” users and SSH keys.
  • git, unarchive โ€” fetch code and extract archives.
  • lineinfile, blockinfile โ€” surgical edits to existing files (use sparingly; prefer template).
  • command vs shell โ€” shell goes through /bin/sh (pipes, redirects); command does not. Neither is idempotent by default โ€” gate with creates: / removes: or changed_when:.

Variables, templates and secrets

Variables can be defined inline, in vars/, in per-host (host_vars/) or per-group (group_vars/) files, on the command line (--extra-vars), or pulled from inventory plugins. Templates use Jinja2 ({{ var | filter }}, conditionals, loops). For secrets use Ansible Vault (ansible-vault encrypt) โ€” encrypted YAML you can commit safely, decrypted at runtime with a password or a vault-id provider.

Loops, conditionals, tags and check mode

  • loop: โ€” the modern loop keyword (replaces the legacy with_items).
  • when: โ€” run a task only if the expression is true (when: ansible_os_family == "RedHat").
  • tags: โ€” label tasks so --tags deploy runs only that subset.
  • --check โ€” dry run; --diff shows what would change.
  • gather_facts: false โ€” skip the initial fact collection when you don't need it (it's slow).

Best practices

  • Always set name: on every task โ€” the output reads like documentation.
  • Use fully-qualified module names (ansible.builtin.copy) and pin collections in requirements.yml.
  • Prefer module-driven idempotency over command/shell; if you must shell out, set changed_when.
  • Encrypt secrets with Vault โ€” never commit plain passwords.
  • Organize anything reused into a role; share via Ansible Galaxy or a private registry.
  • Test with Molecule + Docker before pushing to prod hosts.
  • Run ansible-lint in CI.

FAQ

Does Ansible work on Windows? Yes, via WinRM (or SSH on Windows Server 2019+). There are dedicated Windows modules (win_package, win_feature, win_service). The control node itself must run on Linux/macOS โ€” Windows is supported only as a target.

Can Ansible manage Kubernetes? Yes, via the kubernetes.core collection (modules k8s, helm, k8s_info). It's a good fit when Kubernetes is one piece of a broader provisioning flow alongside servers and network gear.

Configuration management or orchestration? Both. Plays can be sequential (orchestrate a multi-tier deploy across DB, app, and load balancer) or parallel (apply the same config to a fleet). The serial: keyword controls how many hosts run in parallel during a rolling deploy.

Ansible vs Puppet vs Chef vs Salt? Ansible is push + agentless (SSH). Puppet and Chef are pull + agent-based (long-running daemon on every node). Salt uses ZeroMQ master-minion (fast at scale, more setup). Ansible wins on quick adoption and small fleets; Salt wins on huge fleets needing low-latency events.

What is AWX / Tower / AAP? The browser UI and API around Ansible: scheduled jobs, RBAC, credential vault, surveys, audit logs. AWX is the open-source upstream; Ansible Automation Platform is Red Hat's commercial offering with support.

Related Tools