2

I have two inventories, staging and production.
staging contents:

[webserver]
192.168.56.101  #server 1
192.168.56.103  #server 2

production contents:

[webserver]
192.168.56.108  #server test

I pull an archive file from server 1 and want to deliver it to server test then unpack it.
my ansible script is like:

- name: fetch archived file to local machine
  fetch:
    src: /tmp/code_release_version_{{ release_version }}.tar.gz
    dest: /tmp/code_release_version_{{ release_version }}.tar.gz
    flat: yes
  tags: test

- name: copy archived file to another remote server on /tmp directory
  copy:
    src: /tmp/code_release_version_{{ release_version }}.tar.gz
    dest: /tmp/code_release_version_{{ release_version }}.tar.gz
  delegate_to: 192.168.56.108
  tags: test

- name: extract files
  unarchive:
    src: /tmp/code_release_version_{{ release_version }}.tar.gz
    dest: /var/www
    copy: no
  delegate_to: 192.168.56.108
  tags: test

I play:

ansible-playbook -i staging --extra-vars "host=webserver[0] user=emma release_version=1" --ask-sudo-pass playbook.yml --tags "test"

how to get host from inventory instead of write the host manually like I do delegate_to: 192.168.56.108 ?

Thank you before.

0

i don't think you can use the same group name for different groups of servers (whether in different environments or not).

you're using the inventory file for one environment when you run the playbook:

ansible-playbook -i staging ... --extra-vars "host=webserver[0]" playbook.yml 

instead, you might try having a third inventory file for this kind of tasks where all of your servers are included.

i'm guessing you playbook.yml is something like:

hosts: "{{ host }}"
tasks:
- name: fetch archived file to local machine
  fetch:
...

- name: copy archived file to another remote server on /tmp directory
  copy:
..
  delegate_to: 192.168.56.108

so a workaround to avoid the delegate_to using the all included inventory file (let's named it: all):

[webserverdev]
192.168.56.101  #server 1
192.168.56.103  #server 2

[webserverprod]
192.168.56.108  #server test

then a playbook like the following:

hosts: webserverdev[0]
tasks:
- name: fetch archived file to local machine
  fetch:
...

hosts: webserverprod[0]
- name: copy archived file to another remote server on /tmp directory
  copy:
..

running the playbook using the new inventory:

ansible-playbook -i all ... playbook.yml 
  • my playbook.yml was like you guessed, i'm using roles implementation. – Fatimah Wulandari Nov 6 '15 at 2:55
  • the trick is to have in one playbook more than one - hosts: blocks, i mean, to avoid delegate_to on tasks – Osvaldo Nov 8 '15 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.