DEV Community

Lam
Lam

Posted on

5 2

Ansible Cheat Sheet

References

[Roles] Env vars

    vars:
      local_home: "{{ lookup('env','HOME') }}"
Enter fullscreen mode Exit fullscreen mode

[Roles] Task: Failures

    - name: my task
      command: ...
      register: result
      failed_when: "'FAILED' in result.stderr"

      ignore_errors: yes

      changed_when: "result.rc != 2"
Enter fullscreen mode Exit fullscreen mode

Roles

    - host: xxx
      roles:
        - db
        - { role:ruby, sudo_user:$user }
        - web

    # Uses:
    # roles/db/tasks/*.yml
    # roles/db/handlers/*.yml
Enter fullscreen mode Exit fullscreen mode

Vars

    - host: lol
      vars_files:
        - vars.yml
      vars:
        project_root: /etc/xyz
      tasks:
        - name: Create the SSH directory.
          file: state=directory path=${project_root}/home/.ssh/
          only_if: "$vm == 0"
Enter fullscreen mode Exit fullscreen mode

Handlers

    handlers:
      - name: start apache2
        action: service name=apache2 state=started

    tasks:
      - name: install apache
        action: apt pkg=apache2 state=latest
        notify:
          - start apache2
Enter fullscreen mode Exit fullscreen mode

[Tasks] Includes

    tasks:
      - include: db.yml
    handlers:
      - include: db.yml user=timmy
Enter fullscreen mode Exit fullscreen mode

Tasks

    - hosts: all
      user: root
      sudo: no
      vars:
        aaa: bbb
      tasks:
        - ...
      handlers:
        - ...
Enter fullscreen mode Exit fullscreen mode

[Getting started] Running a playbook

    $ ansible-playbook playbook.yml
Enter fullscreen mode Exit fullscreen mode

[Getting started] Hosts

    $ sudo mkdir /etc/ansible
    $ sudo vim /etc/ansible/hosts

    [example]
    192.0.2.101
    192.0.2.102
Enter fullscreen mode Exit fullscreen mode

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay