Closed zwxk14 closed 8 years ago
Can you give an example of what options you mean? The standard options like -C and -D?
Yes, The ansible-playbook standard options like:
- -e VARS, --extra-vars=VARS
- -t, TAGS, --tags=TAGS
- --skip-tags=SKIP_TAGS
The options above are very useful for playbooks.
What is the correct way to use --extra-vars? Tried -e,somevar=true,-e,someothervar=false
but variables are not visible within playbooks. I get errors from Ansible about undefined variables.
@sperlic try it like this: -e var1=True var2=5
@frozenice Unfortunately I already tried on that and many other ways but no cigar. I always get "msg": "'var1' is undefined"
Hm, seems to work for me. I created a job with one node step (Ansible Playbook).
/tmp/test-playbook
-e var1=True var2=5
Contents of /tmp/test-playbook:
---
- hosts: all
tasks:
- debug: msg="var1 {{ var1 }}, var2 {{ var2 }}"
Running the job on a node yields this log output in Rundeck:
Using /etc/ansible/ansible.cfg as config file
PLAY ***************************************************************************
TASK [setup] *******************************************************************
ok: [somenode]
TASK [debug] *******************************************************************
ok: [somenode] => {
"msg": "var1 True, var2 5"
}
PLAY RECAP *********************************************************************
somenode : ok=2 changed=0 unreachable=0 failed=0
What are you doing differently?
I additionally use rolls and tags in combination with extra vars. Here are the steps to reproduce the problem
Create eg. ansible
directory structure like this
/tmp/ansible/
├── roles
│ └── test-playbook
│ └── tasks
│ └── main.yml
└── test-playbook.yml
test-playbook.yml
---
- hosts: all
roles:
- { role: test-playbook, tags: tag1 }
roles/test-playbook/tasks/main.yml
---
- debug:
msg: "tag1, var1 {{ var1 }}, var2 {{ var2 }}"
tags:
- tag1
- debug:
msg: "tag2, var1 {{ var1 }}, var2 {{ var2 }}"
tags:
- tag2
Create job with one node step (Ansible playbook)
/tmp/ansible/test-playbook.yml
and try following Extra Arguments
Output with Extra Arguments: -t tag2 -e var1=True var2=5
or -t tag1,tag2 -e var1=True var2=5
Using /etc/ansible/ansible.cfg as config file
PLAY [all] *********************************************************************
TASK [setup] *******************************************************************
ok: [somenode]
PLAY RECAP *********************************************************************
somenode : ok=1 changed=0 unreachable=0 failed=0
Output with Extra Arguments: -t,tag1 -e var1=True var2=5
Using /etc/ansible/ansible.cfg as config file
PLAY [all] *********************************************************************
TASK [setup] *******************************************************************
ok: [somenode]
TASK [test-playbook : debug] ***************************************************
fatal: [somenode]: FAILED! => {"failed": true, "msg": "'var1' is undefined"}
NO MORE HOSTS LEFT *************************************************************
to retry, use: --limit @/tmp/ansible/test-playbook.retry
PLAY RECAP *********************************************************************
somenode : ok=1 changed=0 unreachable=0 failed=1
Execution failed: 160: [Workflow result: , step failures: {1=Dispatch failed on 1 nodes: [somenode: AnsibleNonZero: Ansible exited with non-zero code.]}, Node failures: {somenode=[AnsibleNonZero: Ansible exited with non-zero code.]}, status: failed]
Also combinations like -t,tag1,tag2,-e var1=True var2=5
does not work, but it seems that -e var1=True var2=5 -t tag1,tag2
is right combination.
Using /etc/ansible/ansible.cfg as config file
PLAY [all] *********************************************************************
TASK [setup] *******************************************************************
ok: [somenode]
TASK [test-playbook : debug] ***************************************************
ok: [somenode] => {
"msg": "tag1, var1 True, var2 5"
}
TASK [test-playbook : debug] ***************************************************
ok: [somenode] => {
"msg": "tag2, var1 True, var2 5"
}
PLAY RECAP *********************************************************************
somenode : ok=3 changed=0 unreachable=0 failed=0
Thank you for following me.
It seems that still does not work as expected. When using extra arguments -e var1=True var2=5 -t tag2
will run the whole playbook instead tasks tagged with tag2
.
Have you tried running it manually via Ansible?
ansible-playbook /tmp/ansible/test-playbook.yml -l somenode -e "var1=True var2=5" -t tag2
Or something like this. Does it do what you want? I tried it briefly, but couldn't get a correct result with your example.
Yes and it and works, here's output
$ ansible-playbook /tmp/ansible/test-playbook.yml -l somenode -t tag2 -e 'var1=True var2=5'
PLAY [all] *********************************************************************
TASK [setup] *******************************************************************
ok: [somenode]
TASK [test-playbook : debug] ***************************************************
ok: [somenode] => {
"msg": "tag2, var1 True, var2 5"
}
PLAY RECAP *********************************************************************
somenode : ok=2 changed=0 unreachable=0 failed=0
What result you get? Is there anything missing in the steps to reproduce the problem?
tag2
seems to work, but try tag1
- it runs both tags for me. If I remove , tags: tag1
from the playbook it looks good. I don't know if it is supposed to work that way - I haven't played enough with Ansible, yet.
Doesn't seem to work in Rundeck, though. I guess the extra parameters get messed up when passed to Ansible, due to spaces or whatnot. Need to take a closer look at my AnsibleRunner later.
That should work in that way, -t tag1
is tag for playbook role and tag2 should be separated. In this example tagged role does not make much sense but it is useful when the playbook is more complex. Sorry for the confusion and thanks for accepted bug.
I changed the extraArgs handling a bit. Can you please try again with the latest version?
I tried with extra arguments -t tag2 -e var1=True var2=5
but it did not help.
Using /etc/ansible/ansible.cfg as config fileERROR! the playbook: var2=5 could not be found
Execution failed: 179: [Workflow result: , step failures: {1=Dispatch failed on 1 nodes: [somenode: AnsibleNonZero: Ansible exited with non-zero code.]}, Node failures: {somenode=[AnsibleNonZero: Ansible exited with non-zero code.]}, status: failed]
Have you tried this manually with Ansible? I think you need to wrap those extra args in quotes, if you have multiple. Try: -t tag2 -e "var1=True var2=5"
Extra arguments have more sense now, works flawlessly. Thanks.
Awesome! :) Closing this now, feel free to open a new issue for anything else.
Only can specify the path to the playbook.