Sara James
Sara James

Reputation: 143

How can I pass extra variable which has ENVIRONMENT variable to ansible playbook in the command line?

Here is the code.

- hosts: "{{ env }}"
  gather_facts: false
  become: true
  become_method: sudo
  tasks:
   - name: Check HealthService
     shell: docker exec {{ container_name }} sh -c {{ command }}

Here is what i'm trying to pass at the command line which fails.

 ansible-playbook -i inventory.py site.yaml  -e "container_name=webproxy"  --limit=vm4node.lite.com -e "env=dev" -e "command='curl -k -s https://localhost:"${nginx_https_port}"/healthcheck'"

I would like this to be executed at the shell: docker exec webproxy sh -c 'curl -k -s https://localhost:"${nginx_https_port}"/HealthCheck'

How should i passing the extra vars which has ENV variable at the command line. Any help would be greatly appreciated.

Upvotes: 1

Views: 2272

Answers (1)

krasnosvar
krasnosvar

Reputation: 119

May be it's late for you. But can help others, looking same.

In your command you are tryin put environment variable in command, it fails. But it can be solved by such workaround:

1 - Let's set env variable:

export nginx_https_port=443

2 - In ansible set OS environment variable as ansible variable in extra vars:

-e "nginx_port=$nginx_https_port"

3 - in your command set nginx_port as jinja2 template var:

 -e "command='curl -k -s https://localhost:{{ nginx_port }}/healthcheck'"

4 - Full ansible command will look like:

ansible-playbook -i inventory.py site.yaml  -e "nginx_port=$nginx_https_port" -e "container_name=webproxy"  --limit=vm4node.lite.com -e "env=dev" -e "command='curl -k -s https://localhost:{{ nginx_port }}/healthcheck'"

Upvotes: 1

Related Questions