Last active
October 23, 2016 10:35
-
-
Save yaegashi/bf67bd6fac3db8f798a4 to your computer and use it in GitHub Desktop.
Exploiting Jinja2 in Ansible - http://blog.keshi.org/hogememo/2015/12/07/exploiting-ansible-jinja2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ansible-playbook -i localhost, basicexp-playbook.yml | |
PLAY [localhost] ************************************************************** | |
TASK: [OK: ['a', 'b', 'c']] *************************************************** | |
ok: [localhost] => { | |
"msg": [ | |
"a", | |
"b", | |
"c" | |
] | |
} | |
TASK: [OK: 'a,b,c'] *********************************************************** | |
ok: [localhost] => { | |
"msg": "a,b,c" | |
} | |
TASK: [OK: ['a', 'b']] ******************************************************** | |
ok: [localhost] => { | |
"msg": [ | |
"a", | |
"b" | |
] | |
} | |
TASK: [OK: a,b,c] ************************************************************* | |
ok: [localhost] => { | |
"msg": "a,b,c" | |
} | |
TASK: [NG: Sequence assignment] *********************************************** | |
skipping: [localhost] | |
TASK: [NG: Built-in functions like len(), map()] ****************************** | |
skipping: [localhost] | |
TASK: [NG: Expression only in {% %} statement] ******************************** | |
skipping: [localhost] | |
TASK: [NG: Expression-statement expansion] ************************************ | |
skipping: [localhost] | |
TASK: [OK: ['a', 'b', 'c', 1]] ************************************************ | |
ok: [localhost] => { | |
"msg": [ | |
"a", | |
"b", | |
"c", | |
1 | |
] | |
} | |
TASK: [OK: ['a', 'b', 'c', 1, 2, 3]] ****************************************** | |
ok: [localhost] => { | |
"msg": [ | |
"a", | |
"b", | |
"c", | |
1, | |
2, | |
3 | |
] | |
} | |
TASK: [OK: ['a', 'b', 'c', 1]] ************************************************ | |
ok: [localhost] => { | |
"msg": [ | |
1 | |
] | |
} | |
PLAY RECAP ******************************************************************** | |
localhost : ok=7 changed=0 unreachable=0 failed=0 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- hosts: localhost | |
gather_facts: no | |
vars: | |
strabc: a,b,c | |
listabc: [a, b, c] | |
tasks: | |
- name: "OK: ['a', 'b', 'c']" | |
debug: | |
msg: | | |
{{ strabc.split(',') }} | |
- name: "OK: 'a,b,c'" | |
debug: | |
msg: | | |
{{ ','.join(listabc) }} | |
- name: "OK: ['a', 'b']" | |
debug: | |
msg: | | |
{{ listabc[0:2] }} | |
- name: "OK: a,b,c" | |
debug: | |
msg: | | |
{% set o = strabc %} | |
{{ o }} | |
- name: "NG: Sequence assignment" | |
debug: | |
msg: | | |
{% set listabc[0] = 1 %} | |
{{ listabc }} | |
when: false | |
- name: "NG: Built-in functions like len(), map()" | |
debug: | |
msg: | | |
{{ len(listabc) }} | |
when: false | |
- name: "NG: Expression only in {% %} statement" | |
debug: | |
msg: | | |
{% listabc.append(1) %} | |
{{ listabc }} | |
when: false | |
- name: "NG: Expression-statement expansion" | |
debug: | |
msg: | | |
{% do listabc.append(1) %} | |
{{ listabc }} | |
when: false | |
- name: "OK: ['a', 'b', 'c', 1]" | |
debug: | |
msg: | | |
{% if listabc.append(1) %}{% endif %} | |
{{ listabc }} | |
- name: "OK: ['a', 'b', 'c', 1, 2, 3]" | |
debug: | |
msg: | | |
{% set dummy = listabc.extend([1, 2, 3]) %} | |
{{ listabc }} | |
- name: "OK: ['a', 'b', 'c', 1]" | |
debug: | |
msg: | | |
{% set o = [1] %} | |
{% for i in listabc %} | |
{% set o = o + [ i + ':123' ] %} | |
{% endfor %} | |
{{ o }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ansible-playbook -i localhost, dictfilter-playbook.yml | |
PLAY [localhost] ************************************************************** | |
TASK: [debug ] **************************************************************** | |
ok: [localhost] => { | |
"var": { | |
"dict_filtered": { | |
"baz": [ | |
"D", | |
"E" | |
], | |
"foo": [ | |
"A", | |
"B", | |
"C" | |
] | |
} | |
} | |
} | |
PLAY RECAP ******************************************************************** | |
localhost : ok=1 changed=0 unreachable=0 failed=0 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- hosts: localhost | |
gather_facts: no | |
vars: | |
dict: { foo: [A, B, C], bar: [], baz: [D, E] } | |
dict_filtered: | | |
{% set o = {} %} | |
{% for k, v in dict.iteritems() %} | |
{% if v %} | |
{% set _ = o.update({k: v}) %} | |
{% endif %} | |
{% endfor %} | |
{{ o }} | |
tasks: | |
- debug: | |
var: dict_filtered |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
server1 | |
server2 | |
server3 | |
server4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- hosts: all | |
become: yes | |
become_user: sshuser | |
vars: | |
all_user_keys: | | |
{% set o = [] %} | |
{% for i in play_hosts %} | |
{% set _ = o.append(hostvars[i].pubkey) %} | |
{% endfor %} | |
{{ o }} | |
all_host_keys: | | |
{% set o = [] %} | |
{% for i in play_hosts %} | |
{% set v = hostvars[i] %} | |
{% set h = ','.join([i,v.ansible_fqdn]+v.ansible_all_ipv4_addresses) %} | |
{% set k = ' '.join([h, 'ssh-rsa', v.ansible_ssh_host_key_rsa_public]) %} | |
{% set _ = o.append({'name': i, 'key': k}) %} | |
{% endfor %} | |
{{ o }} | |
tasks: | |
- shell: ssh-keygen -q -f ~/.ssh/id_rsa -N '' | |
args: | |
creates: ~/.ssh/id_rsa | |
- shell: cat ~/.ssh/id_rsa.pub | |
register: reg | |
- set_fact: | |
pubkey: "{{reg.stdout}}" | |
- authorized_key: | |
user: "{{ansible_user_id}}" | |
key: "{{item}}" | |
with_items: all_user_keys | |
- known_hosts: | |
name: "{{item.name}}" | |
key: "{{item.key}}" | |
with_items: all_host_keys |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ansible-playbook -i localhost, listexp-playbook.yml | |
PLAY [localhost] ************************************************************** | |
TASK: [OK: ['a:A', 'b:B', 'c:C']] ********************************************* | |
ok: [localhost] => { | |
"msg": [ | |
"a:A", | |
"b:B", | |
"c:C" | |
] | |
} | |
TASK: [OK: ['a:1', 'c:3']] **************************************************** | |
ok: [localhost] => { | |
"msg": [ | |
"a:1", | |
"c:3" | |
] | |
} | |
TASK: [OK: 6] ***************************************************************** | |
ok: [localhost] => { | |
"msg": "6" | |
} | |
TASK: [NG: 0] ***************************************************************** | |
ok: [localhost] => { | |
"msg": "0" | |
} | |
PLAY RECAP ******************************************************************** | |
localhost : ok=4 changed=0 unreachable=0 failed=0 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- hosts: localhost | |
gather_facts: no | |
vars: | |
listabc: [a, b, c] | |
dictabc: { a: 1, b: 2, c: 3 } | |
list123: [1, 2, 3] | |
tasks: | |
- name: "OK: ['a:A', 'b:B', 'c:C']" | |
debug: | |
msg: | | |
{% set o = [] %} | |
{% for i in listabc %} | |
{% set _ = o.append(i + ':' + i | upper) %} | |
{% endfor %} | |
{{ o }} | |
- name: "OK: ['a:1', 'c:3']" | |
debug: | |
msg: | | |
{% set o = [] %} | |
{% for k, v in dictabc | dictsort %} | |
{% if v is odd %} | |
{% set _ = o.append(k + ':' + v | string) %} | |
{% endif %} | |
{% endfor %} | |
{{ o }} | |
- name: "OK: 6" | |
debug: | |
msg: | | |
{% set o = [0] %} | |
{% for i in list123 %} | |
{% set _ = o.append(o.pop() + i) %} | |
{% endfor %} | |
{{ o[0] }} | |
- name: "NG: 0" | |
debug: | |
msg: | | |
{% set o = 0 %} | |
{% for i in list123 %} | |
{% set o = o + i %} | |
{% endfor %} | |
{{ o }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
master1 ansible_ssh_host=10.0.45.11 | |
master2 ansible_ssh_host=10.0.45.12 | |
master3 ansible_ssh_host=10.0.45.13 | |
slave1 ansible_ssh_host=10.0.45.51 | |
slave2 ansible_ssh_host=10.0.45.52 | |
slave3 ansible_ssh_host=10.0.45.53 | |
[master_servers] | |
master1 | |
master2 | |
master3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ansible-playbook -i listjoin-hosts.txt listjoin-playbook.yml | |
PLAY [localhost] ************************************************************** | |
TASK: [debug ] **************************************************************** | |
ok: [localhost] => { | |
"var": { | |
"zkurl": "zk://10.0.45.11:2181,10.0.45.12:2181,10.0.45.13:2181/mesos" | |
} | |
} | |
PLAY RECAP ******************************************************************** | |
localhost : ok=1 changed=0 unreachable=0 failed=0 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- hosts: localhost | |
gather_facts: no | |
vars: | |
zkurl: | | |
{% set o = [] %} | |
{% for i in groups.master_servers %} | |
{% set _ = o.append(hostvars[i].ansible_ssh_host+':2181') %} | |
{% endfor %} | |
zk://{{ o | join(',') }}/mesos | |
tasks: | |
- debug: | |
var: zkurl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
run() { | |
echo "$ $*" | |
"$@" | |
} | |
run ansible-playbook -i localhost, basicexp-playbook.yml > basicexp-log.txt | |
run ansible-playbook -i localhost, listexp-playbook.yml > listexp-log.txt | |
run ansible-playbook -i listjoin-hosts.txt listjoin-playbook.yml > listjoin-log.txt | |
run ansible-playbook -i localhost, dictfilter-playbook.yml > dictfilter-log.txt | |
run ansible-playbook -i setjoin-hosts.txt setjoin-playbook.yml > setjoin-log.txt | |
#run ansible-playbook -i distpubkey-hosts.txt distpubkey-playbook.yml > distpubkey-log.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[tag_Name_Zookeeper_1] | |
a | |
b | |
[tag_Name_Zookeeper_2] | |
a | |
c | |
[tag_Name_Zookeeper_3] | |
b | |
d | |
[others] | |
localhost |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ansible-playbook -i setjoin-hosts.txt setjoin-playbook.yml | |
PLAY [localhost] ************************************************************** | |
TASK: [debug ] **************************************************************** | |
ok: [localhost] => { | |
"var": { | |
"Node": "a:2181,b:2181,c:2181,d:2181" | |
} | |
} | |
PLAY RECAP ******************************************************************** | |
localhost : ok=1 changed=0 unreachable=0 failed=0 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- hosts: localhost | |
gather_facts: no | |
vars: | |
Node: | | |
{% set o = {} %} | |
{% for g, h in groups.iteritems() %} | |
{% if g.startswith('tag_Name_Zookeeper') %} | |
{% for i in h %} | |
{% set _ = o.update({i+':2181': 1}) %} | |
{% endfor %} | |
{% endif %} | |
{% endfor %} | |
{{ o.keys() | sort | join(',') }} | |
tasks: | |
- debug: | |
var: Node |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment