When running the shell module in a playbook, how to pass a url to the script?

306 views
Skip to first unread message

Emil Kacperski

unread,
Jun 4, 2015, 9:39:28 PM6/4/15
to ansible...@googlegroups.com
Hello,

If I am running the shell module to run a script and I want to pass a url into the script how would I do this?  Can't use a variable because it's not a dict word?

For example the script is :

echo $url > test.output

The playbook has a task running :

shell echo.sh

If I have a configuration file where the url's are contained, how can I pass it to the script?

Thanks!

Emil

Шалыгин Константин

unread,
Jun 4, 2015, 11:58:44 PM6/4/15
to ansible...@googlegroups.com
As I understart u want something like this?

- name: 1
  shell
: 'date'
 
register: echo0_result
- name: 2
  shell
: 'echo Current date is - {{ echo0_result.stdout }}'
 
register: echo1_result
- name: 3
 
debug: msg='1st result is {{ echo0_result.stdout }}, 2nd is {{ echo1_result.stdout }}'

Emil Kacperski

unread,
Jun 5, 2015, 12:40:37 PM6/5/15
to ansible...@googlegroups.com
Not quite. basically right now I have 10 individual servers where I run a script to bring up a chromium browser output is hdmi.  Basically
I have a videowall with monitors.  Each server has the script and a local screen.conf file which contains the url string.

But instead of logging into each server to change the url or restart the script I want to do this with ansible.

The script and everything is running just fine using the script.  Except of course I want a screen.conf to reside on the ansible server
and not on the remote server.  So that when I run the restart script the url is passed into the script and everything is controlled from
the ansible server logging into the remote servers.

Basically I am not sure how to pass on the url to the remote server script. 

The example I gave is very basic, just assume that the echo.sh is located on the remote server and the url file is located on the ansible
server.  Let's assume screen.conf, so when I run the echo.sh using the shell module I need it to grab the url from the screen.conf
and then pass it to the echo.sh script where it's referenced as $url.

Does this make sense?

Brian Coca

unread,
Jun 5, 2015, 2:51:59 PM6/5/15
to ansible...@googlegroups.com
if the configuration file is local to the 'master', you can use a lookup:

lookup('pipe', 'cat /file/with/url')

if it is remote, backtick to do same
> --
> 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/153d7880-f03b-42b7-a122-be81c6137a22%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.



--
Brian Coca

Emil Kacperski

unread,
Jun 5, 2015, 3:29:03 PM6/5/15
to ansible...@googlegroups.com
Are we now going away from Ansible?  Ok on ansible you have a /etc/ansible/task/url.conf file contains:

[server1]
[server2]

Now you have a playbook:

---

- hosts: server1

  remote_user: remote

  tasks:

  - name: start videowall    

    shell: /etc/ansible/start_video.sh


So now currently inside the start_video.sh file the config file is loaded which contains the url and then when chromium is run it's referenced.  


How can I get Ansible to store the url from the url.conf (on ansible server) and store it within the playbook before the shell command executes

the start_video.sh and when it executes, so that the remote server now knows what $url is when the script runs?


I have tried different methods, including with_vars but it does not seem to work probably because a "var" has to be a dict word.  


Thanks for the help, this last hurdle is always the hardest to figure out ;-).



Brian Coca

unread,
Jun 5, 2015, 7:16:24 PM6/5/15
to ansible...@googlegroups.com
it would be easier if it were a host/group vars file or even a proper
ini file (there is an ini lookup plugin in queue). But if that is what
you have and assuming the caption is per inventory hostname:

shell: /etc/ansible/start_video.sh {{lookup('pipe', 'grep -A1 ' +
inventory_hostname + ' /etc/ansible/task/url.conf|tail -n 1'}}

^ that should work

--
Brian Coca

Emil Kacperski

unread,
Jun 5, 2015, 7:32:57 PM6/5/15
to ansible...@googlegroups.com
Thanks Brian, will look into that and see if I can make it work.

Someone else brought up a very easy solution, simply have multiple config files on the ansible server and do a copy
to the dest.  It's funny how I was so determined to get it done a certain way, while a simple solution was right 
in front of me.

Thanks all for the ideas!
 
Reply all
Reply to author
Forward
0 new messages