name: Add HTTP listener rules
elb_application_lb:
state: present
name: "{{ albinfo.load_balancer_name }}"
subnets:
- "{{ albinfo.availability_zones[0].subnet_id }}"
- "{{ albinfo.availability_zones[1].subnet_id }}"
- "{{ albinfo.availability_zones[2].subnet_id }}"
security_groups:
- "{{ albinfo.security_groups[0] }}"
listeners:
- Protocol: HTTP
Port: 80
DefaultActions:
- Type: forward
TargetGroupName: default
Rules:
- Conditions:
- Field: host-header
Values: "{{ item.url }}"
Priority: "{{ item.priority }}"
Actions:
- TargetGroupName: "{{ item.name }}"
Type: forward
purge_listeners: no
with_items: "{{ regions }}"
Related
ec2_snapshot module allows me to create snapshots of volumes and tag at the same time. This is straight forward while using fix names for tags. But how can I set tag name itself from a variable?
Example task:
- name: AWS EBS Disks Snapshot For Volumes
ec2_snapshot:
aws_access_key: "{{ aws_access_key_id }}"
aws_secret_key: "{{ aws_secret_key_id }}"
security_token: "{{ aws_security_token }}"
volume_id: "{{ item.id }}"
region: "{{ aws_region }}"
snapshot_tags:
Name: "{{ timestamp.stdout }}"
"{{ tagname_variable }}": "{{ tagvalue_variable }}"
type: "{{ item.type }}"
description: "{{ timestamp.stdout }}_snapshot"
with_items:
- "{{ volumeinputs }}"
The tagname_variable is literally created as a tag name, not the value of the variable.
How I can make this work?
You will need to dynamically create that part of you dictionary, for example, with a combine, as YAML dictionaries key are usually not templated by Ansible.
This can be done in a vars sections of the task itself:
- name: AWS EBS Disks Snapshot For Volumes
ec2_snapshot:
aws_access_key: "{{ aws_access_key_id }}"
aws_secret_key: "{{ aws_secret_key_id }}"
security_token: "{{ aws_security_token }}"
volume_id: "{{ item.id }}"
region: "{{ aws_region }}"
snapshot_tags: "{{ _snapshot_tags }}"
description: "{{ timestamp.stdout }}_snapshot"
loop: "{{ volumeinputs }}"
vars:
_snapshot_tags_static:
Name: "{{ timestamp.stdout }}"
type: "{{ item.type }}"
_snapshot_tags: >-
{{
_snapshot_tags_static
| combine({tagname_variable: tagvalue_variable})
}}
I created this playbook to deploy new VM from template.
Now if I try to deploy a new Linux VM it works correctly but the hostname won't be customized
It keeps the same hostname from the template and I don't know why ??
Here is my playbook (this is just a test):
- name: Clone multiple VMs
hosts: localhost
gather_facts: false
vars_files:
multiple_vms.yml
tasks:
- name: Clone multiple Ubuntu VMS from Template
local_action:
module: vmware_guest
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
validate_certs: no
folder: "{{ folder }}"
template: "{{ vmtemplate }}"
name: "{{ item }}"
cluster: "{{ vcenter_cluster }}"
datacenter: "{{ vcenter_datacenter }}"
state: poweredon
networks:
- name: "{{ network }}"
type: "{{ network_dhcp_or_static }}"
hardware:
memory_mb: {{ number_memory }}
num_cpus: {{ number_cpu }}
customization:
hostname: "{{ servers }}"
domain : "mydomaine.test"
#customization_spec: "{{ customization_spec }}"
with_items: "{{ servers }}"
where vars multiple_vms.yml:
servers:
- ubuntu_test01
- ubuntu_test02
....
So I need my new VMS to take the hostname from the servers list (ubuntu_test01.mydomaine.test ..etc)
Thanks for the help
Using vmware_guest and customization option
First, try hostname: "{{ item }}"
Second, don't do the loop thing at all. Make a proper inventory, then:
- name: Clone multiple VMs
hosts: all
gather_facts: false
vars_files:
multiple_vms.yml
tasks:
- name: Clone multiple Ubuntu VMS from Template
vmware_guest:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
validate_certs: no
folder: "{{ folder }}"
template: "{{ vmtemplate }}"
name: "{{ ansible_hostname }}"
cluster: "{{ vcenter_cluster }}"
datacenter: "{{ vcenter_datacenter }}"
state: poweredon
networks:
- name: "{{ network }}"
type: "{{ network_dhcp_or_static }}"
hardware:
memory_mb: {{ number_memory }}
num_cpus: {{ number_cpu }}
customization:
hostname: "{{ ansible_hostname }}"
domain : "mydomaine.test"
delegate_to: localhost
N.B.: local_action is deprecated. Use delegate_to: localhost.
In an Ansible task, I want to iterate on all subelements of each dictionary category. The container I am using looks like this:
myDict:
- {category1: el00, category2: [el10, el11], category3: [el20, el21, el22, el23]}
The expected output would be :
msg: el00
msg: el10 el11
msg: el20 el21 el22 el23
If you want to take into account the fact that the variable myDict is a list and that there might be more items create a file
shell> cat iteration.yml
- debug:
msg: "{{ i.0 }} {{ i.1 }} {{ i.2 }}"
with_nested:
- "{{ item.category1 }}"
- "{{ item.category2 }}"
- "{{ item.category3 }}"
loop_control:
loop_var: i
and include it in the loop
- include_tasks: iteration.yml
loop: "{{ myDict }}"
gives
msg: el00 el10 el20
msg: el00 el10 el21
msg: el00 el10 el22
msg: el00 el10 el23
msg: el00 el11 el20
msg: el00 el11 el21
msg: el00 el11 el22
msg: el00 el11 el23
If you want to iterate the first item in the list only the task below gives the same result
- debug:
msg: "{{ item.0 }} {{ item.1 }} {{ item.2 }}"
with_nested:
- "{{ myDict.0.category1 }}"
- "{{ myDict.0.category2 }}"
- "{{ myDict.0.category3 }}"
I`m struggling to get this working without success.. Here my playbook.
First a search my machines by tag and later try to terminate them.
- name: EC2 Facts
ec2_instance_facts:
region: us-east-1
filters:
"tag:Type": "staging"
aws_access_key: "{{ lookup('env', 'AWS_ACCESS_KEY_ID') }}"
aws_secret_key: "{{ lookup('env', 'AWS_SECRET_ACCESS_KEY') }}"
register: ec2
- name: Kill EC2 Instance
ec2:
instance_ids: "{{ item.instance_id }}"
state: "{{ state }}"
region: "{{ lookup('env', 'AWS_REGION') }}"
aws_access_key: "{{ lookup('env', 'AWS_ACCESS_KEY_ID') }}"
aws_secret_key: "{{ lookup('env', 'AWS_SECRET_ACCESS_KEY') }}"
with_items: "{{ ec2.instances }}"
running like this:
ansible-playbook ec2_id_kill.yml --extra-vars "state=absent"
Looks ansible cannot find the instance_id from the facts
I am using this to terminate the single instance, you can adjust this to terminate multiple instance:
- name: EC2 Facts
ec2_instance_info:
region: us-east-1
filters:
"tag:Type": "staging"
aws_access_key: "{{ lookup('env', 'AWS_ACCESS_KEY_ID') }}"
aws_secret_key: "{{ lookup('env', 'AWS_SECRET_ACCESS_KEY') }}"
register: ec2
- name: Kill EC2 Instance
ec2:
instance_ids: "{{ ec2.instances[0].id }}"
state: "{{ state }}"
region: "{{ lookup('env', 'AWS_REGION') }}"
aws_access_key: "{{ lookup('env', 'AWS_ACCESS_KEY_ID') }}"
aws_secret_key: "{{ lookup('env', 'AWS_SECRET_ACCESS_KEY') }}"
Hope that help you
I want to create a series of directory trees dependent on two seperate lists.
Example:
---
# variable file ...
datacenters:
- london
- paris
types:
- databases
- baremetal
- vms
So I want my trees to be like so ...
dest: "/{{ datacenter.0 }}/{{ types.0 }}"
dest: "/{{ datacenter.0 }}/{{ types.1 }}"
dest: "/{{ datacenter.0 }}/{{ types.2 }}"
dest: "/{{ datacenter.1 }}/{{ types.0 }}"
dest: "/{{ datacenter.1 }}/{{ types.1 }}"
dest: "/{{ datacenter.1 }}/{{ types.2 }}"
dest: "/{{ datacenter.N }}/{{ types.N }} .... etc
I'm not exactly sure how to do this without using an include file ....
You can use Nested Loops
- name: Test with_nested
hosts: localhost
vars:
datacenters:
- london
- paris
types:
- databases
- baremetal
- vms
tasks:
- name: Do it
debug: msg="{{item[0]}}/{{item[1]}}"
with_nested:
- datacenters
- types