0

I would like to override the target_host variable from tower into the playbook. It's easy to override any other playbook variable, but I can't seem to get hosts value to work; always complains that no host was specified.

---
- hosts: "{{ target_host }}"
  gather_facts: no
  vars:
    target_host: "10.80.100.163,"
  remote_user: root

  tasks:
  - name: Add users | create users, shell, home dirs
    user:
      name: bubba
      shell: /bin/bash
      createhome: yes
      password: $6$pGO4DKLQ$Eu97vmle/Zvb53gVCXGecfZzvYVd4twj8/EOMwmbYgCUkRAxsWQVXtFrxdZGal6hSLnY..5b/4x1MweH5ierz.
      comment: "Created with Ansible"

Hoping not to have to (learn how to) create a dynamic inventory for one IP, is there a way to do this?

thanks!

5
  • Does it even make sense to "override" this at all? You could just run the playbook against whatever host you want. May 14, 2019 at 4:46
  • host will not be known till runtime.
    – jacksonp
    May 14, 2019 at 4:48
  • And it's a host other than the one you specified in Tower? May 14, 2019 at 4:49
  • correct, it is not static. Tower API will receive the "target_host" from the application and needs to pass it to a playbook when it runs. Some examples show above syntax but it sure does't work for me.
    – jacksonp
    May 14, 2019 at 4:52
  • Hm, I'll think about that a bit. I'm OK with Ansible but I haven't quite settled into Tower yet. May 14, 2019 at 4:53

2 Answers 2

1

You can use ansible module: add_host https://docs.ansible.com/ansible/latest/modules/add_host_module.html

---
- hosts: "localhost"
  gather_facts: no
  tasks:
  - name: Add host
    add_host:
      hostname: "{{ your_new_hostname }}"
      groups: "group_for_new_hostname"
      ansible_user: "{{ ssh_user }}"
      ansible_ssh_pass: "{{ ssh_pass }}"

- hosts: "group_for_new_hostname"
  tasks:
  - name: Add users | create users, shell, home dirs
    user:
      name: bubba
      shell: /bin/bash
      createhome: yes
      password: $6$pGO4DKLQ$Eu97vmle/Zvb53gVCXGecfZzvYVd4twj8/EOMwmbYgCUkRAxsWQVXtFrxdZGal6hSLnY..5b/4x1MweH5ierz.
      comment: "Created with Ansible"
0

Assuming your playbook file is pb.yaml and it looks like:

---
- name: A playbook name
  #  by default all belonging to tomcat childs go affected
  #  use -e varmcat=someinvhost to override target host explicitly
  hosts: "{{ vartomcat | default('tomcat') }}"
  roles:
    - role: role1
    - role: role2

  tasks:
    - name: task
      ....... 

A sample inventory inv.yaml file for is like:

---
all:
  hosts:
    hostA: &hostA
      ansible_host: serverA.domain.com
    hostX1: &hostX1
      ansible_host: serverX1.domain.com
    hostX2: &hostX2
      ansible_host: serverX2.domain.com
    hostB: &hostB
      ansible_host: serverB.domain.com
    hostY1: &hostY1
      ansible_host: serverY1.domain.com
    hostY2: &hostY2
      ansible_host: serverY2.domain.com

  children:
    tomcat:
      hosts:
        hostA:
        hifsB:
    groupX:
      hosts:
        hostX1:
        hifsX2:
    groupY:
      hosts:
        hostY1:
        hifsY2:

And so you can override vartomcat to either particlar host or some other group

ansible-playbook -i inv.yaml pb.yaml -e vartomcat=hostA

ansible-playbook -i inv.yaml pb.yaml -e vartomcat=hostX1

ansible-playbook -i inv.yaml pb.yaml -e vartomcat=groupY

if you do not override:

ansible-playbook -i inv.yaml pb.yaml 

the tomcat group is used by default

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .