Change Wallpaper on Gnome Desktop’s using Ansible

Setup Wallpaper on Ubuntu using Ansible

Today, In this article we will setup the Gnome Desktop setting using Ansible Playbook.

1) Setting up inventory file for remote hosts.

For the inventory, I created a file hosts and filled it with following content details.

/etc/ansible# cat hosts
server1 ansible_host=172.16.1.1 ansible_user=gaurav ansible_ssh_pass=123 ansible_connection=ssh
server2 ansible_host=172.16.1.3 ansible_user=gaurav ansible_ssh_pass=123 ansible_connection=ssh 
server3 ansible_host=172.16.1.1 ansible_user=gaurav ansible_ssh_pass=123 ansible_connection=ssh
[remoteservers]
server1
server2
server3

2) Script to change the wallpaper on remote machine.

This script changes the wallpaper from a given directory on remote systems.

#! /bin/bash
DOLUSER="gaurav"
SRC_URI="http://getwallpapers.com/wallpaper/full/6/f/2/724421-amazing-ubuntu-wallpaper-hd-2560x1600-for-samsung.jpg"
FNAME="/home/$DOLUSER/Pictures/black.jpg"
wget "$SRC_URI" -O "$FNAME"
DNAME='file:///home/'$DOLUSER'/Pictures/black.jpg'
PID=$(pgrep gnome-session)
export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ|cut -d= -f2-)
sleep 5
/usr/bin/gsettings set org.gnome.desktop.background picture-uri $DNAME

3) Setting up playbook.

In third step I created the playbook (i.e playbook.yaml) and add hosts (ansible run this playbook for all available hosts).
I set ‘become’ to true to activate privilege escalation and use ‘become_method’ as a sudo (In our case ‘gaurav’ is the sudo user).

-
  name: 'Change Wallpaper on Gnome Desktop's using Ansible'
  hosts: remoteservers
  become: True
  become_method: sudo
  tasks:
    - name: 'create a directory for Wallpaper'
      file:
        path: /home/gaurav/Pictures/.wallpaper/
        state: directory
        owner: gaurav
        group: gaurav
        mode: '0775' 
    - name: 'Copy wallpaper to remote systems'
      copy:
        src: /etc/ansible/playbook/wallpaperscript.sh
        dest: /home/gaurav/Pictures/.wallpaper/
        owner: gaurav
        group: gaurav
        mode: '0777'
    - name: 'Change wallpaper on remote system'
      cron:
        name: 'wallpaper change'
        minute: "*"
        hour: "*/3"
        day: "*"
        month: "*"
        weekday: "*"
        job: /home/gaurav/Pictures/.wallpaper/wallpaperscript.sh
        user: gaurav
        state: present
      tags:
        - cronjobs  
      become: False

In this playbook, First copy the script i.e “wallpaperscript.sh” to “/home/gaurav/Pictures/.wallpaper/” directory on remote servers and set the cron that will execute every 3 hours on remote system.

3) Execute the playbook.

Execute the playbook in ansible server using below command.

root@ansible:/etc/ansible/playbook# ansible-playbook playbook.yaml --ask-become-pass

Hope this post will help Devops beginners. Please share you feedback and Comments. Stay tune for more updates with ittroubleshooter.in …!!!

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of