0

From: Best way to launch aws ec2 instances with ansible

  - name: Add the newly created EC2 instance(s) to the local host group (located inside the directory)
    local_action: lineinfile 
                  dest="/etc/ansibles/aws/hosts" 
                  regexp={{ item.private_ip }} 
                  insertafter="[webserver]" line={{ item.private_ip }}
    with_items: "{{ ec2.instances }}"

creates this error:

fatal: [localhost]: FAILED! => { "failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'ansible.vars.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'private_ip'\n\n

I have defined variable private_ip: under vars , with a value

John D
  • 15
  • 6

1 Answers1

0

I think that the private_ip property in the code above references to the property of the ec2 variable that's used to catch the returned values from the ec2 module (from the last step), no the one that you defined elsewhere.

  - name: Launch the new EC2 Instance
    local_action: ec2 
                  group={{ security_group }} 
                  instance_type={{ instance_type}} 
                  image={{ image }} 
                  wait=true 
                  region={{ region }} 
                  keypair={{ keypair }}
                  count={{count}}
    register: ec2 (this is where the variable is defined!!!)

Essentially Ansible is complaining that the ec2 variable hasen't got the

attribute 'private_ip'

, so check the preceding code and see how that variable gets defined.

In the example above you're trying to get the private_ip address from aws. Is that really what you want? Most of the time you want the public ip address since that's what you will use to connect to the ec2 machine, provision it, deploy your app etc...

Tomislav Mikulin
  • 5,306
  • 4
  • 23
  • 36