Pautas de Ansible





Las variables se utilizan ampliamente en Ansible. Pero una de las cosas frustrantes de Ansible es que ofrece demasiada libertad. Esto tiene ventajas y desventajas. La desventaja es la complejidad junto con una alta responsabilidad y la ventaja es la flexibilidad. Recapitulemos y organicemos lo que sabemos sobre las variables de Ansible.





Las variables se pueden dividir en dos categorías:





  • ( "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). . , .





:





NAME





URL





import_playbook







https://docs.ansible.com/ansible/latest/modules/importplaybookmodule.html#import-playbook-module





import_role







https://docs.ansible.com/ansible/latest/modules/importrolemodule.html#import-role-module





include_role







https://docs.ansible.com/ansible/latest/modules/includerolemodule.html#include-role-module





import_tasks







https://docs.ansible.com/ansible/latest/modules/importtasksmodule.html#import-tasks-module





include_tasks







https://docs.ansible.com/ansible/latest/modules/includetasks_module.html#include-tasks-module





: (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.  





, . 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

ansible  





13.

, .





14. Ansible-

Ansible. , molecule. . , .





15.

Ansible? , git , . , . . — .





requirements.yaml:





---
- src: git@gitlab.company.com:mygroup/ansible.git
 scm: git
 version: "0.1"
...
      
      



:





  • src





  • scm





  • version





  • name






 «DevOps ».









- : «Prometheus: ». Prometheus , , .












All Articles