Docker and Ansible: running specific tasks on a dynamically defined host list

85 views
Skip to first unread message

Kamesh Sampath

unread,
Sep 15, 2015, 9:14:50 AM9/15/15
to Ansible Project
Hello,

I am running docker swarm with Ansible, my playbook deploys a docker image on specific node  using swarm, post that deployment I want to execute certain Ansible tasks on the host where the image is deployed (still running from the same playbook role), as I am using consul as my backend I am able to retrieve the host(s) where the image is deployed and used "add_host" to create a host group. 

I don't know how to trigger the tasks via "include" on that hosts. Any thoughts how to achieve that ?

Below are the references of the main.yml of the role and the include file.

Regards,
Kamesh

main.yml

---

- set_fact:
    portal_compose_dir: "{{ compose_configs_dir }}/{{app_name}}/portal"
  tags: [liferay]

- set_fact:
    liferay_home_dir: "{{liferay_base_dir}}/{{app_name}}"
  tags: [liferay]

- include: rollback.yml
  when: rollback_app is defined

- name: "Create Liferay Directories"
  file:
    dest: "{{ item }}"
    state: directory
  with_items:
    - "{{ portal_compose_dir }}"
  tags: [liferay]

- name: "Liferay App Swarm Filters"
  template:
    src: constraints.env.j2
    dest: "{{ portal_compose_dir }}/constraints.env"
  tags: [liferay]

- name: "Liferay Docker Compose"
  template:
    src: "liferay-compose.yml.j2"
    dest: "{{ portal_compose_dir }}/docker-compose.yml"
  tags: [liferay]

## TODO Refresh versions
- name: "Pull the Liferay App"
  shell: docker-compose pull lportal
  args: 
    chdir: "{{ portal_compose_dir}}"
  environment:
    DOCKER_HOST: "tcp://{{ ip }}:2376"
    DOCKER_CERT_PATH: "/home/{{ ansible_ssh_user }}/.docker"
    DOCKER_TLS_VERIFY: 1
    PATH: "{{ path }}"
  tags: [liferay]

- name: "Spin up Liferay Container"
  shell: docker-compose up -d --no-recreate lportal
  args: 
    chdir: "{{ portal_compose_dir }}"
  environment:
    DOCKER_HOST: "tcp://{{ ip }}:2376"
    DOCKER_CERT_PATH: "/home/{{ ansible_ssh_user }}/.docker"
    DOCKER_TLS_VERIFY: 1
    PATH: "{{ path }}"
  tags: [liferay]

- name: "Liferay Service Info"
  get_url: 
    dest: /tmp/liferay_service_info.json
  tags: [liferay-config]

- shell: "cat /tmp/liferay_service_info.json |  jq --raw-output '.[] | .Address ' "
  environment:
    PATH: "{{ path }}"
  register: liferay_hosts_info
  tags: [liferay-config]

- debug: var=liferay_hosts_info.stdout
  ignore_errors: true
  tags: [liferay-config]

- add_host: 
    groups: liferay_deployed_hosts
    name: "{{ liferay_hosts_info.stdout }}"
  tags: [liferay-config]

- include: deploy-portal-ext.yml
  tags: [liferay-config]



deploy-portal-ext.yml

---
- hosts: liferay_deployed_hosts
  remote_user: vagrant
  sudo: yes
  tasks:
  handlers:

 - name: "Create Liferay Directories"
    file:
      dest: "{{ item }}"
      state: directory
    with_items:
      - "{{ liferay_home_dir }}"
      - "{{ liferay_home_dir }}/www/html"
    tags: [liferay-config]

  - name: "Liferay Portal Configuration"
    template:
      src: portal-setup-wizard.properties.ctmpl.j2
      dest: "{{ portal_compose_dir }}/portal-setup-wizard.properties.ctmpl"
    tags: [liferay-config]

  - name: "Deploy Portal Ext Properties"
    shell: >
      consul-template \
        -consul "{{ip}}:8500" \
        -template "{{portal_compose_dir}}/portal-setup-wizard.properties.ctmpl:{{ liferay_home_dir}}/portal-setup-wizard.properties" \
        -once
    notify: restart liferay
    environment: 
      PATH: "{{ path }}"
    tags: [liferay-config]




Reply all
Reply to author
Forward
0 new messages