Ansible Tutorial - How To Define a Ansible Register Variable in Playbook

Tags: ansible register variable, ansible, ansible tutorial, ansible ansible certification, ansible tags, ansible tasks, ansible modules, ansible debug, ansible inventory, ansible hosts file, ansible facts,ansible playbook tutorial, ansible linux, ansible best practices
Ansible registers module used to capture / store the output of a task/command.it is similar to variable declarations we can define the value in different scenarios like a conditional, logging etc.

Ansible Playbook for Get Hosts Information | Ansible Playbook Tutorial

ansible, ansible tutorial, ansible certification, ansible tags, ansible tasks, ansible modules, ansible debug, ansible inventory, ansible hosts file, ansible facts
Ansible Playbook -  Get System Information
tags: ansible, ansible tutorial, ansible certification, ansible tags, ansible tasks, ansible modules, ansible debug, ansible inventory, ansible hosts file, ansible facts







In this absible playbook example we will write a ansible tasks using a gathering facts of ansible hosts. we use ansible facts as variable and echo there value.
out ansible playbook will display hostname, date and timezone, network information, os information, kernel version and Hardware information of hosts.

Ansible Tutorial - How to Use Ansible User and Group Module | Ansible Ad-Hoc Command



ansible user module, ansible group module, 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 Tutorial : Ansible modules user & group
ansible user module, ansible group module, 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



In this topic we will learn about how to manage user and group using ansible modules, we use following ansible module for it.

  • Ansible User Module
  • Ansible Group Module

User Management

User Module : It is used for user management, create user, delete user and more

To help ansible user module  
ansible-doc user
ansible user module, ansible group module, 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 Tutorial - ansible-doc

To Check password algorithm of Hosts, its must require to generate password and set password of user because plan text password does not support.
ansible 172.17.20.98 -m shell -a "authconfig --test" | grep password



Generate password :
python -c "from passlib.hash import sha512_crypt; import getpass; print sha512_crypt.using(rounds=5000).hash(getpass.getpass())"
Password: **********
$6$O.RWXg/0E73uMPKe$DF.CGkdD0F/z6Q9Pww31mGxKvgRlhIN3bLXh2U/TKWTbSdax47xxrXe5nehSkxS.rrLdKeJr8rbC5ZwhMJRuC0


Create a User : Using hash password string in ansible
ansible 172.17.20.98 -m user -a 'name=lokcar password=$6$O.RWXg/0E73uMPKe$DF.CGkdD0F/z6Q9Pww31mGxKvgRlhIN3bLXh2U/TKWTbSdax47xxrXe5nehSkxS.rrLdKeJr8rbC5ZwhMJRuC0 state=present'


To Create a user with define shell and home directory
ansible 172.17.20.98 -m user -a 'name=lokesh1 password=$6$n2em3sW/g7aKU6Ek$LgIJnIfmQkCqry3gOHpe8zNodK6yQ01SL2737eYtxjuJtbP0tRo5HRaOlf6.OzjpThGIyHf0kj4dv8f/yA9CN1 state=present home=/tmp/ shell=/usr/bin/lshell'
ansible user module, ansible group module, 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 Tutorial - Create User with define Shell and Directory 
Change user primary group
ansible 172.17.20.98 -m user -a "name=lokesh group=tesy"

ansible 172.17.20.98 -m user -a "name=lokesh groups=lokesh"

Add user in multiple groups
ansible 172.17.20.98 -m user -a "name=lokesh append=yes groups=lokesh,apache,ftp"
  
Deleting a user
ansible 172.17.20.98 -m user -a "name=ans2 state=absent"
ansible user module, ansible group module, 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 Tutorial - Delete Users

Ansible Group Management :

To Create Group
ansible 172.17.20.98 -m group -a "name=ansible1"
ansible user module, ansible group module, 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 Tutorial - Create Groups
   
Delete group
ansible 172.17.20.98 -m group -a "name=ansible1 state=absent"



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

Ansible Tutorial - Ansible Modules Ping Setup Command Shell Copy Find

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
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 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-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:

  1. ping
  2. hostname
  3. setup
  4. command
  5. shell
  6. copy
  7. get_url
  8. 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 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 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 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 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, 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 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 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  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