3. Setup and Ad Hoc Commands

In this lab we’ll continue with our environment setup from Chapter 1 and learn how to run ad hoc commands.

Task 1

  • Ping all nodes in the inventory file using the ping module.
Solution Task 1
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
$ ansible all -i hosts -m ping
5.102.146.128 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
...
...

Task 2

  • Gather all facts from the nodes.
  • Only gather the fact ansible_default_ipv4 from all hosts.
Solution Task 2
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ 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/libexec/platform-python"
    },
    "changed": false
}
...
...

Task 3

  • Search through the online documentation for special (magical) variables.
  • Which special variable could you use to set the hostname on each of the servers using the information in the inventory file?
Solution Task 3
  • See Ansible docs for special variables: Special Variables
  • inventory_hostname contains the name of the managed host from the inventory file and can be used to set the hostname on the servers.

Task 4

  • 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 the ansible command to make that work?
Solution Task 4
1
2
3
4
5
6
7
8
9
$ 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

Task 5

  • Set the hostname on all nodes using the inventory and an ansible ad hoc command.
  • Check on all nodes if the hostname has been changed.
Solution Task 5
1
2
ansible all -i hosts -b -m hostname -a "name={{ inventory_hostname }}"
ansible all -i hosts -a "cat /etc/hostname"

Task 6

Complete the next steps using Ansible ad hoc commands:

  • Install httpd on the nodes in group web
  • Start httpd on the remote server and configure it to always start on boot.
  • Revert the changes made by the ad hoc commands again.
Solution Task 6
1
2
ansible web -i hosts -b -m dnf -a "name=httpd state=installed"
ansible web -i hosts -b -m service -a "name=httpd state=started enabled=yes"

Reverting the changes made on the remote hosts:

1
2
ansible web -i hosts -b -m service -a "name=httpd state=stopped enabled=no"
ansible web -i hosts -b -m dnf -a "name=httpd state=absent"

Task 7

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

Possible solution 1:

1
2
3
ansible node2 -i hosts -m file -a "path=/home/ansible/testfile.txt state=touch"
ansible node2 -i hosts -m copy -a "dest=/home/ansible/testfile.txt content='SOME RANDOM TEXT'"
ansible node2 -i hosts -m file -a "path=/home/ansible/testfile.txt state=absent"

Possible solution 2: The copy module can create the file directly

1
2
ansible node2 -i hosts -m copy -a "dest=/home/ansible/testfile.txt content='SOME RANDOM TEXT'"
ansible node2 -i hosts -m file -a "path=/home/ansible/testfile.txt state=absent"

All done?