I'm passing the IP as parameter as mentioned here: https://stackoverflow.com/a/18255256/1784001
ansible-playbook roles/example/main.yml -i 127.0.0.1,
Is there any way to access the value for the inventory parameter, "127.0.0.1" in a playbook?
I checked the special variables, but I see no mention of it: https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html
In some of the tasks I need that value, for example creating backup directories, or scp-ing to the host.
inventory_hostname always contains the inventory hostname of the host the play is running at.
The parameter -i "specify inventory host path or comma separated host list." Running the playbook main.yml
- hosts: all
tasks:
- debug: var=inventory_hostname
With the command
$ ansible-playbook -i 127.0.0.1, main.yml
gives
ok: [127.0.0.1] =>
inventory_hostname: 127.0.0.1
Related
I do have a playbook which tooks a specific group and put all hosts of this group into a command on another host.
to be more precise.
all hosts from the hosts group oldservers from my inventory file must be in the /etc/ssh.conf on one or multiple clients.
the task looks like...
---
- name: echo Old Servers
debug:
var: groups["oldservers"]
- name: create ssh_conf_for_old_server
blockinfile:
path: /etc/ssh/ssh_config
backup: True
block: |
Host {{ groups["oldservers"]|join(' ') }}
user admin
KexAlgorithms +diffie-hellman-group1-sha1
HostKeyAlgorithms +ssh-dss
Ciphers +aes128-cbc
this should be executed on a client which is not member of the group servers.
hosts file (inventory):
[clients]
192.168.200.1
192.168.200.2
[oldservers]
192.168.201.1
192.168.201.2
My execution line is ansible-playbook -i 192.168.200.1, -u ansible ./createServerList.yml
I guess I should do it a bit different. Dont I ?
The result should be ... at first output all the oldservers (debug)
than write a block with these old server into the /etc/ssh/ssh_config
For command ansible-playbook -i 192.168.200.1 -u ansible ./createServerList.yml, you are passing the ip address directly as inventory. Because of this Ansible is unaware of the inventory file where host groups are defined. So can you try running this instead ansible-playbook -i <path_to_inventory_file> -u ansible ./createServerList.yml
And then if you have to restrict playbook running only certain hosts or group, do
ansible-playbook -i <path_to_inventory_file> -u ansible ./createServerList.yml --limit "192.168.200.1,192.168.200.2"
OR
ansible-playbook -i <path_to_inventory_file> -u ansible ./createServerList.yml --limit clients
The docs specify that I can run my playbook on a specific host using -i:
Patterns and ansible-playbook flags
You can change the behavior of the patterns defined in playbooks using command-line options. For example, you can run a playbook that defines hosts: all on a single host by specifying -i 127.0.0.2,. This works even if the host you target is not defined in your inventory. You can also limit the hosts you target on a particular run with the --limit flag:*
However, I tried running ansible-playbook <playbook> -i <new_hostname> -u <username> and the inventory used was still my default one. How to use this correctly?
Quoting from "man ansible"
-i, --inventory, --inventory-file
specify inventory host path or comma separated host list.
To specify a single host as a "comma separated host list", the comma is still needed. For example, the playbook
shell> cat playbook.yml
- hosts: all
gather_facts: false
tasks:
- debug:
var: inventory_hostname
gives
shell> ansible-playbook -i test_99, playbook.yml
PLAY [all] ****
TASK [debug] ****
ok: [test_99] =>
inventory_hostname: test_99
Without the comma after the host, Ansible takes the argument as an "inventory host path".
shell> ansible-playbook -i test_99 playbook.yml
[WARNING]: Unable to parse /scratch/test_99 as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit
localhost does not match 'all'
PLAY [all] ****
skipping: no hosts matched
I do not wish to specify any hosts file to ansible-playbook command.
ansible-playbook site.yml -e "source_host=mymac1 source_file=myfile1"
My site.yml looks like this:
more site.yml
---
- hosts: "{{ source_host | default('my_pc')}}"
user: weblogic
However, I get the following error:
[WARNING]: Could not match supplied host pattern, ignoring: all
[WARNING]: provided hosts list is empty, only localhost is available
PLAYBOOK: site.yml
********************************************************************************************************************************************************************************** 2 plays in site.yml [WARNING]: Could not match supplied host pattern,
ignoring: mymac1
Can you please suggest how can i pass any host to my playbook without having to maintain and host respository with all the host information
I am on ansible version 2.3.1.0
You can use inline inventory:
playbook.yml:
- hosts: all
tasks:
- debug: msg=hello
command:
ansible-playbook -i 'mymac1,' -e source_file=myfile1 playbook.yml
note comma after hostname.
Also see: Ansible ad-hoc command with direct host specified - no hosts matched
quick question for Ansible Guru's. I want to run an ansible playbook for a specific set of boxes that I copied to a list.txt disregarding the inventory and the target block in ansible playbook:
---
- name: Ansible Runbook v.1.0
hosts: test1
gather_facts: yes
# serial: "10%"
When I am running the following command I am getting no hosts matched:
ansible-playbook playbook.yaml --tags "simplejson" -vvv -i /x/home/list.txt
PLAY [Ansible Runbook v.1.0] **************************************************
skipping: no hosts matched
$cat list.txt
hostname2b
Any ideas for a workaround ?
The reason of no host matching is that host test1, which is hardcoded in playbook, is not present in the inventory file that you specified from command line. The problem is ansible-playbook command does not accept any hosts parameter. So there is no direct way of getting around the hardcoded hosts test1.
However, there is a workaround for this as explained here. You can use a variable for hosts and specify all from command line for that variable. Something like this:
---
- name: Ansible Runbook v.1.0
hosts: "{{ host_param }}"
gather_facts: yes
Then pass that variable with extra-vars:
ansible-playbook playbook.yaml -i /x/home/list.txt --extra-vars="host_param=all" --tags "simplejson" -vvv
Can you pass a json array: --extra-vars "{server: [bo-121.q.net,mer-122.q.net,mer-123.q.net]}" to the host var? I want the playbook to work for one or many hosts.
It is my first boot tasks that runs on all hosts.
playbook yml:
- hosts: "$server"
If I understood the question correctly I think you can accomplish this by giving host names or groups as command line. E.g. if I want to deploy the staging server only:
ansible-playbook -i hosts.ini playbook.yml --limit staging
hosts.ini is
[default]
staging ansible_ssh_host=x.x.x.x ansible_ssh_user=x
ansible-playbook mesos.yml --extra-vars "cluster=mesos1"
mesos.yml
- hosts: '{{ cluster }}'
group / host needs to be in your inventory file.