Идемпотентность — повторяемость, результат команды должен быть одинаковый при каждом запуске
Каждый модуль имеет поле name — используется для описания что делает модуль
Формат описания такой module: options
tasks: - name: make sure apache is running service: name: httpd state: started
Так можно выполнить команду, успешное завершение которой не 0
tasks:
- name: run this command and ignore the result
shell: /usr/bin/somecommand || /bin/true
Or this:
tasks:
- name: run this command and ignore the result
shell: /usr/bin/somecommand
ignore_errors: True
Если команда длинная, то можно ее перенести и добавить пробелы
tasks: - name: Copy ansible inventory file to client copy: src=/etc/ansible/hosts dest=/etc/ansible/hosts owner=root group=root mode=0644
Можно использовать переменные, которые указаны в секции vars, называется переменная vhost
tasks: - name: create a virtual host file for {{ vhost }} template: src: somefile.j2 dest: /etc/httpd/conf.d/{{ vhost }}
Сокращенные действия:
template: src: templates/foo.j2 dest: /etc/foo.conf
Так можно сделать перезапуск сервиса, только если файл изменился
- name: template configuration file template: src: template.j2 dest: /etc/foo.conf notify: - restart memcached - restart apache
Часть задачи, которая прослушивает notify
секцию называется hendlers
Hendlers — ни чем не отличается от основных задач, и если нет notify не будет запущена, в не зависимости от того сколько notify она будет запущена только 1 раз, в самом конце
handlers: - name: restart memcached service: name: memcached state: restarted - name: restart apache service: name: apache state: restarted
handlers: - name: restart memcached service: name: memcached state: restarted listen: "restart web services" - name: restart apache service: name: apache state:restarted listen: "restart web services" tasks: - name: restart everything command: echo "this task will restart the web services" notify: "restart web services"
Чтобы сбросить handler
tasks: - shell: some tasks go here - meta: flush_handlers - shell: some other tasks
handlers notified within pre_tasks, tasks, and post_tasks sections are automatically flushed in the end of section where they were notified;
handlers notified within roles section are automatically flushed in the end of tasks section, but before any tasks handlers.
обработчики, уведомленные в разделах pre_tasks, tasks и post_tasks, автоматически очищаются в конце раздела, где они были уведомлены;
обработчики, уведомленные в разделе ролей, автоматически очищаются в конце раздела задач, но перед обработчиками задач.
ansible-playbook
--syntax-check так можно проверить синтаксис playbook
--verbose
— расширенный вывод
Так можно посмотреть какие хосты будут затронуты конкретным плейбуком, не выполняя его
ansible-playbook playbook.yml --list-hosts
0 Comment