Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Testing group var precedence
#!/bin/bash
apt-get update && \
apt-get purge -y nano && \
apt-get install -y git vim tmux fail2ban build-essential python2.7 python-dev libssl-dev libffi-dev
curl --silent --show-error --retry 5 https://bootstrap.pypa.io/get-pip.py | sudo python2.7
pip install ansible==2.1.1
curl https://raw.githubusercontent.com/ansible/ansible/devel/lib/ansible/plugins/connection/lxc.py > /usr/local/lib/python2.7/dist-packages/ansible/plugins/connection/lxc.py
[all]
localhost ansible_connection=local physical_host=localhost ansible_become=True ansible_user=root
container1 ansible_connection=lxc physical_host=localhost ansible_become=True ansible_user=root
container2 ansible_connection=lxc physical_host=localhost ansible_become=True ansible_user=root
[hosts]
localhost
[all_containers]
container1
container2
[monitoring_all]
[monitoring_all:children]
monitoring_server
monitoring_agent
[monitoring_server]
container1
[monitoring_agent]
container2
- name: Prepare the hosts
hosts: hosts
tasks:
- name: Install LXC packages
apt:
pkg: "{{ item }}"
state: latest
default_release: "trusty-backports"
with_items:
- lxc
- lxc-dev
tags: prepare-host
- name: Install pip
shell: "curl --silent --show-error --retry 5 https://bootstrap.pypa.io/get-pip.py | sudo python2.7"
tags: prepare-host
- name: Install LXC python library
pip:
name: lxc-python2
state: latest
tags: prepare-host
- name: Create and prepare containers [24/1688]
hosts: all_containers
gather_facts: no
tasks:
- name: Create the containers
lxc_container:
name: "{{ inventory_hostname }}"
template: download
state: started
backing_store: dir
template_options: --dist ubuntu --release trusty --arch amd64
container_log: yes
container_log_level: DEBUG
delegate_to: "{{ physical_host }}"
- name: Deploy the container prep script
copy:
content: |
#!/usr/bin/env bash
set -e -x
export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get install -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" --force-yes python2.7
rm -f /usr/bin/python
ln -s /usr/bin/python2.7 /usr/bin/python
userdel --force --remove ubuntu || true
apt-get clean
dest: "/var/lib/lxc/{{ inventory_hostname }}/rootfs/usr/local/bin/cache-prep-commands.sh"
mode: "0755"
delegate_to: "{{ physical_host }}"
- name: Execute the container prep script
command: "chroot /var/lib/lxc/{{ inventory_hostname }}/rootfs /usr/local/bin/cache-prep-commands.sh"
delegate_to: "{{ physical_host }}"
- name: Check the value of myvar for monitoring_all
hosts: monitoring_all
gather_facts: no
tasks:
- debug:
var: myvar
- name: Check the value of myvar for monitoring_server
hosts: monitoring_server
gather_facts: no
tasks:
- debug:
var: myvar
- name: Check the value of myvar for monitoring_agent
hosts: monitoring_agent
gather_facts: no
tasks:
- debug:
var: myvar
root@ansible1:~# ansible-playbook -i inventory test-playbook.yml
PLAY [Prepare the hosts] *******************************************************
TASK [setup] *******************************************************************
ok: [localhost]
TASK [Install LXC packages] ****************************************************
ok: [localhost] => (item=[u'lxc', u'lxc-dev'])
TASK [Install pip] *************************************************************
changed: [localhost]
[WARNING]: Consider using get_url or uri module rather than running curl
TASK [Install LXC python library] **********************************************
ok: [localhost]
PLAY [Create and prepare containers] *******************************************
TASK [Create the containers] ***************************************************
ok: [container1 -> localhost]
ok: [container2 -> localhost]
TASK [Deploy the container prep script] ****************************************
ok: [container1 -> localhost]
ok: [container2 -> localhost]
TASK [Execute the container prep script] ***************************************
changed: [container2 -> localhost]
changed: [container1 -> localhost]
PLAY [Check the value of myvar for monitoring_all] *****************************
TASK [debug] *******************************************************************
ok: [container2] => {
"myvar": "monitoring_agent"
}
ok: [container1] => {
"myvar": "monitoring_all"
}
PLAY [Check the value of myvar for monitoring_server] **************************
TASK [debug] *******************************************************************
ok: [container1] => {
"myvar": "monitoring_all"
}
PLAY [Check the value of myvar for monitoring_agent] ***************************
TASK [debug] *******************************************************************
ok: [container2] => {
"myvar": "monitoring_agent"
}
PLAY RECAP *********************************************************************
container1 : ok=5 changed=1 unreachable=0 failed=0
container2 : ok=5 changed=1 unreachable=0 failed=0
localhost : ok=4 changed=1 unreachable=0 failed=0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment