2 min read

Ansible

Ансибл это система управления конфигурациями, сама конфигурация описывается в плейбуке с использованием yaml. Собрал небольшую шпаргалку по основным модулям ансибла.


Модули apt/yum
Ансибл умеет работать с большинством пакетных менеджеров, достаточно просто указать имя модуля и состояние пакета

  - name: Install mc
    apt:
      name: mc
      state: present
    
   - name: Remove mc
     dnf:
       name: mc
       state: absent

установка mc через apt и удаление чезез yum

Модуль copy

  - name: copy a file from local machine to local machine
    copy:
      src: files/src.txt
      dest: files/dest.txt

  - name: copy a file from remote machine to remote machine
    copy:
      src: /etc/src.txt
      dest: /etc/dest.txt

  - name: copy a file from local machine to remote machine with owner and permissions
    copy:
      src: files/src.txt
      dest: /etc/dest.txt
      owner: foo
      group: foo
      mode: '0644'

Модуль fetch

- name: fetch module
  hosts: all
  tasks:
  - name: copy a file from remote machine to local machine
    fetch:
      src: /var/log/access.log
      dest: /var/log/fetched

  - name: copy a file from local machine to remote machine with owner and permissions
    copy:
      src: files/src.txt
      dest: /etc/dest.txt
      owner: foo
      group: foo
      mode: '0644'

Модуль file

  - name: Create a file
    file:
      path: /etc/foo.conf
      state: touch
      mode: u=rw,g=r,o=r
      
  - name: Create a directory if it does not exist
    file:
      path: /etc/some_directory
      state: directory
      mode: '0755'
      
  - name: Remove file (delete file)
    file:
      path: /etc/foo.txt
      state: absent
      
  - name: Change file ownership, group and permissions
    file:
      path: /etc/foo.conf
      owner: foo
      group: foo
      mode: '0644'
      
  - name: Create a symbolic link
    file:
      src: /file/to/link/to
      dest: /path/to/symlink
      owner: foo
      group: foo
      state: link

Модуль acl

  - name: Grant user Joe read access to a file
    acl:
      path: /etc/foo.conf
      entity: joe
      etype: user
      permissions: r
      state: present

  - name: Removes the acl for Joe on a specific file
    acl:
      path: /etc/foo.conf
      entity: joe
      etype: user
      state: absent

  - name: Sets default acl for joe on foo.d
    acl:
      path: /etc/foo.d
      entity: joe
      etype: user
      permissions: rw
      default: yes
      state: present

Модуль template
Модуль template представляет собой файл, который содержит все параметры конфигурации пользователя, но динамические значения задаются как переменные. Однако если указать для gather_facts значение no то переменных не будет и файл скопируется как есть

  - name: Template a file to /etc/file.conf
    template:
      src: /mytemplates/foo.j2
      dest: /etc/file.conf
      owner: bin
      group: wheel
      mode: '0644'

Модуль lineinfile

  - name: adding a line
    lineinfile:
      path: /etc/selinux/config
      regexp: '^SELINUX='
      line: SELINUX=enforcing

  - name: deleting a line
    lineinfile:
      path: /etc/sudoers
      state: absent
      regexp: '^%wheel'

  - name: Replacing a line
    lineinfile:
      path: /etc/hosts
      regexp: '^127\.0\.0\.1'
      line: 127.0.0.1 localhost
  - name: replace a line only after a specified string 
    lineinfile:
      path: /etc/httpd/conf/httpd.conf
      regexp: '^Listen '
      insertafter: '^#Listen '
      line: Listen 8080

Модуль shell
выполняет указанную команду на удаленном сервере

- name: exec date
  shell: date