ansible shell variable

207 views
Skip to first unread message

Andrew Morgan

unread,
May 24, 2020, 7:55:52 PM5/24/20
to Ansible Project
Hello All,

I have an ansible script that I am writing to use meta data from an aws ec2 instance. I want to create and image of a list og servers from my inventory file them launch each one, then perform a function then terminate the instances.I am currently stuck on trying to get the subnet portion of the meta data as I need to perform two commands.
1) get the mac address 
2) get the subnet 

But I am not able to successfully get the subnet id

---
- hosts: dev
 become: yes
 #connection: local
# gather_facts: no
 

  tasks:
#  - name: Install boto3 and botocore with pip3 module
#    pip:
#      name:
#      - boto3
#      - botocore
#      executable: pip3.6

  - name: Get Instance info
   command: "wget -q -O - http://169.254.169.254/latest/meta-data/instance-id"
   register: instanceid

  - debug: msg="instance id is {{instanceid.stdout }} "

  - name: Get subnetID
   shell: >
      curl --silent http://169.254.169.254/latest/meta-data/network/interfaces/macs/$INTERFACE/subnet-id
   
   args:
     executable: /bin/bash
   register: subnet_id
 #  environment:


I am currently getting an error:

  msg: |-
    subnet_id is <?xml version="1.0" encoding="iso-8859-1"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
     <head>
      <title>404 - Not Found</title>
     </head>
     <body>
      <h1>404 - Not Found</h1>
     </body>
    </html>

Can anyone please assist?

Thank you 

Vladimir Botka

unread,
May 24, 2020, 8:40:33 PM5/24/20
to Andrew Morgan, ansible...@googlegroups.com
On Sun, 24 May 2020 16:55:52 -0700 (PDT)
Andrew Morgan <alonso...@gmail.com> wrote:

> 1) get the mac address
> 2) get the subnet
>
> ---
> - hosts: dev
> become: true
> #connection: local
> gather_facts: true

Run the command below a see what "facts" are available when "gather_facts:
true" is enabled.

shell> ansible dev -m setup

Andrew Morgan

unread,
May 24, 2020, 11:31:12 PM5/24/20
to Ansible Project
This is the output:

fatal: [deVPN]: FAILED! => changed=true 
  cmd: |-
    ansible dev -m setup
  delta: '0:00:00.033604'
  end: '2020-05-24 23:29:51.883830'
  msg: non-zero return code
  rc: 127
  start: '2020-05-24 23:29:51.850226'
  stderr: '/bin/bash: ansible: command not found'
  stderr_lines: <omitted>
  stdout: ''
  stdout_lines: <omitted>

Vladimir Botka

unread,
May 25, 2020, 1:16:43 AM5/25/20
to Andrew Morgan, ansible...@googlegroups.com
On Sun, 24 May 2020 20:31:12 -0700 (PDT)
Andrew Morgan <alonso...@gmail.com> wrote:

> > > 1) get the mac address
> > > 2) get the subnet
> > >
> > > ---
> > > - hosts: dev
> > > become: true
> > > #connection: local
> > > gather_facts: true

> > Run the command below a see what "facts" are available when "gather_facts:
> > true" is enabled.
> > shell> ansible dev -m setup

> This is the output:
>
> fatal: [deVPN]: FAILED! => changed=true
> cmd: |-
> ansible dev -m setup
> delta: '0:00:00.033604'
> end: '2020-05-24 23:29:51.883830'
> msg: non-zero return code
> rc: 127
> start: '2020-05-24 23:29:51.850226'
> stderr: '/bin/bash: ansible: command not found'
> stderr_lines: <omitted>
> stdout: ''
> stdout_lines: <omitted>

This is a misunderstanding. Do not run the command with ansible-playbook as
"shell" task on the remote host!

The "command" shall be run from the command-line on controller. The output
will show all facts collected by the module "setup" on the remote host. The
same facts will be collected automatically by a playbook when not disabled by
"gather_facts: false". See
https://docs.ansible.com/ansible/latest/modules/setup_module.html#examples

shell> ansible dev -m setup

Find the variables that fit your purpose. For example

shell> cat playbook.yml
- hosts: dev
tasks:
- debug:
var: ansible_default_ipv4
- debug:
msg: "MAC: {{ ansible_default_ipv4.macaddress }}"
- debug:
msg: "Network: {{ ansible_default_ipv4.network }}"

shell> ansible-playbook playbook.yml

HTH,

-vlado
Message has been deleted

Andrew Morgan

unread,
May 25, 2020, 7:57:19 PM5/25/20
to Ansible Project

Thank you very much for the help
Reply all
Reply to author
Forward
0 new messages