user4692887
user4692887

Reputation:

Print out the ansible host name

One of the task of my ansible playbook is below, and this playbook suppose to bring up an EC2 instance:

TASK: [Demo] ******************************************************************
changed: [ec2-52-24-222-200.us-west-2.compute.amazonaws.com] => (item=ec2.instances)

How can I print out or create a folder with the nameec2-52-24-222-200.us-west-2.compute.amazonaws.com on Ansible

Upvotes: 2

Views: 7971

Answers (1)

udondan
udondan

Reputation: 59989

The hostname as defined in your inventory is stored in the variable inventory_hostname.

So to print out the host name you would do:

- debug: var=inventory_hostname

To create a directory:

- file:
    path: "/tmp/{{ inventory_hostname }}"
    state: directory

If you meant by "on Ansible" that you want to create the directory on the Ansible control host, you need to delegate the task:

- file:
    path: "/tmp/{{ inventory_hostname }}"
    state: directory
  delegate_to: localhost

Upvotes: 5

Related Questions