Ansible - mwicat/personal GitHub Wiki
https://ansible-docs.readthedocs.io/zh/stable-2.0/rst/playbooks_best_practices.html https://docs.ansible.com/ansible/latest/modules/list_of_all_modules.html
Special magic variables
https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html
Limit to group of hosts
ansible-playbook site.yml --limit webservers
or
ansible-playbook webservers.yml
Install collection
ansible-galaxy collection install mysql.mysql_user
List hosts in inventory
ansible -i inventories/test/ all --list-hosts
Use test inventory
ansible-playbook -i inventories/test site.yml --tags ntp --limit webservers
Dry-run
ansible-playbook -i inventories/production site.yml --limit host1,host2 --check
Debug
ansible-playbook --check -i inventories/production site.yml -vvvv
Limit to hosts
ansible-playbook -i inventories/production site.yml --limit host1,host2
List hosts
ansible-inventory --list
Run command on host
ansible -i inventories/test myhost -a ls
ansible localhost -a 'uname'
Inline
ansible localhost --become -m apt -a 'pkg=htop state=present'
Playbook
---
- hosts: localhost
tasks:
- name: Install htop
apt: pkg=htop state=present
ansible-playbook --become playbook.yml --check
ansible-playbook --become playbook.yml
Debug playbook
- hosts: all
tasks:
- debug:
msg: task1
Run specific task
ansible-playbook --become playbook.yml --step --start-at-task=taskname
Galaxy install role
ansible-galaxy install geerlingguy.apache,v1.0.0
ansible-galaxy list
Copy file
- name: Copy mysql release package
copy:
src: mysql-apt-config_0.8.13-1_all.deb
dest: /root/mysql-apt-config.deb
Install packages
- name: ensure packages are installed
package:
name: "{{ item }}"
with_items:
- mysql-server
Copy directory
- name: Copy conf.d
copy:
src: '{{ item }}'
dest: /etc/apache2/conf.d
owner: root
group: root
mode: u=rwX,g=r,o=r
tags:
config
with_fileglob:
- conf.d/*
Add line to file
- lineinfile:
path: /etc/hosts
regexp: '^192\.168\.0\.3'
line: '192.168.0.3 myhost'
owner: root
group: root
mode: 0644
when: project_environment == "development"
Create directory tree
- name: Create /dir tree
file:
path: "{{ item }}"
state: directory
with_items:
- /dir
- /dir/dir1
- /dir/dir2
User role with vars
---
- hosts: all
roles:
- percona-server-tools
vars:
percona_server_tools_reset_root_password:
run: true
root_password: 'rootpass'
Install ansible apt integration
ansible -i inventories/test/ yourhost -m apt