Ansible - Basic
Installation&setup
- Redhat/Centos need to Install EPEL, fedora has it in its default source
- Generate ssh, copy ssh to remote, save remote hosts to known_hosts
Configuration
default configuration file: /etc/ansible/ansible.cfg
basic options:
inventory
: path to host list filelibrary
: path to extra moduleremote_tmp
: remote host temp files pathlocal_tmp
: management node temp fiels path
configuration file priority
Ansible will try to find configuration file in below order:
- ANSIBLE_CONFIG (env var)
- ansible.cfg (current directory)
- .ansible.cfg (home directory)
- /etc/ansible/ansible.cfg
Host Inventory
- default host file:
/etc/ansible/hosts
- use
-i
or--inventory-file
to load separate host list ansible-playbook -i hosts site.yml
- use
- We can use [group_name] to setup group, but it’s not required
- we can also nested the groups
- we can also assign connection param in inventory file:
- create vars for a group
CLI tools
ansible <host> [options]
ansibel <host> -m <modules>
Module
Ansible Module: module
in ansible is cmd
in bash
- Use module in cli
-m <module_name>
-a <module_param>
- Use module in playbook
module_name: module_option=option_value
- type of module
- Core module: doesn’t need to download and install, core module will be well tested
- extra module: need to download and install, maybe bug
- download: git clone xxxxx
- change configuration file
/etc/ansible/ansible.cfg
, addlibrary = /home/$pathToExtraModule
, or change ansible.cfg in current directory or change ANSIBLE_LIBRARY
- check modules
ansible-doc module_name
- common modules
- ping
- not only check connectivity, also check ssh availiability and python version
- don’t need any param
- debug
- similer to
echo
, it can print some debugging msg msg: <some info>
var: <some variables>
- similer to
- copy
- copy files from the mgt host to remote host
mode
to set permissionbackup
can backup files before copyvalidate
can set some script to validate
- template
- copy files, but there’re some dynamic variables to change in the files
- use
{{ var_name }}
in files to access remote host env variables and vars in playbook’s var section. - also support permission and user/group/validate…
- file
- configure files, symlinks and folders' permission, create and delete them.
mode
to configure permissionsrc
anddest
to create symlinksstate: touch
to create filestate: directory
to create folder
- user
- create user and attribute
groups
to add user to groupstate: absent
to delete user- change user attribute like ssh key, and expires
- yum
- use
state:latest
to install the latest package - use
state:absent
to delete the package - use
state:present
andname:<pkgname_with_version>
to install a certain version - use
enablerepo: <some_repo>
to install package from a certain repo - use
name: @Development
tools to install a group of packages - use
name: <path_to_rpm_file>
to install a local package - use
name: <url_to_rpm>
to install a package from URL
- use
- service
state: started
to start servvcestate: stopped
to stop servicestate: restarted
to restart servicestate: reloaded
to reload serviceenable: yes
to start it when the host onlineargs: eth0
innetwork
service to enable network’s interface
- firewalld
- add rules for service
- add rules for port
1
port: 8081/tcp
- other rules
rich_rule, source, zone, interface, masquerade
- shell: issue cmds in remote host
- support operators like
HOME, < > | ; & && >> ||
- call script
- shell: script.sh >> log.txt
- use
args:
tochdir
change working directorycreates
excute only when file non-existexecutable
use certain bash
- support operators like
- command: similar to shell, but doesn’t support operator
- ping