In this lab we’ll continue with our environment setup and learn how to run ad hoc commands.
-
Ping all nodes in the inventory file using the ping module
Tip
|
You’ve used the |
-
Search through the online documentation for special (magical) variables.
-
Which special variable could you use to set the
hostname
on all the servers using the information in theinventory
file?
-
Try to find an appropriate ansible module to complete task 3. Find out what parameters the module accepts.
-
This module will try to make changes to the
/etc/hostname
file. What options should you use with theansible
command for it to work?
-
Set the hostname on all nodes using the inventory and an ansible ad-hoc command.
-
Login on one of the nodes. Has the
hostname
been changed?
Complete the next steps using ansible ad hoc commands:
-
Install
httpd
on the nodes in groupweb
-
Start
httpd
on the remote server and configure it to always start on boot. -
Revert the changes made by the ad hoc commands again.
Complete the next steps using ansible ad hoc commands:
-
Create a file
/home/ansible/testfile.txt
on node2. -
Paste some custom text into the file using the
copy
module. -
Remove the file with an ad hoc command.
Solution 1
$ ansible all -i hosts -m ping 5.102.146.128 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" } ... ...
Solution 2
$ ansible all -i hosts -m setup # (a lot of green output should be printed) $ ansible all -i hosts -m setup -a "filter=ansible_default_ipv4" 5.102.146.204 | SUCCESS => { "ansible_facts": { "ansible_default_ipv4": { "address": "5.102.146.204", "alias": "eth0", "broadcast": "5.102.146.255", "gateway": "5.102.146.1", "interface": "eth0", "macaddress": "5a:42:05:66:92:cc", "mtu": 1500, "netmask": "255.255.255.0", "network": "5.102.146.0", "type": "ether" }, "discovered_interpreter_python": "/usr/bin/python" }, "changed": false } ... ...
Solution 3
-
https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html
-
inventory_hostname
can be used to set the hostname on the servers.
Solution 4
$ ansible-doc -l | grep hostname # or see webpage bigip_hostname Manage the hostname of a BIG-IP hostname Manage hostname win_hostname Manages local Windows computer name $ ansible-doc -s hostname - name: Manage hostname hostname: name: # (required) Name of the host
-
We will need root privileges and therefore we have to use the become option
-b
Solution 5
$ ansible all -i hosts -b -m hostname -a "name={{ inventory_hostname }}" $ ssh ansible@[nodeIPhere]
Did the hostname change?
Solution 6
$ ansible web -i hosts -b -m yum -a "name=httpd state=installed" node1 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "changes": { "installed": [ "httpd" ] ... ... $ ansible web -i hosts -b -m service -a "name=httpd state=started enabled=yes" node1 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "enabled": true, "name": "httpd", "state": "started", "status": { ... ...
Reverting the changes made on the remote host:
$ ansible web -i hosts -b -m service -a "name=httpd state=stopped enabled=no" $ ansible web -i hosts -b -m yum -a "name=httpd state=absent"
Solution 7
$ ansible node2 -i hosts -b -m file -a "path=/home/ansible/testfile.txt state=touch" $ ansible node2 -i hosts -b -m copy -a "dest=/home/ansible/testfile.txt content='SOME RANDOM TEXT'" $ ansible node2 -i hosts -b -m file -a "path=/home/ansible/testfile.txt state=absent"