Ansible. Примеры конструкций

Ansible. Примеры конструкций

#Перезагрузка сервисов с применением переменных

- name: Restart Apache 
  service:
    name: "{{ services }}"
    state: started
    enabled: yes
  become: true
  vars:
    services:
    - httpd

#Поменять права на каталог

- name: "Modify html folder permissions"
  ansible.builtin.file:
    path: /var/www/html
    state: directory
    owner: name
    group: name

#Удалить каталог

- name: "Delete old folder"
  ansible.builtin.file:
    state: absent
    path: /var/www/html

#Копирование каталога

- name: "Copy dist folder"
  ansible.builtin.copy:
    src: files/html
    dest: /var/www/
    owner: name  
    group: name
    mode: 0644

#Include тасков (выбор в зависимости от типа ОС)

- name: Ubuntu tasks
  include_tasks: "ubuntu.yml"
  when: ansible_os_family == "Debian"

- name: Centos tasks
  include_tasks: "centos.yml"
  when: ansible_os_family == "RedHat"

#Установка пакета

- name: Task
  ansible.builtin.package:
    name: git
    state: present

#Замена regexp и блока в тексте

- name: Task
   ansible.builtin.lineinfile:
     path: "/etc/ssh/sshd_config"
     regexp: "PasswordAuthentication no"
     line: "PasswordAuthentication yes"

- name: "Update postgresql.conf"
      lineinfile:
        path: "config.ini"
        regexp: "{{ item.regexp }}"
        line: "{{ item.line }}"
      with_items:
        - regexp: "frequency = 15m"
          line: "frequency = 1h"
        - regexp: "frequency = 5m"
          line: "frequency = 2h"
- name: "Update pg_hba.conf"
      blockinfile:
        path: "./config.ini"
        insertafter: "### hosts are here ###"
        block: |
          host replication repluser 127.0.0.1/32 md5
          host replication repluser 192.168.0.1/32 md5
          host main 192.168.10.1/32 md5
          host postgres 192.168.11.0/24 md5

#Создание пользователя для авторизации

- name: Task
  ansible.builtin.user:
    name: devuser
    shell: /bin/bash
    groups: developers
    append: yes

#Клонирование репозитория от созданного выше пользователя

- name: task
  git: repo=https://github.com/some/repo.git version=master     dest=/var/www/dev
  become: yes 
  become_user: devuser


Jinja

Замена конфигурации:

- name: "replace default config"
  ansible.builtin.template:
    src: files/default.ubuntu.conf.j2
    dest: /etc/nginx/sites-available/default.conf
    owner: name
    group: name
    mode: 0644

Добавить комментарий