Wednesday, July 27, 2022

Install Vault on ubuntu 20

 Today I will install vault on ubuntu 20 to use it as a part of my Ansible learning journey 


  • Add GPG key

curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -

  • Add Repository
apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
  • update the apt repo
apt update
  • Install Vault
apt install vault -y
  • start Vault service
 systemctl start vault
  • Make sure that the service started successfully 
 systemctl status vault
  • Enable the service to make sure that it will start automatically after the reboot
systemctl enable vault


after that you will need to determine how many key shares and how many key threshold , I chose 3 and 2 for testing purposes and after that you will download your keys file to be used later in the restore process. 

Wednesday, July 13, 2022

Variables in Ansible

in case we need to install bulk of packages , we will need a variable with the packages names


root@dcex1510ctrl001:~# cat ansible/playbooks/variables.yaml
- name: Ansible playbook to study variables
  hosts: localhost
  vars:
    courses:
      - ansible
      - terraform
      - docker
  tasks:
  - name: Display Value
    debug:
      msg: "{{ courses[0] }}"


root@dcex1510ctrl001:~# ansible-playbook ansible/playbooks/variables.yaml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [Ansible playbook to study variables] ***************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************
ok: [localhost]

TASK [Display Value] *************************************************************************************************************
ok: [localhost] => {
    "msg": "ansible"
}

PLAY RECAP ***********************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0



We will create a variable with kind array and a loop to install some packages

root@dcex1510ctrl001:~/ansible/playbooks/variablesPlaybooks# cat installPackagesLoop.yaml             
- name: Ansible playbook to install 2 packages and remove them through variables and loops
  hosts: client01
  vars:
    packages:
      - apache2
      - mysql-client
  tasks:
  - name: Install the packages
    apt:
      name: "{{ item }}"
      state: present
    loop: "{{ packages }}"
  - name: Remove the packages
    apt:
      name: "{{ item }}"
      state: absent
    loop: "{{ packages }}"


root@dcex1510ctrl001:~/ansible/playbooks/variablesPlaybooks# ansible-playbook -i /root/ansible/hosts/hosts.ini installPackagesLoop.yaml

PLAY [Ansible playbook to install 2 packages and remove them through variables and loops] ****************************************

TASK [Gathering Facts] ***********************************************************************************************************
ok: [10.11.25.67]

TASK [Install the packages] ******************************************************************************************************
ok: [10.11.25.67] => (item=apache2)
changed: [10.11.25.67] => (item=mysql-client)

TASK [Remove the packages] *******************************************************************************************************
changed: [10.11.25.67] => (item=apache2)
changed: [10.11.25.67] => (item=mysql-client)

PLAY RECAP ***********************************************************************************************************************
10.11.25.67                : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0





We can create variable file for ansible like terraform, let's do it here 

root@dcex1510ctrl001:~/ansible/playbooks/variablesPlaybooks# cat vars.yaml
server_name: Test_VM1
server_port: 443


root@dcex1510ctrl001:~/ansible/playbooks/variablesPlaybooks# cat variables.yaml
- name: Ansible playbook to study variables
  hosts: localhost
  tasks:
  - name: Display Server Name
    debug:
      msg: "{{ server_name }}"
  - name: Display Server Name
    debug:
      msg: "{{ server_port }}"



root@dcex1510ctrl001:~/ansible/playbooks/variablesPlaybooks# ansible-playbook -e "@vars.yaml" variables.yaml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [Ansible playbook to study variables] ***************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************
ok: [localhost]

TASK [Display Server Name] *******************************************************************************************************
ok: [localhost] => {
    "msg": "Test_VM1"
}

TASK [Display Server Name] *******************************************************************************************************
ok: [localhost] => {
    "msg": 443
}

PLAY RECAP ***********************************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0



We can add the variable file in our playbook and the playbook will read the variables from it

root@dcex1510ctrl001:~/ansible/playbooks/variablesPlaybooks# cat variables.yaml
- name: Ansible playbook to study variables
  hosts: localhost
  vars_files:
    vars.yaml
  tasks:
  - name: Display Server Name
    debug:
      msg: "{{ server_name }}"
  - name: Display Server Name
    debug:
      msg: "{{ server_port }}"



root@dcex1510ctrl001:~/ansible/playbooks/variablesPlaybooks# ansible-playbook variables.yaml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [Ansible playbook to study variables] ***************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************
ok: [localhost]

TASK [Display Server Name] *******************************************************************************************************
ok: [localhost] => {
    "msg": "Test_VM1"
}

TASK [Display Server Name] *******************************************************************************************************
ok: [localhost] => {
    "msg": 443
}

PLAY RECAP ***********************************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0


Install and Configure Apache

 Install and Configure Apache on one of our clients

root@dcex1510ctrl001:~# cat ansible/playbooks/apacheInstallAndConfigure.yaml
- name: Ansible playbook to install and configure apache2 and restart the service 
  hosts: client01
  tasks:
  - name: Install Apache
    apt:
      name: apache2
      state: present

  - name: Configure apache
    shell: echo "My First Web Page via Ansible" > /var/www/html/index.html
    args:
      executable: /bin/bash
    notify:
      - Reload Apache
  handlers:
   - name: Reload Apache
     service:
       name: apache2
       state: reloaded


ansible-playbook -i ansible/hosts/hosts.ini ansible/playbooks/apacheInstallAndConfigure.yaml


The usage of the handler is to restart the service. if we will restart the same service several time , we can use handlers one time at the end of the playbook to restart the service only one time

Tuesday, July 12, 2022

Playbook to upgrade and update ubuntu servers

 from time to time we need to update and upgrade our linux servers, so I think this is a real case scenario


root@dcex1510ctrl001:~# cat ansible/playbooks/updateUpgrade.yaml
- name: Ansible playbook to update and upgrade all packages
  hosts: all
  tasks:
    - name: Update apt repo and cache on the clients
      apt:
        update_cache: yes
        force_apt_get: yes
        cache_valid_time: 3600
    - name: Upgrade all packages on the clients
      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

    - name: Reboot the server 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


The first task will update the packages and force the client to use apt-get instead of using aptitude

The second task will upgrade the packages 

The third task will check this file /var/run/reboot-required and if it exists in the clients, then ansible will restart this client because it requires a reboot.

The fourth task will reboot the client based on the status of the file status which we get from the third task. please note that ansible will differentiate between the status of this file for each client. so if this file exists only in the first client, so ansible will reboot the first client only and it will not reboot the second client


root@dcex1510ctrl001:~#  ansible-playbook -i /root/ansible/hosts/hosts.ini /root/ansible/playbooks/updateUpgrade.yaml

PLAY [Ansible playbook to update and upgrade all packages] ***********************************************************************************

TASK [Gathering Facts] ***********************************************************************************
ok: [10.11.25.67]
ok: [10.11.25.68]

TASK [Update apt repo and cache on the clients] ***********************************************************************************
changed: [10.11.25.67]
changed: [10.11.25.68]

TASK [Upgrade all packages on the clients] ***********************************************************************************
changed: [10.11.25.67]
changed: [10.11.25.68]

TASK [Check if a reboot is needed on all servers] ***********************************************************************************
ok: [10.11.25.68]
ok: [10.11.25.67]

TASK [Reboot the server if kernel updated] ***********************************************************************************
changed: [10.11.25.67]
changed: [10.11.25.68]

PLAY RECAP ***********************************************************************************
10.11.25.67       : ok=5    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
10.11.25.68       : ok=5    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0




Monday, July 11, 2022

My first Playbooks

 in the next blog we will start to create some simple playbooks to do the following:

1. transfer a new hosts file to the clients 

2. install some packages on the clients


Transfer a new hosts file to the clients:

1. I created the hosts file that I will transfer to to the clients.

root@dcex1510ctrl001:~# cat /root/files/hosts
127.0.0.1 localhost
10.11.25.66 dcex1510ctrl001
10.11.25.67 dcex1510clnt001
10.11.25.68 dcex1510clnt002


2. I created the playbook itself

root@dcex1510ctrl001:~# cat ansible/playbooks/hostsfile.yaml
- name: Ansible copy a file from the master node to the clients
  hosts: client01
  tasks:
    - name: Copying hosts file to another server
      become: true
      copy:
        src: /root/files/hosts
        dest: /etc/hosts
        owner: root
        group: root
        mode: 0644

3. I executed the playbook

ansible-playbook -i ansible/hosts/hosts.ini ansible/playbooks/hostsfile.yaml


Just a reminder for my hosts file 

root@dcex1510ctrl001:~# cat ansible/hosts/hosts.ini
[client01]
10.11.25.67 ansible_user=root ansible_password=ABCD1234;

[client02]
10.11.25.68 ansible_user=root ansible_password=ABCD1234;

[all:children]
client01
client02

4. As we need to make the same hosts file in the 2 clients, I will change the hosts in the above playbook

root@dcex1510ctrl001:~# cat ansible/playbooks/hostsfile.yaml
- name: Ansible copy a file from the master node to the clients
  hosts: all
  tasks:
    - name: Copying hosts file to another server
      become: true
      copy:
        src: /root/files/hosts
        dest: /etc/hosts
        owner: root
        group: root
        mode: 0644


Install and remove a package on the clients:

root@dcex1510ctrl001:/# cat /root/ansible/playbooks/apacheInstall.yaml
- name: Ansible playbook to install and remove apache package, it is used just for testing a simple playbook
  hosts: all
  tasks:
  - name: Install Apache
    apt:
      name: apache2
      state: present
  - name: Remove Apache
    apt:
     name: apache2
     state: absent


root@dcex1510ctrl001:/# ansible-playbook -i /root/ansible/hosts/hosts.ini /root/ansible/playbooks/apacheInstall.yaml

PLAY [Ansible playbook to install and remove apache package, it is used just for testing a simple playbook] ******************************************************************

TASK [Gathering Facts] ***********************************************************************************
ok: [10.11.25.67]
ok: [10.11.25.68]

TASK [Install Apache] ***********************************************************************************
changed: [10.11.25.68]
changed: [10.11.25.67]

TASK [Remove Apache] ***********************************************************************************
changed: [10.11.25.68]
changed: [10.11.25.67]

PLAY RECAP ***********************************************************************************
10.11.25.67        : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
10.11.25.68        : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0


Ansible ad hoc commands

 Without a playbook you can do some simple actions from the ad hoc commands like the below examples:


ansible all -i ansible/hosts/hosts.ini -m shell -a "/sbin/reboot"


ansible all -i ansible/hosts/hosts.ini -m shell -a "/sbin/reboot" -f 2    

# this will make the reboot in parallel and we can increase the number for more servers in the hosts file


ansible all -i ansible/hosts/hosts.ini -m shell -a "cat /etc/hosts /etc/hostname"


ansible all -i ansible/hosts/hosts.ini  -m shell -a "uptime" 


ansible client01 -i ansible/hosts/hosts.ini  -m shell -a "free -m"


ansible all -i ansible/hosts/hosts.ini -m service -a "name=ssh"


root@dcex1510ctrl001:~# ansible client01 -i ansible/hosts/hosts.ini -m shell -a "hostnamectl set-hostname dcex1510clnt001"

10.X.Y.Z | CHANGED | rc=0 >>


root@dcex1510ctrl001:~# ansible client02 -i ansible/hosts/hosts.ini -m shell -a "hostnamectl set-hostname dcex1510clnt002"

10.A.B.C | CHANGED | rc=0 >>



Ensure a service is started on client01 host:

ansible client01 -i ansible/hosts/hosts.ini -m ansible.builtin.service -a "name=ssh state=started"


Alternatively, restart a service on client01 host:

ansible client01 -i ansible/hosts/hosts.ini -m ansible.builtin.service -a "name=ssh state=restarted"


Ensure a service is stopped:

ansible client01 -i ansible/hosts/hosts.ini -m ansible.builtin.service -a "name=ssh state=stopped"



 in the next blog we will start to create some simple playbooks to do the following:

1. transfer a new hosts file to the clients 

2. change the client hostname

3. install some packages on the clients

Grouping in the ansible hosts file

 We will add a group to have my 2 clients



cat ansible/hosts/hosts.ini

[client01]

10.x.y.z ansible_user=root ansible_password=ABC123


[client02]

10.a.b.c ansible_user=root ansible_password=XYZ123


[all:children]

client01

client02


ansible all -i ansible/hosts/hosts.ini -m ping

10.x.y.z  | SUCCESS => {

    "ansible_facts": {

        "discovered_interpreter_python": "/usr/bin/python3"

    },

    "changed": false,

    "ping": "pong"

}

10.a.b.c | SUCCESS => {

    "ansible_facts": {

        "discovered_interpreter_python": "/usr/bin/python3"

    },

    "changed": false,

    "ping": "pong"

}


Install Vault on ubuntu 20

 Today I will install vault on ubuntu 20 to use it as a part of my Ansible learning journey  Add GPG key curl -fsSL https://apt.releases.has...