Variables are used extensively in Ansible. But one of the frustrating things about Ansible is that it offers too much freedom. This has both advantages and disadvantages. The disadvantage is complexity along with high responsibility, and the advantage is flexibility. Let's recap and organize what we know about Ansible variables.
Variables can be divided into two categories:
( "Filesystem").
( "Code").
, , .
Filesystem- Code-. ? .
1.
(Inventory
, group_vars
, host_vars
, role/defaults/main.yml
role/vars/main.yml
). "" . — , . "" , , . , , vars
, block
. :
- name: Variables scope
hosts: localhost
connection: local
vars:
MY_VAR: "I am global var"
tasks:
- block:
- name: Print variable inside the block.
debug:
var: MY_VAR
vars:
MY_VAR: "I am local var"
- name: Print variable outside the block.
debug:
var: MY_VAR
PLAY [Variables scope]
TASK [Gathering Facts]
ok: [localhost]
TASK [Print variable inside the block.]
ok: [localhost] => {
"MY_VAR": "I am local var"
}
TASK [Print variable outside the block.]
ok: [localhost] => {
"MY_VAR": "I am global var"
}
, . . defaults/main.yml
. , . , , .
2. README
, , , README. ansible-galaxy init , README. , , README , . . , git, wiki-. , , wiki-. .
3.
"" ( ) . . , . , , port? , , . : — consul. — url, — consul_url.
4.
Ansible . , . : , .
:
# No name/description
- copy: dest=/tmp/text.txt, content="bla-bla"
- name: Print variable global var.
debug:
var: MY_VAR
TASK [copy]
changed: [localhost]
TASK [Print variable global var.] *
ok: [localhost] => {
"MY_VAR": "I am global var"
}
5. DRY (Don't Repeat Yourself)
Ansible . , Ansible , DRY (Don't Repeat Yourself). . , .
:
: (include/import)tasks
, (include/import)role
. ? , uri API-. , POST-. 10 uri , - . .
: send_post.yml
- name: .::::::::::::. [ Sent POST request ] .::::::::::::.
uri:
url: "{{ URL }}"
method: POST
status_code: 200
body: "{{ BODY_VAR | to_nice_json }}"
body_format: json
validate_certs: yes
client_cert: tls.crt
client_key: tls.key
register: return_values
when: BODY_VAR is defined
.
- name: Bla-bla
include_tasks: send_post.yml
vars:
URL: "{{ main_url }}/{{ item }}"
BODY_VAR: "{{ item }}"
URL BODY_VAR — .
6. (block)
, . block try / catch .
- block: ... rescue: ...
block/rescue
— ignore_errors
. , . , , - , . , .
- block: - name: ..... - name: ..... - name: ..... always: file: path: /tmp/xxxx state: absent
7. command shell
command
shell
, . , . :
when
creates
( , ).
removes
( , ).
changedwhen
.
, command
shell
.
8.
. , . . , README. . . , always
, never
— .
skip_ansible_lint
— ansible-lint
.
9.
. become
no
, . become
. :
--- - hosts: wordpress become: no ... role: - role: wordpress tasks/main.yml --- - name: Install mysql-server pkg apt: name: mysql-server state: present become: yes
10. YAML-
YAML . :
YAML
- name: Install apache httpd apt: name: apache2 state: present
- name: Install apache httpd apt: pkg=apache2 state=pesent
11. gitignore
.gitignore
, git-. .gitignore
:
*.retry */__pycache__ *.pyc *.log ### IntelliJ IDEA ### .idea *.iws *.iml *.ipr
12. Ansible
13.
, .
14. Ansible-
Ansible. , molecule. . , .
15.
requirements.yaml:
---
- src: git@gitlab.company.com:mygroup/ansible.git
scm: git
version: "0.1"
...
:
src
scm
version
name
- : «Prometheus: ». Prometheus , , .