Friday, December 9, 2016

Ansible 2.2 with_items Deprecation

Prior to 2.2, this worked
- name: do something
# do something here
with_items: ec2.instances

However, this has been deprecated. Changelog item: "with_ 'bare variable' handling, now loop items must always be templated {{ }} or they will be considered as plain strings"

It's now required to do this:
- name: do something
# do something here
with_items: "{{ ec2.instances }}"


References:

Friday, September 30, 2016

MongoDB "root" Role

To perform functions such as replication administration, the "dbAdminAnyDatabase" role is not enough. The "root" role is required".
db.grantRolesToUser("admin",[{role:"root","db":"admin"}])

Reference: http://stackoverflow.com/questions/23943651/mongodb-admin-user-not-authorized

Ansible Move File/Folder in Idempotent Manner

Solution is to use conditionals:

       - name: stat the dest path
         stat: path=/path/dest
         register: statdest
       - name: move file
         command: mv /path/src /path/dest
         when: statdest.stat.exists == false

Reference: http://stackoverflow.com/a/24165405/548272

Thursday, September 1, 2016

Bash: Easy Way to Tokenize Strings Separated by Space

Using the "set" command, it is possible to replace the position parameters with the one assigned. This can then be used to refer to individual elements of a string separated by space.

$ str="one two three"
$ set -- $str
$ echo $1
one
$ echo $2
two
$ echo $3
three

Reference: http://stackoverflow.com/questions/5382712/bash-how-to-tokenize-a-string-variable

Ansible Relative Path with playbook_dir

The playbook_dir resolves to the directory where the playbook file resides. You can use relative path from there to reference other files.

Ansible Variables

Good resource about variables which compiles all the different ways variables are used in Ansible: https://liquidat.wordpress.com/2016/01/26/howto-introduction-to-ansible-variables/

Ansible Variables from File

In the task e.g.
- hosts: appservers
 vars_files:
       - vars.yml

vars.yml e.g.:
var1Name: var1Value
var2Name: var2Value
var3Name: var3Value

References:

Wednesday, August 31, 2016

AWS IAM Policy Statement to Register and Deregister Instances with/from ELB

E.g.

{
   "Version": "2012-10-17",
   "Statement": [
       {
           "Sid": "Stmtxxxxxxxxx",
           "Effect": "Allow",
           "Action": [
               "elasticloadbalancing:RegisterInstancesWithLoadBalancer",
               "elasticloadbalancing:DeregisterInstancesFromLoadBalancer"
           ],
           "Resource": [
               "arn:aws:elasticloadbalancing:aws-region:aws-account-id:loadbalancer/*"
           ]
       }
   ]
}

AWS IAM Permissions to Create Tags

Had a hard time getting this to work, and it turns out that only permissions to the "ec2:CreateTags" action is needed, however, the moment the resource is constrained (even to all resources within a specific region), I was unable to tag an EC2 instance. I was only able to tag an EC2 instance when the "Resource" was set to *

{
   "Version": "2012-10-17",
   "Statement": [
       {
           "Sid": "Stmtxxxxxxxx",
           "Effect": "Allow",
           "Action": [
               "ec2:CreateTags"
           ],
           "Resource": [
               "*"
           ]
       }
   ]
}

Monday, August 29, 2016

AWS CloudWatch Logs: Create Log Stream via Command Line


aws logs create-log-stream --log-group-name groupName --log-stream-name streamName

Ansible: Running a Command on the Local Machine

To run an command on the shell of Ansible machine (i.e. instead of the target machine), use the local_action module:

local_action: command xxxxxxxxx

Ansible: EC2 Instance ID Variable


{{ ansible_ec2_instance_id }}

MongoDB: Query for Documents Based On Array Size


db.collectionName.count({"arrayName":{$exists:true},$where:'this.arrayName.length>3'})

Reference: http://stackoverflow.com/questions/7811163/query-for-documents-where-array-size-is-greater-than-1

Tuesday, August 9, 2016

Ant Java Compilation Classpath

If there are many jar files in the classpath, instead of specifying all of them, include all jar files in a folder this way: http://stackoverflow.com/questions/6103516/ant-adding-multiple-jars-in-classpath-dynamically

Amazon Linux Install Ansible

The YUM method won't work, due to differing python versions.

Use pip instead.
sudo pip install ansible