www.bundesbrandschatzamt.de
Babblings about Systems Administration.

Org Tables

If you use org mode to do literate devops you probably know that you can use tables like in the following example:

#+NAME: foo
| hostname |
|----------|
| host1    |
| host2    |


#+BEGIN_SRC sh :session client :var HOSTS=foo[,0] :cache no :results verbatim drawer

for HOST in $HOSTS; do
echo "----------------------------------------"
echo ${HOST}
echo "----------------------------------------"
done
#+END_SRC

#+RESULTS:
:RESULTS:

----------------------------------------
host1
----------------------------------------
----------------------------------------
host2
----------------------------------------
:END:

The problem: My tables like any tables have usually more than one column. And I need access to all of them. Here you see an example where I achieved that:

#+NAME: mysql-hosts
| hostname       | instance-id         | ElasticIP allocation-id |          eth0 |
|----------------+---------------------+-------------------------+---------------|
| mysql-stage001 | i-0515f26034190fdd0 | eipalloc-3aba7b43       | 192.168.42.11 |
| mysql-stage002 | i-e944eff8440427030 | eipalloc-30c10049       | 192.168.42.12 |
| mysql-stage003 | i-0d3d55b5feec1c0af | eipalloc-75b4750c       | 192.168.42.13 |
| mysql-stage004 | i-ba39c83390d815730 | eipalloc-a8af6ed1       | 192.168.42.14 |
| mysql-stage005 | i-c9e117eb59ac4c304 | eipalloc-caac6db3       | 192.168.42.15 |
| mysql-stage006 | i-093edb05426675c24 | eipalloc-b4bd7ccd       | 192.168.42.16 |

Set ec2 tag Name for all instances in above table:

#+BEGIN_SRC sh :session client :var HOSTS=mysql-hosts[,0] INSTANCEIDS=mysql-hosts[,1] :cache no :results verbatim drawer
  HOSTS=($HOSTS)
  INSTANCEIDS=($INSTANCEIDS)
  export AWS_DEFAULT_PROFILE=test

  for INDEX in ${!HOSTS[@]}; do
      aws ec2 create-tags --resources ${INSTANCEIDS[${INDEX}]} --tags Key=Name,Value=${HOSTS[${INDEX}]}
  done
#+END_SRC

At first you have to name the table. Then you can access the columns. In your code block - bash in my example - you convert the space seperated values in the variables into arrays. Then you use a for loop over the index. Now you can access all columns of the table. Sure you have to name each column separately in you src block. But that is something at least I can live with.

Here is another example were we use the output of one src block as the input for the next src block:

#+NAME:start-stage-instances-mysql-test
#+BEGIN_SRC sh :results table
  aws --profile=test --region=us-east-1 ec2 describe-instances \
      --query 'Reservations[].Instances[].[join(`,`,Tags[?Key==`Name`].Value),join(`,`,Tags[?Key==`Schedule`].Value),InstanceId, State.Name, PrivateIpAddress, PublicDnsName, InstanceType,Placement.AvailabilityZone,LaunchTime]' \
      --output text --filters "Name=tag:Name,Values=mysql-test00*" | sort
#+END_SRC

#+RESULTS: start-stage-instances-mysql-test
| mysql-test001 |   | i-11e377f61f20b7410 | stopped | 192.168.42.1 | ec2-66-66-42-1.compute-1.amazonaws.com | r3.4xlarge | us-east-1a | 2016-06-10T12:28:16.000Z |
| mysql-test002 |   | i-a5d8a2262050900c6 | stopped | 192.168.42.2 | ec2-66-66-42-2.compute-1.amazonaws.com | r3.4xlarge | us-east-1a | 2016-06-10T12:28:16.000Z |
| mysql-test003 |   | i-5da6d60990701576d | stopped | 192.168.42.3 | ec2-66-66-42-3.compute-1.amazonaws.com | r3.4xlarge | us-east-1a | 2016-06-10T12:28:16.000Z |
| mysql-test004 |   | i-05060c9940a07762e | stopped | 192.168.42.4 | ec2-66-66-42-4.compute-1.amazonaws.com | r3.4xlarge | us-east-1a | 2016-06-10T12:28:16.000Z |
| mysql-test005 |   | i-83b07766d8a0f720b | stopped | 192.168.42.5 | ec2-66-66-42-5.compute-1.amazonaws.com | r3.4xlarge | us-east-1a | 2016-06-10T12:28:16.000Z |
| mysql-test006 |   | i-eb9718fcb210c39c3 | stopped | 192.168.42.6 | ec2-66-66-42-6.compute-1.amazonaws.com | r3.4xlarge | us-east-1a | 2016-06-10T12:28:16.000Z |


#+BEGIN_SRC sh :results verbatim drawer :var INSTANCEIDS=start-stage-instances-mysql-test[,2]
  aws --profile=test --region=us-east-1 ec2 stop-instances --instance-ids ${INSTANCEIDS}
#+END_SRC

I hope I could help you a little bit to improve your literate devops. Recently I found ox-jira. Since then the tickets I am working on improved heavily.