Skip to content

Example 4

hosts.ini

[centos]
priovsioner     ansible_connection=local
centos1         ansible_host=192.168.77.22  ansible_port=2345

[ubuntu]
ubuntu1         ansible_host=192.168.77.23

Tip

You can desribe your hosts with various parameters (eg. ansible_port, which defines the port number on which the SSH server on the remote listens for connections). For a comprehensive list take look at the official documentation.

Try to execute the whoami command on all hosts.

ansible all -m command -a "whoami"
What is the expected output of the command ?

The command will fail for centos1 since by default the SSH server listens on port 22.

Bonus round

Fix the problem with centos1 by executing the following steps

  • open a SSH connection to centos1
    ssh vagrant@192.168.77.22
    
  • edit the SSH server configuration on the remote
    sudo vi /etc/ssh/sshd_config
    
  • uncomment the line starting with #Port, set the port value to 2345 (the resulting line should read Port 2345) and save the changes
  • update the SELinux policy on the remote (you will need to install the policycoreutils-python package)
    sudo yum install -y policycoreutils-python
    sudo semanage port -a -t ssh_port_t -p tcp 2345
    
  • restart the sshd service
    sudo systemctl restart sshd