Reputation: 21
Trying to run script with different arguments on different servers using ansible, example:
server 192.168.0.1 -> fabric.sh generic1 status
server 192.168.0.2 -> fabric.sh generic2dr status
server 192.168.0.3 -> fabric.sh generic3 status
How to use variables in playbook below?
It works when I create group for each server, but it's not efficient
---
- hosts: esb
remote_user: root
tasks:
- name: Generic_1
become_user: esb
shell: "/home/fabric.sh generic1 status"
Host file:
[esb]
192.168.0.1
192.168.0.2
192.168.0.3
Upvotes: 0
Views: 83
Reputation: 67959
Next to Basic inventory there is yaml – Uses a specific YAML file as an inventory source.
For example (fit the variables to your needs):
$ cat hosts
all:
hosts:
10.1.0.51:
10.1.0.52:
10.1.0.53:
vars:
ansible_connection: ssh
ansible_user: admin
ansible_become: yes
ansible_become_user: root
ansible_become_method: sudo
ansible_python_interpreter: /usr/local/bin/python3.6
ansible_perl_interpreter: /usr/local/bin/perl
children:
esb:
hosts:
10.1.0.51:
run_string: "fabric.sh generic1 status"
10.1.0.52:
run_string: "fabric.sh generic2dr status"
10.1.0.53:
run_string: "fabric.sh generic3 status"
The play below
- hosts: esb
tasks:
- debug:
var: run_string
gives (abridged):
ok: [10.1.0.51] => {
"run_string": "generic1 status"
}
ok: [10.1.0.52] => {
"run_string": "generic2dr status"
}
ok: [10.1.0.53] => {
"run_string": "generic3 status"
}
Upvotes: 0
Reputation: 311248
You can set per-host variables in your inventory. For example, modify your inventory so it looks like this:
[esb]
192.168.0.1 fabric_args="generic1 status"
192.168.0.2 fabric_args="generic2dr status"
192.168.0.3 fabric_args="generic3 status"
And then use the fabric_args
variable in your playbook:
---
- hosts: esb
remote_user: root
tasks:
- name: Generic_1
become_user: esb
shell: "/home/fabric.sh {{ fabric_args }}"
For more information, read the Using Variables and Working with Inventory sections of the Ansible documentation.
Upvotes: 1