Ansible - Playbooks
ansible-playbook playbook.yml -f 10
–verbose | more details |
remote_user | the ssh user |
---
- name: Update web servers
hosts: webservers
remote_user: root
tasks:
- name: Ensure apache is at the latest version
ansible.builtin.yum:
name: httpd
state: latest
- name: Write the apache config file
ansible.builtin.template:
src: /srv/httpd.j2
dest: /etc/httpd.conf
- name: Update db servers
hosts: databases
remote_user: root
tasks:
- name: Ensure postgresql is at the latest version
ansible.builtin.yum:
name: postgresql
state: latest
- name: Ensure that postgresql is started
ansible.builtin.service:
name: postgresql
state: started
Pull down configs from git and run playbook:
ansible-pull
Verify playbooks:
--check
--diff
--list-hosts
--list-tasks
--syntax-check
ansible-lint verify-apache.yml
Using group_by for dynamic groups:
---
- name: talk to all hosts just so we can learn about them
hosts: all
tasks:
- name: Classify hosts depending on their OS distribution
group_by:
key: os_
# now just on the CentOS hosts…
- hosts: os_CentOS
gather_facts: False
tasks:
- # tasks that only happen on CentOS go in this play
Group vars:
---
# file: group_vars/all
asdf: 10
---
# file: group_vars/os_CentOS.yml
asdf: 42
Include vars:
- hosts: all
tasks:
- name: Set OS distribution dependent variables
include_vars: "os_.yml"
- debug:
var: asdf