Hello everybody and thank you in advance for your help.
I have to add a variable stat: ON for some hosts in my YAML inventory that looks like:
all:
hosts:
host_1: {db: true, bckp: false}
host_2: {db: false, bckp: true}
To looks like:
all:
hosts:
host_1: {db: true, bckp: false, stat: ON}
host_2: {db: false, bckp: true}
I want to add it with Ansible (lineinfile or other module) when I execute a task that power-on a VM.
I try this:
- name: Add variable for with status stopped
lineinfile:
path: inventory/inv.yml
insertafter: EOF
line: "host_1: {db: true, bckp: false, stat: ON}"
delegate_to: localhost
But it's not OK because I have to give only the hostname not other values that I can't know in advance.
I try also:
- name: Add variable for with status stopped
add_host:
name: host_1
groups: group_name
status: ON
delegate_to: localhost
Not good because it add a new line instead of modify the existing one.
I've tried this also:
- name: Read inventory file
slurp:
path: inventory/inv.yml
register: inventory_content
- name: Parse YAML inventory
set_fact:
parsed_inventory: "{{ inventory_content.content | b64decode | from_yaml }}"
- name: Add variable for with status stopped
set_fact:
parsed_inventory: |
{{ parsed_inventory | to_yaml | regex_replace('^(all:\s*hosts:\s*host_1:)', '\\1\n status: ON') }}
- name: Write updated inventory file
copy:
content: "{{ parsed_inventory }}"
It's not good because it change all my inventory and not add the value.
All what I tried didn't work as I expect, I want to:
- Find the line of given hostname (here
host_1) - Insert the new variable after existing variables like:
host_1: {db: true, bckp: false}->host_1: {db: true, bckp: false, stat: ON}
Thank you for your help.
According Organizing host and group variables, for the given inventory file
example.comjust introduce a
host_vars/one.example.comfile with content ofin order to drop a fact and find an example playbook
called via
resulting into an output of
There is absolute no need for parsing or editing a file, using
lineinfile,set_fact,regex_replaceor programming at all.Documentation
How to build your inventory
Things to know about programming using Ansible
Ansible Best Practices: The Essentials
What is the XY problem?
and finally