4

I'm running an ansible playbook against a number of ec2 instances to check if a directory exists.

---
- hosts: all
  become: true
  tasks:
  - name: Check if foo is installed
    stat:
      path:
        /etc/foo
    register: path
  - debug: msg="{{path.stat.exists}}"

And I would like to generate a localfile that lists the private IP addresses of the ec2 instances and states whether or not the directory foo does exist.

I can get the private IP addresses of the instances with this task

  - name: Get info from remote
    shell: curl http://169.254.169.254/latest/meta-data/local-ipv4
    register: bar
  - debug: msg="{{bar.stdout}}"

How do I create a local file with content

IP address: 10.100.0.151 directory foo - false
IP address: 10.100.0.152 directory foo - true

I've tried adding a block for this as such

- hosts: localhost
  become: false
  vars:
    installed: "{{bar.stdout}}"
    status:    "{{path.stat.exists}}"
    local_file: "./Report.txt"
  tasks:

  - name: Create local file with info
    copy:
      dest: "{{ local_file }}"
      content: |
        "IP address {{ installed }} foo - {{ status }}"

But it doesn't look like I can read values of variables from earlier steps.

What am I doing wrong please?

2 Answers 2

2

A similar question has been answered here.

Basically what you want is to reference it through the host var variable.

This should work.

- hosts: localhost
  become: false
  vars:
    local_file: "./Report.txt"
  tasks:

  - name: Create local file with info
    lineinfile:
      path: "{{ local_file }}"
      line:
        "IP Address: {{ hostvars[item]['bar'].stdout }} - Installed: {{ hostvars[item]['path'].stat.exists }}"
    with_items: "{{ query('inventory_hostnames', 'all') }}"

And this should populate your local ./Report.txt file, with the info you need.

Sign up to request clarification or add additional context in comments.

Comments

2

I've used the ec2_metadata_facts module to get the IP address us ingansible_ec2_local_ipv4
I've also created the directory /tmp/testdir on the second host.

- hosts: test_hosts
  gather_facts: no
  vars:
    directory_name: /tmp/testdir
  tasks:

  - ec2_metadata_facts:

  - name: check if directory '{{ directory_name }}' exsists
    stat:
      path: "{{ directory_name }}"
    register: path


  # I make the outputfile empty
  # because the module lineinfile(as I know) can't overwrite a file
  # but appends line to the old content
  - name: create empty output file
    copy:
      content: ""
      dest: outputfile
    delegate_to: localhost
    
  - name: write output to outputfile
    lineinfile:
      dest: outputfile
      line: "IP Address: {{ ansible_ec2_local_ipv4 }} {{ directory_name }} - {{ path.stat.exists }}"
      state: present
    with_items: "{{ groups.all }}"
    # with_items: "{{ ansible_play_hosts }}" can also be used here
    delegate_to: localhost

The outputfile looks like:

IP Address: xxx.xx.x.133 /tmp/testdir - False
IP Address: xxx.xx.x.45 /tmp/testdir - True

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.