vars demo completed

This commit is contained in:
Ilgar_Naghiyev
2020-03-05 14:07:37 +01:00
parent 561e8a159a
commit d90120d49f
2 changed files with 42 additions and 2 deletions

View File

@@ -998,7 +998,7 @@ Overview:
- Roles, blocks, and inventories
- Essential variable use:
- -debug: msg="Look! I'm using my variable {{myVar}}!"
- A not on quotes:
- A note on quotes:
- name: "{{package}}"
- Dictionary variables
@@ -1009,4 +1009,31 @@ Overview:
![img](https://github.com/Bes0n/EX407-Ansible-Automation/blob/master/images/img20.png)
### Demo: Ansible Variables - Magic Variables and Jinja Filters
### Demo: Ansible Variables - Magic Variables and Jinja Filters
Cookbook with variables usage:
```
---
- hosts: labservers
vars:
inv_file: /home/cloud_user/vars/inv.txt
tasks:
- name: create file
file:
path: "{{inv_file}}"
state: touch
- name: generate inventory
lineinfile:
path: "{{inv_file}}"
line: "{{ groups['labservers']|join(' ') }}"
```
- `inv_file` - defined variable before task started
- `groups['labservers']` - magic variable with `labservers` host group in default inventory
- `|join(' ')` - join our hostnames with space between them.
Content of the file after playbook run:
```
[cloud_user@innaghiyev2c playbook]$ cat ../vars/inv.txt
innaghiyev1c.mylabserver.com innaghiyev2c.mylabserver.com innaghiyev3c.mylabserver.com
```

View File

@@ -0,0 +1,13 @@
---
- hosts: labservers
vars:
inv_file: /home/cloud_user/vars/inv.txt
tasks:
- name: create file
file:
path: "{{inv_file}}"
state: touch
- name: generate inventory
lineinfile:
path: "{{inv_file}}"
line: "{{ groups['labservers']|join(' ') }}"