1

Problem

I have a simple Ansible playbook that creates a list of tmux sessions and then runs a script inside of each session. I am trying to give the tmux sessions names in my vars.yml file.

My problem is that I want to run the same command in all of the tmux sessions I create. Here is the simple playbook I have.

Playbook

Obviously the playbook is broken in its current form. I having a hard time of figuring out how to:

  1. Navigate to the correct directory for each unique tmux session (to a directory with the same name as the session)
  2. Start a script (same name shared in all sessions) in the folder navigated to

.

---
- hosts: all

  vars_file:
    - vars.yml

  tasks:
    - name: "Create tmux sessions for each server."
      command: tmux new -d -s {{ servers }}

    - name: "Start each server in its tmux session."
      shell: >
        tmux send-keys -t {{ servers }} "./start.sh" Enter

Variables

---
# Name of all tmux sessions running on server
servers:
  - creative
  - development
  - lobby
  - proxy
  - survival
  - workflow

Any tips for how I might be able to intelligently refer to the current variable executing within the shell command (e.g. cd ~/{{ current_variable }}/scripts/ && ./start.sh)? Thanks!

J.W.F.
  • 328
  • 2
  • 4
  • 15

1 Answers1

2

You need to iterate over the servers variable in your playbook:

In ansible 1.x this would be done:

---
- hosts: all

  vars_file:
    - vars.yml

  tasks:
    - name: "Create tmux sessions for each server."
      command: tmux new -d -s {{ item }}
      with_items: servers

    - name: "Start each server in its tmux session."
      shell: >
        tmux send-keys -t {{ item }} "./start.sh" Enter
      with_items: servers

In ansible 2.x you need to quote the variable used by the with_items directive: "{{ servers }}".

dawud
  • 14,918
  • 3
  • 41
  • 61