tags: ansible tutorial for beginners, ansible, ansible tutorial, ansible ad hoc commands, ansible modules, ansible example, ansible playbook tutorial, ansible linux, ansible best practices, ansible best practices, ansible playbook examples, ansible roles, ansible ping, ansible setup, ansible copy, ansible command, ansible shell module, ansible get_url
Ansible Version 2.7.5
Useful Ansible Command :
ansible - Define and run a single task against a set of hosts
ansible-config - View, edit, and manage ansible configuration
ansible-doc - plugin documentation tool
ansible-galaxy - Perform various Role related operations
ansible-playbook - Runs ansible playbooks on targeted hosts
ansible-vault - encryption/decryption utility for ansible data files
ansible-doc Command
It is a plugin documentation tool, we can take help of any module
Syntax :
ansible-doc [-l|-F|-s] [options] [-t <plugin type> ] [plugin]
To display All modules :
ansible-doc -l
ansible-doc command |
To display help / option particular module/plugin
ansible-doc hostname
To display option of yum modules
ansible-doc yum
ansible-doc command yum documentation |
Ansible Command
Define and run a single task against a set of hosts/groups.
Syntax :
ansible <host-pattern> [options]
For more help or display all options
ansible -h
Modules:
ping
hostname
setup
command
shell
copy
get_url
find
Use Ping module
ansible all -m ping
Explanation:
ansible = Command
all = all hosts of inventory, we can define single hosts, groups or IP address
-m ping = module name
ansible ping modules |
Ansible Hostname Module :
To Change Hostname
ansible 127.0.0.1 -m hostname -a “name=linuxserver”
Explanation:
ansible = Command
127.0.0.1 = hosts of inventory, we can define all hosts, using “all” , “groups name”
-m hostname = module name
-a = use for argument
name=linuxserver = we changed name from linuxtopic to linuxserver
Ansible Setup Module
To display / gather all information of hosts
ansible 127.0.0.1 -m setup | more
ansible setup module |
Filter or Grep information
ansible 127.0.0.1 -m setup | grep swap
Use filter in ansible command output
ansible 127.0.0.1 -m setup | grep -e swap -e memorysize -e process
ansible command filter |
Use ansible command module
ansible 127.0.0.1 -m command -a “ip a” ansible 127.0.0.1 -m command -a “ip a” | grep eth0
ansible 127.0.0.1 -m command -a “cat /etc/hostname”
Use ansible shell module
ansible 127.0.0.1 -m shell -a “ip a” ansible 127.0.0.1 -m shell -a “cat /etc/hostname”
ansible 127.0.0.1 -m shell -a “touch /etc/hostname111”
Ansible copy module
To Copy file from one location to another location
ansible 127.0.0.1 -m copy -a “src=/tmp/hostname111 dest=/tmp/hostname2222 ”
To copy file with change owner, group and permission
ansible 127.0.0.1 -m copy -a “src=/tmp/hostname111 dest=/tmp/hostname2222 owner=lokesh group=lokesh mode=777 ”
Verify :
ll /tmp/hostname*
Ansible Find module
We can find any file using find module:
ansible 127.0.0.1 -m find -a “paths=/tmp/” | grep hostname
Ansible get_url module
We can download any file using get_url module:
ansible 127.0.0.1 -m get_url “url=https://images.indianexpress.com/2018/12/FEATURE-2.jpg dest=/tmp/happynewyear2019.jpg”
Ansible script module
We can run any script using script module using following example
ansible 127.0.0.1 -m script -a "/tmp/script.sh"
Script.sh
#!/bin/bash
echo `hostname`
echo `date -I`
Thanks
End of this ansble ad-hoc command and ansible modules, we need your support so i request you to please comment, share and like this post
www.linuxtopic.com
Fantastic article. Really thank you! Awesome.
ReplyDelete3 star ping pong balls