- name: provision test keypair
  hosts: local
  connection: local
  vars_files:
  - ../../vars/main.yml
  tasks:
    - name: create the keypair
      register: test_keypair
      local_action: nova_keypair name={{ test_keypair_name }} state=present
    - name: persist the keypair
      shell: echo "{{ test_keypair.key }}" > {{ test_keypair_path }}
      when: test_keypair.changed == True
    - name: correct keypair permissions
      shell: chmod 600 {{ test_keypair_path }}
      when: test_keypair.changed == True

- name: provision test keypair
  hosts: local
  connection: local
  vars_files:
  - ../../vars/main.yml
  tasks:
    - name: create the keypair
      register: test_keypair
      local_action: nova_keypair name={{ test_keypair_name }} state=present
    - name: persist the keypair
      shell: OLD_UMASK=`umask`; umask 0077; echo "{{ test_keypair.key }}" > {{ test_keypair_path }}; umask $OLD_UMASK
      when: test_keypair.changed == True

Use umask to write key with correct permissions
Use umask to write key with correct permissions

Alter umask before writing key, then write key and restore umask rather
than writing key with incorrect permissions then go back and fix
permissions.
