Register variable locally and use on remote host

192 views
Skip to first unread message

David Pires

unread,
Jun 5, 2015, 6:52:56 PM6/5/15
to ansible...@googlegroups.com
I am trying to use the route53 module in a playbook. 

If i install boto and add credentials the play works (reading a record and registering it), however I want to know if I can do this locally (where boto is installed) and have that registered variable available on remote hosts.

Example playbook;

---                                                                            

  - hosts: localhost                                                           

    connection: local                                                          

    tasks:                                                                 

    - name: Read current host                                       

      route53:                                                                 

        command: get                                                           

        zone: zone                                                        

        record: record                                                 

        type: A                                                                

      register: host_record                                                  

                                                                               

                                                                               

  - hosts: remote_hosts                                                             

    sudo: True                                                                 

    gather_facts: True                                                                                                               

    roles:                                                                     

      - my_role



This doesn't work, the variable host_record isn't available on the remote host. I would like to know if it's possible and what I am doing incorrectly.


Thanks

Brian Coca

unread,
Jun 5, 2015, 7:32:09 PM6/5/15
to ansible...@googlegroups.com
---

- hosts: remote_hosts
sudo: True
gather_facts: True
pre_tasks:

- name: Read current host
connection: local
sudo: False
route53:
command: get
zone: zone
record: record
type: A
register: host_record

roles:
- my_role


--
Brian Coca

David Pires

unread,
Jun 5, 2015, 8:26:10 PM6/5/15
to ansible...@googlegroups.com
I am getting the same error: "failed=True msg='boto required for this module"

My inventory file is as follows:

[localhost]
localhost ansible_python_interpreter=PATH

[test]
IP

And my playbook:

---

  - hosts: test

    pre_tasks:

      - name: Read host

        connection: local

        route53:

          command: get 

          zone: ZONE

          record: RECORD

          type: A

        register: host_record


    roles:

      - test


Running route53 or ec2 locally using host: localhost works as expected. What am I missing?

Brian Coca

unread,
Jun 5, 2015, 8:31:04 PM6/5/15
to ansible...@googlegroups.com

Do you have boto installed on your machine?

David Pires

unread,
Jun 5, 2015, 8:33:00 PM6/5/15
to ansible...@googlegroups.com
boto is installed on the ansible machine, not the remote host

Brian Coca

unread,
Jun 5, 2015, 9:12:00 PM6/5/15
to ansible...@googlegroups.com
then if you added connection: local to the task, it should be
executing locally and picking up boto
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/54f02330-cd31-42ff-bc5b-4d2c7c010a04%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.



--
Brian Coca

David Pires

unread,
Jun 5, 2015, 10:07:58 PM6/5/15
to ansible...@googlegroups.com
I think I have found the issue, I'm running ansible in a virtualenv. 

I have "ansible_python_interpreter" set for localhost, this doesn't work for remote hosts.

When I run ansible with boto available globally, it works. Is there a solution to get ansible to find dependencies for remote hosts? 

Brian Coca

unread,
Jun 5, 2015, 10:18:35 PM6/5/15
to ansible...@googlegroups.com
I don't think I'm parsing what you are saying correctly. I thought you
were running the task that required boto locally.

ansible_python_interpreter is what you use to direct ansible to the
correct python with the correct dependencies, it only applies for
those hosts for which you set it.




--
Brian Coca

David Pires

unread,
Jun 5, 2015, 10:26:35 PM6/5/15
to ansible...@googlegroups.com
I am trying to run the route53 module locally, register its output and use that variable in a task for a remote host.

This works if i have boto installed globally, using connection: local. It does not work when using inside a virtualenv.

I reproduced this in a virtual machine with boto available globally, and it works. On my mac laptop, I am using boto/ansible inside a virtualenv and this is where boto can not be found for a remote host.

On my local machine, when running on a localhost (with python_interpreter set), it does work and boto is found, however the variable isn't available on the remote host.

Brian Coca

unread,
Jun 5, 2015, 11:01:47 PM6/5/15
to ansible...@googlegroups.com
I'm confused, what you are stating seems to contradict itself.

David Pires

unread,
Jun 5, 2015, 11:11:23 PM6/5/15
to ansible...@googlegroups.com
 ---
  - hosts: remote_host

    pre_tasks:
      - name: Read host
        connection: local
        route53:
          command: get 
          zone: ZONE
          record: RECORD
          type: A
        register: host_record
    roles:
      - test
 
When boto/ansible is available globally, this playbook works as expected, it registers the variable and can be used on a remote host.

This does not work when boto/ansible is being used inside a virtualenv. Boto can not be found.

What seems to be the issue, is ansible is using the global python version (which doesn't have boto) when using remote hosts. 

When inside the virtualenv and setting python_interpreter for localhost, these tasks work on localhost.

Matt Martz

unread,
Jun 5, 2015, 11:26:07 PM6/5/15
to ansible...@googlegroups.com
So set ansible_python_interpreter to the correct Python binary for the virtualenv on the remote host.

Also, you can execute tasks on localhost and then reference it from another host:


Or use local_action which will delegate to localhost, but register the car to the remote host.
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.


--
Matt Martz
@sivel
sivel.net

David Pires

unread,
Jun 5, 2015, 11:53:05 PM6/5/15
to ansible...@googlegroups.com
Setting the interpreter for the remote host would require boto being installed on the remote host, which isn't what I'm after.

Setting the variable on localhost and accessing it remotely using hostvars does work, however, the real issue is how ansible can't seem to find boto when its run inside a virtualenv for a remote host task.

To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.

Martin

unread,
Jun 6, 2015, 6:44:37 AM6/6/15
to ansible...@googlegroups.com
Boto is required on the host it runs on.

There is no way to execute boto specific things on "test" when it is only installed on local host.

What you can do is something like collect information on "test" then run boto stuff on "localhost".

/martin
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.


--
Matt Martz
@sivel
sivel.net

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/f7823de7-dcba-40c5-8c6b-9cefd50a73eb%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
--
Reply all
Reply to author
Forward
0 new messages