Can ansible variables be used to declare hosts in a playbook?

被刻印的时光 ゝ 提交于 2020-12-06 03:40:29

问题


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?


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/51519735/can-ansible-variables-be-used-to-declare-hosts-in-a-playbook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!