Ansible: Setting up a Basic Lab


This article outlines the process for managing a switch using Ansible in a lab environment. This example is showing off Ansible's ad-hoc commands. For repeated tasks, use an Ansible playbook; like this easy playbook example.



  • One switch running Cumulus Linux (any version), called switch1
  • A server or virtual machine running Debian, with a basic installation (default packages only), called server1

On the server the following must be set up.

  • DHCP
  • Ansible

Network addresses:

For this example the subnet is being used. the existing gateway server1 switch1;

Configuring server1

Configuring a DHCP server on the server

  1. Install the required packages:
    root@server:~# apt-get install isc-dhcp-server
  2. Assign the static IP address to server1. Edit /etc/network/interfaces so it looks like:
    auto lo
    iface lo inet loopback

    auto eth0
    iface eth0 inet static
  3. Configure the DHCP scope. Edit /etc/dhcp/dhcpd.conf and add this to the file:
    subnet netmask {
        option routers;
        option domain-name-server;
        option domain-name ""; host switch1 { hardware ethernet 00:JJ:YU:38:AC:45; fixed-address; }
  4. Restart server1.
  5. Reboot switch1.
  6. Confirm that switch1 is reachable from server1:
    root@server:~# ping 
    64 bytes from icmp_req=2 ttl=64 time=0.141 ms

Installing Ansible

# apt-get install python-pip
# pip install ansible

Configuring a MOTD via Ansible

In this section, you will create a simple message of the day (MOTD) using Ansible.

  1. Create a sample MOTD file:
    # echo "SAMPLE MOTD" > ~/sample.motd
  2. Create an inventory file with switch1 in the list:
    # echo "" > ~/ansible.hosts
  3. Push the sample MOTD to switch1 ( Enter the password for the cumulus user (the default user):
    #ansible -k -K -u cumulus -i ~/ansible.hosts -m 'copy' -a 'src=~/sample.motd dest=/etc/motd'
    SSH password:
    sudo password [defaults to SSH password]:
    sw1 | success >> {
        "changed": false,
        "dest": "/etc/motd",
        "gid": 0,
        "group": "root",
        "md5sum": "b04dde3768174a34f75fdde78142849d",
        "mode": "0644",
        "owner": "root",
        "path": "/etc/motd",
        "size": 12,
        "state": "file",
        "uid": 0

See Also:

Have more questions? Submit a request


  • Avatar
    Eoin Kenny

    Should step 3 not just be:
    ansible -k -K -u cumulus -i ~/ansible.hosts -m 'copy' -a 'src=~/sample.motd dest=/etc/motd'

Powered by Zendesk