Tasdik Rahman
Tasdik Rahman

Reputation: 2330

Passing command line arguments to a system script in Ansible role?

In ansible, how should I go about passing command line arguments to a system script?

For example, on the remote host

$ /usr/share/my-script \
--my-arg1=gist.github.com \
--my-arg2="foo bar"

The value foo bar is something which I need to define at the role level and something which changes with each system script.

If there would be a way to put the value of foo bar in defaults and then let jinja2 replace it while the role runs. Any suggestions on how should I approach it?

Upvotes: 2

Views: 1118

Answers (2)

deepak
deepak

Reputation: 1151

You can use script module for achieving it, for example:

- script: /usr/share/my-script --my-arg1 "{{ var1 }}" --my-arg2 "{{ var2 }}"

As above you can define var1 and var2 default value in role vars files as below: var1: "gist.github.com" var2: "foo bar"

Also you can pass values at runtime as below: ansible-playbook -extra-vars "var1=gist1.github.com var2=abcxyz" <playbook_name>

Upvotes: 1

Tasdik Rahman
Tasdik Rahman

Reputation: 2330

I got it to work by

- name: foo bar
  shell: |
    /usr/share/my-script \
    --my-arg1=gist.github.com \
    --my-arg2={{ your_var }}

inside the role where I wanted to run my command.

You can define your_var at different places as allowed by ansible

Upvotes: 0

Related Questions