Monday, June 23, 2025

Ansible Modules

 

inlinefile module:

Adding /Modify or delete a line inside of file.
Main parameter:
path - full path of the file
line - text
insertbefore / insertafter - EOF/regular expression
validate - Validation of command
state - Present/absent
mode/owner/group - permission
setype/seuser/selevel - SElinux setup
Ping Module:
It is validated the host reachability of remote host.
ansible.builtin.ping is a module name.
Reboot Module:
We can reboot a remote host through reboot module.
Main parameter of this reboot module as below:
reboot_timeout - 600
msg - text of reboot notification
reboot_command - define a reboot command depends up on OS
pre_reboot_delay - 0
Post_reboot_delay - 0
test_command - 'whoami'
boot_time_command - "cat /proc/sys/kernel/boot_id"

Copy Module:
Copy a file from one location to other location.
Main Parameters:
dest - Remote file path
src - local file path
fail_on_missing - yes / no
validate_checksum - yes /no
flat - yes/no
Service Module:
We can enable and disable the system services through this module.
ansible.builtin.service_facts
Main parameters:
name : Service name
state :  started, stopped, restarted, reloaded
enabled: yes/no
arguments/args : extra args
Package installation module:
Main parameters:
name - Name of the package
state - present/installed/absent/removed/latest
Create a file:
Main parameter:
path - file path
state - absent/directory/hard/link/touch
Module for a file permission change:
Main parameters:
Path - file path
owner - user
group - group
mode - rwx mode
state - file state [absent/directory/hard/link/touch]
setype/seuser/selevel - SElinux
Module for Download a file through Internet
Main parameters:
url - download URL
dest - destionation path
force - no/yes
checksum - checksum:URL
force_basic_auth/url_username/url_password/use_gssapi - HTTP basic auth/GSSAPI kerberos
headers - custom HTTP headers
http_agent - ansible-http-get
owner/group/mode - permission
setype/seuser/selevel - SElinux

Example:
---
 - name: Download an ansible package
   hosts: all
   become: false
   gather_facts: false
   vars: 
     myurl: "https://releases.ansible.com/ansible/ansible-2.9.25.tar.gz"
mycrc: "sha256:https://releases.ansible.com/ansible/ansible-2.9.25.tar.gz"
mydest: "/home/test/ansible-2.9.25.tar.gz"
   tasks:
     - name: downloading an ansible file
   ansible.builtin.get_url:
     url: "{{ myurl }}" 
desk: "{{ mydest }}"
checksum: "{{ mycrc }}"
mode: '0644'
owner: devops
group: wheel
Module for backing up the file
Main Parameters:
src - source path
dest - destionation path
archive - mirrors the rsync archive flag, enables recursive, links, perms, times, owner, group, flags 
rsync_opts - no/yes

Changed the line inside of file:
---
- name: search demo
  hosts: all
  vars:
    myfile: "/etc/ssh/sshd_config"
    myline: 'PasswordAuthentication no'
  become: true
  tasks:
    - name: string found
      ansible.builtin.lineinfile:
        name: "{{ myfile }]"
        line: "{{ myline }}"
        state: present
      check_mode: true
      register: conf
      failed_when:(conf is changed) or (conf is failed)

No comments:

Post a Comment