0% found this document useful (0 votes)
31 views14 pages

Using The Setup Module

Uploaded by

comp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views14 pages

Using The Setup Module

Uploaded by

comp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 14

USING THE SETUP MODULE:

- So, we discussed about how to automate backups.

- & we also discussed that, if we take a backup, there should be mentioned


date and time.

- So, in this topic we will try to achieve those requirements.

- So, for that purpose, we're going to use a SETUP MODULE.

- Web browser:

- "ansible setup" in google.com

- "Ansible.builtin.setup module"

- Scroll down and you can see some of the examples shown in:

1. gather_subset:
2. filter:

1. CREATING A SMALL PLAYBOOK: (USING GATHER SUBSET):

- Just to see how SETUP Module works, so we're going to try it on our
localhost.

[root@localhost My-Automation-Stuff2]# pwd


/home/trainonic/My-Automation-Stuff2

[root@localhost My-Automation-Stuff2]# touch example11-setup.yml

[root@localhost My-Automation-Stuff2]# cat example11-setup.yml


---

- name: "Testing Playbook"


hosts: localhost

tasks:
- name: "Collection facts from the localhost"
ansible.builtin.setup:
gather_subset:
- "min"
register: output

- name: "Print the output"


debug:
msg: "{{ output }}"

- Save and quit the playbook

2. RUNNING THE PLAYBOOK: (Gathers and Shows you tons of information)

[root@localhost My-Automation-Stuff2]# ansible-playbook example11-setup.yml

PLAY [Testing Playbook]


**************************************************************************

TASK [Collection facts from the localhost]


*******************************************************
ok: [localhost]

TASK [Print the output]


**************************************************************************
ok: [localhost] => {
"msg": {
"ansible_facts": {
"ansible_apparmor": {
"status": "disabled"
},
"ansible_architecture": "x86_64",
"ansible_cmdline": {
"BOOT_IMAGE": "(hd0,msdos1)/vmlinuz-5.14.0-362.18.1.el9_3.x86_64",
"crashkernel": "1G-4G:192M,4G-64G:256M,64G-:512M",
"quiet": true,
"rd.lvm.lv": "rhel/swap",
"resume": "/dev/mapper/rhel-swap",
"rhgb": true,
"ro": true,
"root": "/dev/mapper/rhel-root"
},
"ansible_date_time": { (Focus on this)
"date": "2024-01-30",
"day": "30",
"epoch": "1706596334",
"epoch_int": "1706596334",
"hour": "12",
"iso8601": "2024-01-30T06:32:14Z",
"iso8601_basic": "20240130T120214737684",
"iso8601_basic_short": "20240130T120214",
"iso8601_micro": "2024-01-30T06:32:14.737684Z",
"minute": "02",
"month": "01",
"second": "14",
"time": "12:02:14",
"tz": "IST",
"tz_dst": "IST",
"tz_offset": "+0530",
"weekday": "Tuesday",
"weekday_number": "2",
"weeknumber": "05",
"year": "2024"
},
"ansible_distribution": "RedHat",
"ansible_distribution_file_parsed": true,
"ansible_distribution_file_path": "/etc/redhat-release",
"ansible_distribution_file_search_string": "Red Hat",
"ansible_distribution_file_variety": "RedHat",
"ansible_distribution_major_version": "9",
"ansible_distribution_release": "Plow",
"ansible_distribution_version": "9.3",
"ansible_dns": {
"nameservers": [
"10.10.10.2"
],
"search": [
"localdomain"
]
},
"ansible_domain": "localdomain",
"ansible_effective_group_id": 0,
"ansible_effective_user_id": 0,
"ansible_env": {
"BASH_FUNC_which%%": "() { ( alias;\n eval ${which_declare} ) |
/usr/bin/which --tty-only --read-alias --read-functions --show-tilde --show-dot $@\
n}",
"COLORTERM": "truecolor",
"DBUS_SESSION_BUS_ADDRESS": "unix:path=/run/user/1000/bus",
"DESKTOP_SESSION": "gnome",
"DISPLAY": ":0",
"GDMSESSION": "gnome",
"GDM_LANG": "en_IN.UTF-8",
"GNOME_SETUP_DISPLAY": ":1",
"GNOME_TERMINAL_SCREEN":
"/org/gnome/Terminal/screen/ac3be9d9_f590_4207_a66e_7e097086a12d",
"GNOME_TERMINAL_SERVICE": ":1.87",
"HISTCONTROL": "ignoredups",
"HISTSIZE": "1000",
"HOME": "/root",
"HOSTNAME": "localhost",
"LANG": "en_IN.UTF-8",
"LESSOPEN": "||/usr/bin/lesspipe.sh %s",
"LOGNAME": "trainonic",
"LS_COLORS":
"rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or
=40;31;01:mi=01;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;3
2:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;
31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=0
1;31:*.z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:
*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31
:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;3
1:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;3
1:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=0
1;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tg
a=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.
svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35
:*.mkv=01;35:*.webm=01;35:*.webp=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=0
1;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=0
1;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01
;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=0
1;36:*.au=01;36:*.flac=01;36:*.m4a=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3
=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.oga=01;36:*.opus=01;36:*.sp
x=01;36:*.xspf=01;36:",
"MAIL": "/var/spool/mail/trainonic",
"OLDPWD": "/home/trainonic",
"PATH":
"/root/.local/bin:/root/bin:/home/trainonic/.local/bin:/home/trainonic/bin:/usr/
local/bin:/usr/local/sbin:/usr/bin:/usr/sbin",
"PWD": "/home/trainonic/My-Automation-Stuff2",
"QT_IM_MODULE": "ibus",
"SESSION_MANAGER":
"local/unix:@/tmp/.ICE-unix/21474,unix/unix:/tmp/.ICE-unix/21474",
"SHELL": "/bin/bash",
"SHLVL": "3",
"SSH_AUTH_SOCK": "/run/user/1000/keyring/ssh",
"SYSTEMD_EXEC_PID": "21611",
"TERM": "xterm-256color",
"USER": "trainonic",
"USERNAME": "trainonic",
"VTE_VERSION": "6402",
"WAYLAND_DISPLAY": "wayland-0",
"XAUTHORITY": "/root/.xauthcO2yqp",
"XDG_CURRENT_DESKTOP": "GNOME",
"XDG_DATA_DIRS":
"/root/.local/share/flatpak/exports/share:/home/trainonic/.local/share/flatpak/
exports/share:/var/lib/flatpak/exports/share:/usr/local/share/:/usr/share/",
"XDG_MENU_PREFIX": "gnome-",
"XDG_RUNTIME_DIR": "/run/user/1000",
"XDG_SESSION_CLASS": "user",
"XDG_SESSION_DESKTOP": "gnome",
"XDG_SESSION_TYPE": "wayland",
"XMODIFIERS": "@im=ibus",
"_": "/usr/bin/python3",
"which_declare": "declare -f"
},
"ansible_fips": false,
"ansible_fqdn": "localhost.localdomain",
"ansible_hostname": "localhost",
"ansible_kernel": "5.14.0-362.18.1.el9_3.x86_64",
"ansible_kernel_version": "#1 SMP PREEMPT_DYNAMIC Wed Jan 3 15:54:45
EST 2024",
"ansible_local": {},
"ansible_lsb": {},
"ansible_machine": "x86_64",
"ansible_machine_id": "850a99d0a0554af98347bbb39d83d259",
"ansible_nodename": "localhost.localdomain",
"ansible_os_family": "RedHat",
"ansible_pkg_mgr": "dnf",
"ansible_proc_cmdline": {
"BOOT_IMAGE": "(hd0,msdos1)/vmlinuz-5.14.0-362.18.1.el9_3.x86_64",
"crashkernel": "1G-4G:192M,4G-64G:256M,64G-:512M",
"quiet": true,
"rd.lvm.lv": [
"rhel/root",
"rhel/swap"
],
"resume": "/dev/mapper/rhel-swap",
"rhgb": true,
"ro": true,
"root": "/dev/mapper/rhel-root"
},
"ansible_python": {
"executable": "/usr/bin/python3",
"has_sslcontext": true,
"type": "cpython",
"version": {
"major": 3,
"micro": 18,
"minor": 9,
"releaselevel": "final",
"serial": 0
},
"version_info": [
3,
9,
18,
"final",
0
]
},
"ansible_python_version": "3.9.18",
"ansible_real_group_id": 0,
"ansible_real_user_id": 0,
"ansible_selinux": {
"config_mode": "enforcing",
"mode": "enforcing",
"policyvers": 33,
"status": "enabled",
"type": "targeted"
},
"ansible_selinux_python_present": true,
"ansible_service_mgr": "systemd",
"ansible_ssh_host_key_ecdsa_public":
"AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBOK8ablTS6ZBNUOQq3dKzJ/
VY0ZwvZOiO/g5hu95hsAb+x0L6B8UMtlyHbJA6vExi0kR/e96IQrIEBCb4YDqZ3k=",
"ansible_ssh_host_key_ecdsa_public_keytype": "ecdsa-sha2-nistp256",
"ansible_ssh_host_key_ed25519_public":
"AAAAC3NzaC1lZDI1NTE5AAAAIO6qoq+Kyr9jZDGmzvtIsQUNgrcnl8EXwcHRgKF7/+qJ",
"ansible_ssh_host_key_ed25519_public_keytype": "ssh-ed25519",
"ansible_ssh_host_key_rsa_public":
"AAAAB3NzaC1yc2EAAAADAQABAAABgQCa79Nz1lslLv1ErIXkNzcc0iS/h9XZxlaIDybJLaTRL0B4Jh+4hB
32q4Zr/ulWMX9fQIB+B3PhitPQ55GEdRcs1nuDHn1de+qyhz6pP/MvaWAkvBDeZPzq6PEVYcWNDgZbO1N/
NTcvXuDYhtx45ahdjhwuoGMhnyjZxXoj/cfEObxaY222tZ8UvIWI9HZusFzZqWV6k/km2x/
rM+G+8OZtadsUSeGGIiokRZ2d0014tChUs9nYX7BcbaX2clzIOmdt4AybEEggz6ZijNfXQY3hH0DKoNYoec
1ummt2pQ5NgqQVUQm80Ms0Z7soEpFc2oo+GCh5IB3VHbvZj1PPQTGBXLP/
TtQGKwBp3DOCnOGmTxmULoBRGRItYB3B4S4doyJ6X12gLxUdeZ5WRrerQl6BByRVgWPwxvTZH2fdSXJDuBv
JrVmMowcWGpZy5FBTiVqj/Sb0lNjwnG6VaGChKtcix4gfY4t3O3Xo/
xA2YSoUJUyaVA29j4c+xI3N0fmQTz0=",
"ansible_ssh_host_key_rsa_public_keytype": "ssh-rsa",
"ansible_system": "Linux",
"ansible_system_capabilities": [],
"ansible_system_capabilities_enforced": "False",
"ansible_user_dir": "/home/trainonic",
"ansible_user_gecos": "Trainonic Videos",
"ansible_user_gid": 1000,
"ansible_user_id": "trainonic",
"ansible_user_shell": "/bin/bash",
"ansible_user_uid": 1000,
"ansible_userspace_architecture": "x86_64",
"ansible_userspace_bits": "64",
"gather_subset": [
"min"
],
"module_setup": true
},
"changed": false,
"failed": false
}
}

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

- So, here you can see all the information regarding the local system.

- Ansible_Facts and indented information such as:

- Ansible CMD line:

- Ansible Data and Time:

- Ansible Domain and etc.

3. MODIFYING THE PLAYBOOK (ONLY TO VIEW THE DATe AND TIME INFORMATION):
[root@localhost My-Automation-Stuff2]# cat example11-setup.yml
---

- name: "Testing Playbook"


hosts: localhost

tasks:
- name: "Collection facts from the localhost"
ansible.builtin.setup:
gather_subset:
- "min"
register: output

- name: "Print the output"


debug:
msg: "{{ output.ansible_facts.ansible_date_time }}"
#(Added keyword)

- Save and quit the playbook.


4. RUNNING THE PLAYBOOK:
[root@localhost My-Automation-Stuff2]# ansible-playbook example11-setup.yml

PLAY [Testing Playbook]


**************************************************************************

TASK [Collection facts from the localhost]


*******************************************************
ok: [localhost]

TASK [Print the output]


**************************************************************************
ok: [localhost] => {
"msg": {
"date": "2024-01-30",
"day": "30",
"epoch": "1706596646",
"epoch_int": "1706596646",
"hour": "12",
"iso8601": "2024-01-30T06:37:26Z",
"iso8601_basic": "20240130T120726382474",
"iso8601_basic_short": "20240130T120726",
"iso8601_micro": "2024-01-30T06:37:26.382474Z",
"minute": "07",
"month": "01",
"second": "26",
"time": "12:07:26",
"tz": "IST",
"tz_dst": "IST",
"tz_offset": "+0530",
"weekday": "Tuesday",
"weekday_number": "2",
"weeknumber": "05",
"year": "2024"
}
}

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

- So, this is how you can see all the date and time information and if you
wanted to only see the date then you can only use
the indented "date" keyword. as shown below:
5. MODIFYING THE PLAYBOOK (ONLY TO VIEW THE DATE INFORMATION - NOT DAY):

[root@localhost My-Automation-Stuff2]# cat example11-setup.yml


---

- name: "Testing Playbook"


hosts: localhost

tasks:
- name: "Collection facts from the localhost"
ansible.builtin.setup:
gather_subset:
- "min"
register: output

- name: "Print the output"


debug:
msg: "{{ output.ansible_facts.ansible_date_time.date }}"
#(Added information)

- Save and quit the playbook.

6. RUNNING THE PLAYBOOK:

[root@localhost My-Automation-Stuff2]# ansible-playbook example11-setup.yml

PLAY [Testing Playbook]


**************************************************************************

TASK [Collection facts from the localhost]


*******************************************************
ok: [localhost]

TASK [Print the output]


**************************************************************************
ok: [localhost] => {
"msg": "2024-01-30"
}

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

7. MODIFYING THE PLAYBOOK (ONLY TO VIEW THE DATE AND TIME):

[root@localhost My-Automation-Stuff2]# cat example11-setup.yml


---

- name: "Testing Playbook"


hosts: localhost

tasks:
- name: "Collection facts from the localhost"
ansible.builtin.setup:
gather_subset:
- "min"
register: output

- name: "Print the output"


debug:
msg: "{{ output.ansible_facts.ansible_date_time.iso8601 }}"
#(Added information)

- Save and quit the file.

8. RUNNING THE PLAYBOOK:


[root@localhost My-Automation-Stuff2]# ansible-playbook example11-setup.yml

PLAY [Testing Playbook]


**************************************************************************

TASK [Collection facts from the localhost]


*******************************************************
ok: [localhost]

TASK [Print the output]


**************************************************************************
ok: [localhost] => {
"msg": "2024-01-30T06:42:53Z"
}
PLAY RECAP
***********************************************************************************
****
localhost : ok=2 changed=0 unreachable=0 failed=0
skipped=0 rescued=0 ignored=0

- So, this is how you can gather a specific information.

- & This would be helpful for us, because while taking backups we can name
the file with the date and time.

- So, this is only an example of how you can gather or extract data and time
information.

NOTE:
- Now we're using different method to gather the data and time.

- Similar to above, but a bit different method.

9. GATHERING_FACTS USING (FILTER) IN PLACE OF (GATHER_SUBSET):

[root@localhost My-Automation-Stuff2]# cat example11-setup.yml


---

- name: "Testing Playbook"


hosts: localhost

tasks:
- name: "Collection facts from the localhost"
ansible.builtin.setup:
filter: #(Gather_subset is
replaced with filter)
- "ansible_date_time" #(Ansible Date and
time)
register: output

- name: "Print the output"


debug:
msg: "{{ output }}" #(Updated)

- Save and quit the Playbook.


10. RUNNING THE PLAYBOOK:
[root@localhost My-Automation-Stuff2]# ansible-playbook example11-setup.yml

PLAY [Testing Playbook]


**************************************************************************

TASK [Collection facts from the localhost]


*******************************************************
ok: [localhost]

TASK [Print the output]


**************************************************************************
ok: [localhost] => {
"msg": {
"ansible_facts": {
"ansible_date_time": {
"date": "2024-01-30",
"day": "30",
"epoch": "1706598044",
"epoch_int": "1706598044",
"hour": "12",
"iso8601": "2024-01-30T07:00:44Z",
"iso8601_basic": "20240130T123044860055",
"iso8601_basic_short": "20240130T123044",
"iso8601_micro": "2024-01-30T07:00:44.860055Z",
"minute": "30",
"month": "01",
"second": "44",
"time": "12:30:44",
"tz": "IST",
"tz_dst": "IST",
"tz_offset": "+0530",
"weekday": "Tuesday",
"weekday_number": "2",
"weeknumber": "05",
"year": "2024"
}
},
"changed": false,
"failed": false
}
}

PLAY RECAP
***********************************************************************************
****
localhost : ok=2 changed=0 unreachable=0 failed=0
skipped=0 rescued=0 ignored=0
Similar to gather_subnet, but gathering date and time with "filter" parameter.

11. MODIFYING THE PLAYBOOK (ONLY TO GET THE SPECIFIC DATE AND TIME INFORMATION
USING THE "FILTER"):

[root@localhost My-Automation-Stuff2]# cat example11-setup.yml


---

- name: "Testing Playbook"


hosts: localhost

tasks:
- name: "Collection facts from the localhost"
ansible.builtin.setup:
filter:
- "ansible_date_time"
register: output

- name: "Print the output"


debug:
msg: "{{ output.ansible_facts.ansible_date_time.iso8601 }}" (#added
information)

12. RUNNING THE PLAYBOOK:


[root@localhost My-Automation-Stuff2]# ansible-playbook example11-setup.yml

PLAY [Testing Playbook]


**************************************************************************

TASK [Collection facts from the localhost]


*******************************************************
ok: [localhost]

TASK [Print the output]


**************************************************************************
ok: [localhost] => {
"msg": "2024-01-30T07:06:20Z"
}

PLAY RECAP
***********************************************************************************
****
localhost : ok=2 changed=0 unreachable=0 failed=0
skipped=0 rescued=0 ignored=0
13. MODIFYING THE PLAYBOOK (ONLY TO GET THE SPECIFIC DATE AND TIME INFORMATION
USING THE "FILTER"):

- If we wanted to save this information and use it as an actual variable


through a program.

- So, we're going to create a new tasks to record a variable.

[root@localhost My-Automation-Stuff2]# vim example11-setup.yml

---

- name: "Testing Playbook"


hosts: localhost

tasks:
- name: "Collection facts from the localhost"
ansible.builtin.setup:
filter:
- "ansible_date_time"

- name: "Task2: Recording Variable"


set_fact:
TD: "{{ ansible_date_time.date }}" (Parsing out the actual date)

- Save and quit the file.

i. So, all we've done here is we've added the setup module against the
local machine, which is
our Linux machine, and then it's going to parsing out the dates and
we're assigning that date
to the variable "TD".

- After that, we're going to create a directory structure within


our terminal and one of
those directories actually going to be this variable "TD", the
actual date of when the
playbook was executed.

- So, this is going to allow us to be able to have a nice, clean


and readiable directory
structure where we can actually store all of our backups.

- So, now, we understand how we can actually get the precise


information we want, and let
us see now how we can create that directory structure.

#ls -l (To verify the existing directories.)

- This is how you can see the specific date and time information using the
"Filter" keyword.
- So, why we actually learned about parsing TIME and DATE?
- Well, because whenever we wanted to take a backup, it get backed up
with the TIME and DATE basis, just to make sure
that backup was taken on that specific date:

- So, the folder that we're going to create for backup is going to be
named as the current date and time.

- This is going to be covered in the very next topic (Creating


Directories with the File Module).
***********************************************************************************
****************************************************
X-X-X-X-X

You might also like