Skip to content

Instantly share code, notes, and snippets.

@mttjohnson
Last active April 11, 2019 04:16
Show Gist options
  • Save mttjohnson/9cfad2468e25a5b2d9f152d90e2e1d69 to your computer and use it in GitHub Desktop.
Save mttjohnson/9cfad2468e25a5b2d9f152d90e2e1d69 to your computer and use it in GitHub Desktop.
Ansible facts to from and updating json files
# Display all ansible facts from remote machines
ansible -i inventories/example all -m setup
# Write value to json file
TEMP_PLAYBOOK_FILE="TEMP_PLAY.yml"
PLAYBOOK_CONTENTS=$(cat <<'CONTENTS_HEREDOC'
---
- hosts: all
become: true
vars:
vars_file: {}
tasks:
- set_fact:
new_host: "{{ '{\"' + inventory_hostname + '\":{\"internal_ip\":\"' + hostvars[inventory_hostname]['ansible_default_ipv4']['address'] + '\"}}' }}"
- set_fact:
vars_file: "{{ vars_file | combine( new_host, recursive=True ) }}"
- debug:
var: vars_file
- set_fact:
new_host: "{{ '{\"localhost\":{\"internal_ip\":\"127.0.0.1\"}}' }}"
- set_fact:
vars_file: "{{ vars_file | combine( new_host, recursive=True ) }}"
- debug:
var: vars_file
- local_action: copy content={{ vars_file | to_nice_json }} dest=json/host-vars.json
become: false
CONTENTS_HEREDOC
)
echo "${PLAYBOOK_CONTENTS}" > ${TEMP_PLAYBOOK_FILE}
ansible-playbook -i inventories/example ${TEMP_PLAYBOOK_FILE} --diff
rm ${TEMP_PLAYBOOK_FILE}
# Read value from json file
TEMP_PLAYBOOK_FILE="TEMP_PLAY.yml"
PLAYBOOK_CONTENTS=$(cat <<'CONTENTS_HEREDOC'
---
- hosts: all
become: true
vars:
vars_file: "{{ lookup('file', 'json/host-vars.json', errors='warn') }}"
tasks:
- debug:
msg: "{{ vars_file }}"
- set_fact:
vars_file_json: "{{ vars_file }}"
- debug:
var: vars_file_json
- debug:
msg: "{{ vars_file_json | to_json | from_json }}"
# For json_query, reference: http://jmespath.org/tutorial.html
- set_fact:
# Putting variables into the json query ends up involving escpaing quotes
vars_json_query: "{{ '\"' + inventory_hostname + '\".\"internal_ip\"' }}"
- debug:
msg: "{{ vars_json_query }}"
- name: Resulting parsed Internal IP
debug:
msg: "{{ vars_file_json | to_json | from_json | json_query( vars_json_query ) }}"
CONTENTS_HEREDOC
)
echo "${PLAYBOOK_CONTENTS}" > ${TEMP_PLAYBOOK_FILE}
ansible-playbook -i inventories/example ${TEMP_PLAYBOOK_FILE} --diff
rm ${TEMP_PLAYBOOK_FILE}
# Read value from json file, add to value, and write json to file
TEMP_PLAYBOOK_FILE="TEMP_PLAY.yml"
PLAYBOOK_CONTENTS=$(cat <<'CONTENTS_HEREDOC'
---
- hosts: all
become: true
vars:
vars_file: "{{ lookup('file', 'json/host-vars.json', errors='warn') }}"
tasks:
- name: debug vars_file
debug:
var: vars_file
- set_fact:
vars_file_obj: "{{ (vars_file | length > 0) | ternary(vars_file, {}) }}"
- name: debug vars_file_obj
debug:
var: vars_file_obj
- set_fact:
combined_vars_file: "{{ vars_file_obj }}"
- set_fact:
new_host: "{{ '{\"newserver\":{\"internal_ip\":\"11.22.33.44\"}}' }}"
- set_fact:
combined_vars_file: "{{ combined_vars_file | combine( new_host, recursive=True ) }}"
- set_fact:
new_host: "{{ '{\"' + inventory_hostname + '\":{\"internal_ip\":\"' + hostvars[inventory_hostname]['ansible_default_ipv4']['address'] + '\"}}' }}"
- set_fact:
combined_vars_file: "{{ combined_vars_file | combine( new_host, recursive=True ) }}"
- name: debug combined_vars_file
debug:
var: combined_vars_file
- local_action: copy content={{ combined_vars_file | to_nice_json }} dest=json/host-vars.json
become: false
CONTENTS_HEREDOC
)
echo "${PLAYBOOK_CONTENTS}" > ${TEMP_PLAYBOOK_FILE}
ansible-playbook -i inventories/example ${TEMP_PLAYBOOK_FILE} --diff
rm ${TEMP_PLAYBOOK_FILE}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment