1

F.e. I created two roles via "ansible-galaxy init": deploy_back and deploy_front

Also I wrote a playbook.yml file.

- name: Create cluster
  become: yes
  hosts: all

  roles:
  - deploy_front
  - deploy_back

If I launch it via "ansible-playbook playbook.yml" - then on both servers gonna be deployed two roles. How can I deploy different roles on different servers in one playbook?

If I write playbook.yml

- name: Create cluster
  become: yes

  hosts: front
  roles:
  - deploy_front

  hosts: db
  roles:
  - deploy_db

then outputs looks like "...found a duplicate dict key (hosts)... found a duplicate dict key (roles).... Using last defined value only."

0

You must separate your hosts: and other declarations into different plays. The code should look something like this:

- name: Create front cluster
  become: yes
  hosts: front
  roles:
  - deploy_front

- name: Create db cluster
  become: yes
  remote_user: different-db-user
  hosts: db
  roles:
  - deploy_db
  • Thanks, But how can I give a general name to entire work (including deploying db, front, back servers) ? – Dmitry Medvedev May 28 at 13:52
  • Put the 1st play (Create front cluster) into playbook1.yml and the 2nd play (Create db cluster) into playbook2.yml. Create playbook.yml (General Central ...) and import_playbook playbook1.yml and playbook2.yml. – Vladimir Botka May 28 at 15:31
0

The reason you get a message of "duplicate key" is due how the YAML is structured.

A playbook is a collection of plays, in the form of an ordered list. Each play is specified by a series of keyword/value pairs. Common play keywords are: name, hosts, become, and roles, to name a few.

In YAML, to specify different items in a list, you use the dash (-) character. In both of your example playbook.xml playbooks, you only have one left-most dash character. That means you only have a one item list for a playbook, or a one play playbook.

If you add a dash character before your second hosts declaration in to your second example playbook.yml, you will have a valid playbook that now contains two plays:

- name: Create cluster
  become: yes

  hosts: front
  roles:
  - deploy_front

- hosts: db
  roles:
  - deploy_db

In your original second example, only a single play is defined. Moreover, both the hosts and the roles keyword is defined twice in that one play. YAML is complaining that you have two different definitions for both the hosts and roles keyword.

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.