Skip to content

Example 5

hosts.yml

centos:
  hosts:
    provisioner:
      ansible_connection: local
      unique_hash: "NDRhOTBlZmU3OTc2"
    centos1:
      ansible_host: '192.168.77.22'
      ansible_port: 2345
      unique_hash: "MzI0ZTU2ZWMxNTkz"
ubuntu:
  hosts:
    ubuntu1:
      ansible_host: '192.168.77.23'
      unique_hash: "YjlkYTdlMjYyYTU3"

Tip

The lineinfile module enables the user change single lines in a file on the remote. It is frequently used to change values in configuration files. Check out the module's official documentation or run

ansible-doc lineinfile

Create a Jinja2 template and copy it to the remote hosts

ansible centos1 -m lineinfile -a 'path="/etc/motd" state="present" line="Have a nice day"' -b

Bonus round

Use your current knowledge of Ansible modules to revert the changes introduced in Example 4 of Section 1. Inventories and privileges escalation (change the SSH server listening port on centos1 back to 22).

  • you will need to remove the Port 2345 entry from /etc/ssh/sshd_config
  • you will need to run sudo semanage port -d -t ssh_port_t -p tcp 2345
  • you will need to restart the SSH server; use Ansible's systemd module