Skip to content

Example 3

hosts.yml

centos:
  hosts:
    provisioner:
      ansible_connection: local
    centos1:
      ansible_host: '192.168.77.22'
ubuntu:
  hosts:
    ubuntu1:
      ansible_host: '192.168.77.23'

playbook.yml

- name: Example playbook
  hosts: all
  gather_facts: false
  tasks:
    - name: Example task
      command: whoami
      register: whoami_output
    - debug:
        msg: "Remote user: {{ whoami_output.stdout }}"

Tip

The debug module can be used to output the results of remote commands. The register directive creates a new variable in the playbook's scope, which can be accessed by other tasks.

Execute the following command

ansible-playbook playbook.yml

Bonus round

Execute the same command while adding the -b flag. Notice the difference in the command's output.