Category Archives: Ansible

Ansible

How do I generate crypted passwords for the user module?
mkpasswd --method=SHA-512

Ad-Hoc Commands
ansible -i env/XXX/hosts group_you_want -a "sudo sed -i 's/DNS1.*//;s/DNS2.*//' /etc/sysconfig/network-scripts/ifcfg-ens192" -u xx --become

Show Environment Variables values:
ansible -i env/XXX/hosts -m debug -a "var=hostvars" group_you_want

Show hosts values:
ansible -i env/XXX/hosts -m setup group_you_want

Use another private-key:
ansible -i env/XXX/host --private-key=~/.ssh/anotheruser ....

Jinja all hosts ips:
{% for host in groups['group_you_want'] %}
blablacommand {{ hostvars[host]['ansible_default_ipv4']['address'] }}
{% endfor %}

Ansible and Google Compute Engine

Install dependencies

pip install apache-libcloud

Then install the gcloud. Visit: https://cloud.google.com/sdk/downloads

Create and Download your service account p12
Visit: https://console.developers.google.com/permissions/serviceaccounts

Convert the p12 format to pem

openssl pkcs12 -in ~/.ssh/foo.p12 -passin pass:notasecret -nodes -nocerts | openssl rsa -out ~/.ssh/foo.pem

To list google images

gcloud compute images list

To list google machine-type

gcloud compute machine-types list

To check your service account mail
Visit https://console.developers.google.com/projectselector/permissions/serviceaccounts

Create a playbook

cat > gce_def.yml
- name: Compute Engine demo
  hosts: localhost
  gather_facts: False
  vars:
    pid: my-first111134
    email: 1234567890-xxxxxxxxxxxxx@developer.gserviceaccount.com
    pem: /etc/ansible/pkey.pem
  tasks:
    - name: Create instance
      gce:
        instance_names: demo1,demo2
        zone: us-central1-b
        machine_type: n1-standard-1
        image: debian-7-wheezy
        project_id: "{{ pid }}"
        pem_file: "{{ pem }}"
        service_account_email: "{{ email }}"

List running instances

gcloud compute instances list