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
-ior--inventory-fileto 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
modeto set permissionbackupcan backup files before copyvalidatecan 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.
modeto configure permissionsrcanddestto create symlinksstate: touchto create filestate: directoryto create folder
- user
- create user and attribute
groupsto add user to groupstate: absentto delete user- change user attribute like ssh key, and expires
- yum
- use
state:latestto install the latest package - use
state:absentto delete the package - use
state:presentandname:<pkgname_with_version>to install a certain version - use
enablerepo: <some_repo>to install package from a certain repo - use
name: @Developmenttools 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: startedto start servvcestate: stoppedto stop servicestate: restartedto restart servicestate: reloadedto reload serviceenable: yesto start it when the host onlineargs: eth0innetworkservice to enable network’s interface
- firewalld
- add rules for service
- add rules for port
1port: 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:tochdirchange working directorycreatesexcute only when file non-existexecutableuse certain bash
- support operators like
- command: similar to shell, but doesn’t support operator
- ping