UncleNUC Wiki

Second chance for NUCs

User Tools

Site Tools


lab:ansible_virtualbox_autoboot_linux:test_the_first_ubuntu_vm_on_ansible

This is an old revision of the document!


Test the First Ubuntu VM on Ansible

In this step we will test the basic connectivity to our new server.

Test SSH Access

  1. Log in to your host machine and become the user ansible
    • Option 1 - ssh to your host machine as the user ansible
    • Option 2 - log in as usual and run su - ansible
  2. SSH to the IP address of the new server
    • ssh <ip_address>
      • The IP address is displayed on the VM's console screen when it starts up
      • Another option is to look at your router's DHCP clients list
      • Yet another another option is nmap
  3. The login succeeds without entering a password (you do need accept the fingerprint to add to the list of known hosts)
    • Will use the user ansible to manage the server

Test Ansible to the New Server

Create the Inventory File

  1. Become user ansible (i.e., su - ansible)
  2. Create a simple inventory file named hosts in the ansible user's home directory
    • Put the VM's IP address in the file, following the template below
    • [servers]
      <ip_address_of_VM>
  3. Confirm the inventory by listing it to the screen
    • ansible -i hosts all --list-hosts

Confirm the Server is Up

Still as user ansible, run some ad hoc commands to test:

  • ansible -i hosts all -m ping
  • ansible -i hosts servers -m ping
  • ansible -i hosts servers -a “sudo /sbin/reboot”

Update Ubuntu

Next we will use a playbook to update the software packages on the new server.

update_servers.yml
---
- hosts: servers
  become: true
  become_user: root
  tasks:
    - name: Update apt repo and cache on all Debian/Ubuntu boxes
      apt: update_cache=yes force_apt_get=yes cache_valid_time=3600

    - name: Upgrade all packages on servers
      apt: upgrade=dist force_apt_get=yes

    - name: Check if a reboot is needed on all servers
      register: reboot_required_file
      stat: path=/var/run/reboot-required get_md5=no

    - name: Reboot the box if kernel updated
      reboot:
        msg: "Reboot initiated by Ansible for kernel updates"
        connect_timeout: 5
        reboot_timeout: 300
        pre_reboot_delay: 0
        post_reboot_delay: 30
        test_command: uptime
      when: reboot_required_file.stat.exists

Run the playbook: ansible-playbook -i hosts update_servers.yml

Disable DNS Stub Resolver

Even though each node receives its DNS information via DHCP, Ubuntu 22.04 will at times fail to resolve names. Rebooting solves the problem temporarily, but it will come back. The following playbook will disable the DNS Stub listener to prevent the problem.

disable_dns_stub.yml
---
- name: Disable DNS stub listener
  hosts: servers
  remote_user: ansible
  become: true
  tasks:
    - name: Disable DNS stub listener
      ini_file: dest=/etc/systemd/resolved.conf section=Resolve option=DNSStubListener value=no backup=yes
      tags: configuration
    - name: Restart NetworkManager
      systemd:
        name: NetworkManager
        state: restarted
    - name: Restart systemd-resolved
      systemd:
        name: systemd-resolved
        state: restarted
    - name: daemon-reload
      systemd:
        daemon_reload: true

Run the playbook: ansible-playbook -i hosts disable-dns-stub.yml

Next Step

Congratulations on your first VM deployed using Ansible on Virtualbox! Move on to the next where we Delete the first Ubuntu VM using Ansible.

Or, back to Deploy Ubuntu unattended install to Oracle Virtualbox using Ansible

Optional

Here is an example of a playbook to reboot the servers in the group [servers].

reboot_servers.yml
---
- name: Reboot servers
  hosts: servers
  remote_user: ansible
  become: true
  tasks:
    - name: Reboot the box
      reboot:
        msg: "Reboot initiated by Ansible"
        connect_timeout: 5
        reboot_timeout: 300
        pre_reboot_delay: 0
        post_reboot_delay: 30
        test_command: uptime
    - name: Check if server is up
      ping:

What do you think this playbook will do? Open the VM's console and watch what happens when you run ansible-playbook -i hosts reboot_servers.yml. When does the reboot task complete?

What if you had 5 servers to reboot. Does this script wait for the each server to come up before rebooting the next? What would a playbook that does that look like? https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_strategies.html

lab/ansible_virtualbox_autoboot_linux/test_the_first_ubuntu_vm_on_ansible.1705636035.txt.gz · Last modified: 2024/01/19 03:47 by user