Module and Adhoc Command

Ad Hoc Commands

  • Ad hoc commands are simple, one line operations that are run without writing a playbook.

  • They are useful for quick tests and changes.

  • For example, to start a service or ensure a line exists in a file.

  • Ad hoc commands have limitations.

Ansible Modules

  • Ansible provides modules, code that can be used to automate particular tasks

  • Some uses of modules:

    • Ensure users exist with certain settings

    • Make sure the latest version of a software package is installed

    • Deploy a configuration file to a server

    • Enable a network service and make sure that it is running

  • Most modules are idempotent, which means they only make changes if a change is needed. Idempotent modules can be run safely multiple times.

  • An ad hoc command runs one module on the specified managed hosts.

Commands

  • ansible $Host-Pattern -m module [-a 'module argument'] [-i inventory]

  • ansible-doc -l: list all installed modules: https://docs.ansible.com/ansible/latest/modules/modules_by_category.html

  • ansible-doc ping: view information about module ping

Examples

Use ping module to check if Ansible module (Python) can be run on managed host

  • This is not ping module to send network echo request

  • Use shell command to ping another address

Overriding default settings

  • These options override the configuration in the ansible.cfg configuration file.

    • -k or --ask-pass will prompt for the connection password.

    • -u REMOTE_USER overrides the remote_user setting in ansible.cfg.

    • -b option enables privilege escalation, running operations with become: yes.

    • --become_user: speicify become user

    • -K or --ask-become-pass will prompt for the privilege escalation password.

    • --become-method will override the default privilege escalation method. The default is sudo. Find valid choices using ansible-doc -t become -l.

Ansible Module Usage

  • File Modules:

    • copy: Copy a local file to the manages host

    • file: Set permissions and other properties of files

    • lineinfile: Ensure a particular line is or is not in a file

    • synchronize: Synchronize content using rsync

  • Software package modules:

    • yum: Manage packages using YUM

    • dnf: Manage packages using DNF

    • gem: Manage Ruby gems

  • System Modules:

  • firewalld: Manage arbitrary ports and services using firewalld

  • reboot: Reboot a machine

  • service: Manage services

  • user: Add, remove, and manage user accounts

  • group: Add, remove, and manage group

  • Net Tools modules:

    • get_url: Download files over HTTP, HTTPS, or FTP

    • nmcli: Manage networking

    • uri: Interact with web services and communicate with APIs

Some Examples

  • ansible -m user -a 'name=user uid=4000 state=present' server.domain.com: make sure user user is present and has uid number 4000

  • ansible all -m group -a 'name=developers gid=2000 state=present': make sure group developers with UDI 2000 exists on all managed hosts

  • ansible all -m user -a 'name=newbie groups=developers,wheel append=yes state=present': add user newbie to group developers and wheel without chaing primary group ore remove newbee from other groups

  • ansible all -m package -a 'name=httpd state=present': ensure httpd package is installed on all hosts

  • ansible -m ios_command -a "commands='show ip int br'" rtr3: run cli command on cisco ios device

  • ansible -m ios_command -a "commands='show ip int brief,show ver'" switch

Some modules that are not Idempotent

  • make sure they are safe to run twice

  • command: runs a single command on the remote system, requires Python on managed host

  • shell: runs a command on the remote system's shell (redirection and other features work), requires Python on managed host

  • raw: simply runs a command with no processing (can be dangerous), This run commands direclty using the remote shell -> useful when managing system that cannot have Python installed

Debug module

  • Besiding using | (Literal Block Scalar) to break string over multiple lines, > (Folded Block Scalar) can be used to break multiple line, but when printed, new line will be represented as a space

  • https://www.ansiblepilot.com/articles/break-a-string-over-multiple-lines-ansible-literal-and-folded-block-scalar-operators/

Copy Module - Save content to file

Save output from all host to one file:

  • Using special var: ansible_play_hosts_all

Building custom ansible module

  • Modules can be written in python or powershell language

  • Python or powershell scripts are saved in library folder

  • Other common code that are used by scripts in library folder can be saved in module_utils folder

  • In the following example, ansible playbook is using backup_check module (backup_check.py) in library folder

  • The content of backup_check.py

Last updated