Skip to content

Latest commit

 

History

History
178 lines (157 loc) · 4.58 KB

lab03.0_setup_and_adhoc.adoc

File metadata and controls

178 lines (157 loc) · 4.58 KB

LAB03 - Ansible Setup and Ad Hoc Commands

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

TASK 1

  • Ping all nodes in the inventory file using the ping module

Tip

You’ve used the ping module in a previous lab.

TASK 2

  • Gather all facts from the nodes.

  • Only gather the fact ansible_default_ipv4 from all hosts.

TASK 3

  • 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 the inventory file?

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 for it to work?

TASK 5

  • 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?

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.

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.

Solutions

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