Home Ansible Examples
Post
Cancel

Ansible Examples

Ad-hoc commands

ad hoc commands are great for tasks you repeat rarely. For example, if you want to power off all the machines in your lab for Christmas vacation, you could execute a quick one-liner in Ansible without writing a playbook.

An example:

1
sudo ansible all -b -a "poweroff"

Using playbooks is a better way.

Playbooks

List of Ansible modules: https://docs.ansible.com/ansible/2.9/modules/list_of_all_modules.html

Update and Upgrade (Ubuntu)

1
2
3
4
5
6
7
- hosts: "*"
  become: yes
  tasks:
    - name: apt
      apt:
        update_cache: yes
        upgrade: 'yes'

Update and Upgrade (Red Hat)

1
2
3
4
5
6
7
- hosts: "*"
  become: yes
  tasks:
    - name: dnf
      dnf:
        name: "*"
        state: latest

Set the timezone

1
2
3
4
5
- name: Set timezone to New York
  hosts: "*"
  tasks:
  - name: set timezone
    shell: timedatectl set-timezone America/New_York

Copy over a file

1
2
- name: Copy over config file
  template: src=/etc/systemd/timesyncd.conf dest=/etc/systemd/timesyncd.conf

Add the EPEL repo and install the htop utility (Red Hat)

1
2
3
4
5
6
7
8
9
10
11
12
- hosts: "*"
  become: yes
  tasks:
    - name: Add EPEL
      dnf:
        name: "https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm"
        state: present
        disable_gpg_check: true
    - name: Add htop
      dnf:
        name: "htop"
        state: present
This post is licensed under CC BY 4.0 by the author.