3

I have a playbook in the format below:

---
- hosts: myIP
  tasks:
  - name: Install a yum package in Ansible example
    yum:
      name: ThePackageIWantToInstall
      state: present

where myIP and ThePackageIWantToInstall are variables.

When the job template runs, I would like the user in the extra variables popup to be able to go with:

myIP = 192.168.1.1
ThePackageIWantToInstall = nano

As the documentation doesn't provide an example of supplying a variable via a job template, is this possible?

robe007
  • 3,523
  • 4
  • 33
  • 59

2 Answers2

2

Yes.

- name: Do The Thing
  hosts: "{{ foo }}"
  roles:
   - "{{ role }}"

Need mustaches and quotes.


to run from popup

(I don't use this, but it was suggested as an edit, thanks...)

foo: value

Paul Hodges
  • 13,382
  • 1
  • 17
  • 36
  • right and then when I assign them in the extra variables pop up is it litterally -foo: 192.188.1.1? – turtleduck2018 Jul 25 '18 at 13:27
  • I executed the test with `ansible-playbook -i Jenkins.sh site.yml --extra-vars='foo=each bar=test'` (I use a dynamic hosts script, but that's not relevant here.) I don't use the popup. Refer to the documentation for that and do some testing to get the syntax right. – Paul Hodges Jul 25 '18 at 13:29
1

I have achieved similar thing with add_hosts. Here iam not installing package but creating file with name passed from command line. Any number of hosts (separated by commas can be passed from command line).

# cat addhost2.yml
- hosts: localhost
  gather_facts: no
  tasks:
    - add_host:
        name: "{{ item }}"
        groups: hosts_from_commandline
      with_items: "{{ new_hosts_passed.split(',') }}"


- hosts: hosts_from_commandline
  tasks:
    - name: Ansible create file with name passed from commandline
      file:
        path: "/tmp/{{ filename_from_commandline }}"
        state: touch
# ansible-playbook -i hosts addhost2.yml --extra-vars='new_hosts_passed=192.168.3.104,192.168.3.113 filename_from_commandline=lathamdkv'

Hope this helps

Sudhakar MNSR
  • 594
  • 1
  • 3
  • 17