Inventory File
This file has the list of hosts where the ansible commands or playbook need to be executed. Default inventory file will be under /etc/ansible/hosts.
Sample Entries
[test-servers]
192.168.0.1
192.168.0.2
192.168.0.3
[appserver]
[appserver]
one.example.com
two.example.com
three.example.com
three.example.com
We can put system name in more than one group,
for an instance a server name can be in both test-servers and a appserver group.
Different Port number
If you have hosts that run on non-standard SSH ports you can mention the port number after the hostname with a colon.
four.example.com:5309
Adding a lot of hosts?
If you have a lot of hosts following similar patterns you can do this rather than listing each hostname:
[test-servers]
test[01:50].example.com
For numeric patterns, leading zeros can be included or
removed, as desired. Ranges are inclusive. You can also define alphabetic
ranges:
[appservers]
db-[a:f].example.com
Note: The ‘test-servers‘ in the
brackets indicates group names, it is used in differentiate the server deciding
which systems you are going to controlling at what times and for what reason.
Now let’s check our all 3 servers
by just doing a ping from my localhost (Master Server).
To perform the action we need to
use the command ‘ansible‘ with options ‘-m‘ (module) and ‘-all‘ (group of
servers).
# ansible -m ping test-servers
Note : If the inventory file is
in the default path then we no need to mention the complete path of inventory file. You can specify a different inventory file using
the -i <path> option on the command line.
other2.example.com ansible_connection=ssh ansible_user=mdehaan
host1 http_port=80 maxRequestsPerChild=808
Testing with Simple modules.
Now we will test with module called
‘command‘, which is used to execute list of commands (like, date, uptime,
whoami etc.) on all selected remote hosts at one go, for example watch out few
examples shown below.
a) To check the partitions on all remote hosts
a) To check the partitions on all remote hosts
# ansible -m command -a "df
-h" test-servers
- m - Module name
- a - Command line arguments
b) Check memory usage on all
remote hosts.
# ansible -m command -a
"free -mt" test-servers
c) Checking Uptime for all 3
servers.
# ansible -m command -a
"uptime" test-servers
d) Check for hostname and
Architecture.
# ansible -m command -a
"arch" test-servers
# ansible -m shell -a
"hostname" test-servers
e) If we need the output to any
file we can redirect as below.
# ansible -m command -a "uptime" web-servers > /u01/uptime_output.txt
Like this way, we can run many
shell commands using ansible as what we have run the above steps.
Executing Commands as other user
If you would like to access sudo mode, there are also
flags to do that:
# as bruce
$ ansible all -m ping -u bruce
# as bruce, sudoing to root
$ ansible all -m ping -u bruce --sudo
# as bruce, sudoing to batman
$ ansible all -m ping -u bruce --sudo --sudo-user
batman
# With latest version of ansible `sudo` is deprecated
so use become
$ ansible all -m ping -u bruce -b
# as bruce, sudoing to batman
$ ansible all -m ping -u bruce -b --become-user batman
(The sudo implementation is changeable in Ansible’s
configuration file if you happen to want to use a sudo replacement. Flags
passed to sudo (like -H) can also be set there.)
To Run command on all nodes irrespective of your server groups
If we use -all option then ansible will try to ping all the hosts in the inventory file irrespective of the group.
If we use -all option then ansible will try to ping all the hosts in the inventory file irrespective of the group.
# ansible all -a "/bin/echo hello"
–all Option
No comments:
Post a Comment