Friday, March 10, 2023

How to use group_vars with Ansible role


We have these 6 servers in hosts file:




We want Bind9 to be installed only on dns_servers
We want MariaDB to be installed only on db_servers
We want Nginx to be installed only on web_servers
We want user 'notadmin' to be created on all servers

For this example I will use Debian based servers.
The hosts file (see above) is copied in current working directory
First we create ansible role named "using-ansible-group-vars-example:

$ ansible-galaxy init using-group-vars-example

Then we create file playbook-using-group-vars-example.yml in the current working directory with the following content:
- name: How to use group_vars example
  hosts: all
  remote_user: admin
  become: yes
        - ./using-group-vars-example

Note: all remote hosts should be configure according Ansible documentation for this to work (ssh-copy-id and user "admin" in the /etc/sudoers with access to sudo with no password).

Then we create group_vars directory again in the current working directory:

$ mkdir group_vars

Now we have directory structure like this:

Create the following files inside group_vars/ directory with the names of the server groups from our hosts file


install_mariadb: "true"
create_user: "true"

create_user: "true"
install_dns: "true"

install_nginx: "true"
create_user: "true"

Content of the using-group-vars-example/defaults/main.yml should be this:
# defaults file for using-group-vars-example
# we must initialize these otherwise it will pop an error
create_user: none
install_nginx: none
install_mariadb: none
install_dns: none

Content of the using-group-vars-example/tasks/main.yml should be this:

# tasks file for using-group-vars-example
- name: install nginx
  include_tasks: nginx.yml
  when: install_nginx == "true"

- name: create user
  include_tasks: user.yml
  when: create_user == "true"

- name: install dns server
  include_tasks: dns.yml
  when: install_dns == "true"

- name: install mariadb server
  include_tasks: db.yml
  when: install_mariadb == "true"

Now we create 4 more files inside the using-group-vars-example/tasks/ as follow:

- name: Install BIND9
    name: bind9 bind9-utils
    state: present
    update_cache: yes


- name: Install MariaDB server
    name: mariadb-server mariadb-server-core
    state: present
    update_cache: yes

- name: Update the repository cache and update package "nginx"
    name: nginx
    state: present
    update_cache: yes

- name: Add the user 'notadmin'
    name: notadmin
    state: present
    comment: notadmin user for testing purposes
    createhome: yes
    home: /home/notadmin

These will be included only when declared in group_vars/ yml files

Now the final structure should look like this:

now we run this from the . directory like this:

$ ansible-playbook playbook-using-group-vars-example.yml -i hosts

Monday, February 27, 2023

Extracting single directory from tar.gz archive to a specific destination directory

archive-1.05.tar.gz contains the following:


We want to unarchive only contents of the "archive-1.05/lib/" to a specific destination (/home/user/lib)

$ tar -xvzf archive-1.05.tar.gz  -C /home/user/lib --strip-components=1 --no-anchored lib

If we do not specify --no-anchored flag then --strip-components will not work.