- vagrant ssh acs 1.mkdir exercise1; cd exercise1
- vi inventory
ansible -i -m -u -k -v (-vv debug level2/ -vvv debug level3)
-
192.168.33.20 , 192.168.33.33
-
ping the ip. eg ansible 192.168.33.20 -i inventory -u vagrant -m ping -k ansible all -i inventory -u vagrant -m ping -k ansible all -i inventory -u vagrant -m ping -k -vvv
ansible all -i inventory -u vagrant -m command -a "/sbin/reboot" ansible all -i inventory -u vagrant -m command -a "/usr/sbin/yum update -y" or ansible all -i inventory -u vagrant -m -a "/usr/sbin/yum update -y"
-
Module has 2 types : command(run a python executable) and shell (can use shell variable)
- Inventory Features - Behavioral parameters, Groups, Groups of Groups, Assign Variables, Scaling out using multiple files, Static/Dynamic Inventory File
web1 ansible_ssh_host=192.168.33.20 ansible_ssh_user=vagrant ansible_ssh_pass=vagrant
run ansible web1 -i inventory -m ping
web1 ansible_ssh_host=192.168.33.20 ansible_ssh_user=vagrant ansible_ssh_pass=vagrant db1 ansible_ssh_host=192.168.33.30 ansible_ssh_user=vagrant ansible_ssh_pass=vagrant
[webservers] web1 db1
web1 ansible_ssh_host=192.168.33.20 ansible_ssh_user=vagrant ansible_ssh_pass=vagrant db1 ansible_ssh_host=192.168.33.30 ansible_ssh_user=vagrant ansible_ssh_pass=vagrant
[webservers] web1
[dbservers] db1
[datacenter:children] webservers dbservers
run command ansible datacenter -i inventory -m ping
web1 ansible_ssh_host=192.168.33.20 db1 ansible_ssh_host=192.168.33.30
[webservers] web1
[dbservers] db1
[datacenter:children] webservers dbservers
[datacenter:vars] ansible_ssh_user=vagrant ansible_ssh_pass=vagrant
run command ansible datacenter -i inventory -m ping
Using directories to manage
|__production | | | |--------group_vars | | \all | | \db | |--------host_vars | | \web1 | |--------inventory_prod |__test | | | |--------group_vars | | \all | | \db | |--------host_vars | | \web1 | |--------inventory+test
Order of Operations (Precedence)
- Group_Vars - All
- Group_Vars - GroupName
- Host_Vars Hostname (The highest precedence)
--The most specific version of that variable is actually going to take the highest level of precedence.
-- variable files are written in YAML file
Eg of variable file
ntp: ntp-west.company.com syslog: logger-west-company.com
Create the group variable username in the group_vars file
run command ansible webservers -i inventory_prod -m user -a "name={{username}} password=12345" --sudo
- touch ansible.cfg
- [defaults] host_key_checking=False
- ansible web1 -i inventory_prod -m ping
- export ANSIBLE_HOST_KEY_CHECKING=True
- ansible web1 -i inventory_prod -m ping
There are 3 kinds of modules : Core, Extras and Deprecated
- To list modile run ansible-doc -l
- To list modile run ansible-doc -s
- To list modile run ansible-doc
Copies a file from local box to remote system Has "backup" capability Can do validation remotely
Pulls a file from remote host to local system Can use md5 checksums to validate
Manages installed applications on Debian-based systems Can install, update or delete packages Can update entire system
Manages installed applications on Redhat-based systems Can install, update or delete packages Can update entire system
Can stop, start and restart services Can enable services to start on boot
- Browse module documentation
- Install Web Server(Yum module)
- Start Web Server(Service module)
- Install db server(Yum module)
- Start db server(Service module)
- Stop firewalls (Service module)
run command ansible webservers -i inventory -m yum -a "name=httpd state=present" --sudo
run command ansible webservers -i inventory -m service -a "name=httpd enabled=yes state=started" --sudo
run command ansible dbservers -i inventory -m yum -a "name=mysql-server state=present" --sudo
check the db state. run command service mysqld status
ansible dbservers -i inventory -m service -a "name=mysqld state=started" --sudo
- Browse http://192.168.33.20/
- run command to stop firewall ansible webservers:dbservers -i inventory -m service -a "name=iptables state=stopped" --sudo
- OR (group1:group2)_
- NOT (!group2)
- Wildcard (web*.ex.com)
- Regex (~web[0-9]+)
- Complex Patterns AND (group1:&group2). Specific for intersection
- Gather facts on remote systems
- Used in Playbooks
- run command ansible web1 -i inventory -m setup
- run command ansible web1 -i inventory -m setup -a "filter=ansible_eth*"
- run command ansible web1 -i inventory -m setup -a "filter=ansible_mounts"
- run command ansible all -i inventory -m setup --tree ./setup
Plays map hosts to tasks A play can have multiple tasks A playbook can have multiple plays
- run command to execute playbook ansible-playbook playbook.yml
refer to example.yml
- Write a playbook
- Add play to install web server
- Add play to install db server
- Add play to start services
- Fail a play
- Retry a failed play
- Create ansible.cfg. Refer to PlaybookDemo/ansible.cfg
- run command ansible-playbook web_db.yml
- in the case of failure can just rerun the fail case eg: run command ansible-playbook web_db.yaml --limit @/home/vagrant/web_db.yaml.retry
- Templates
- Creates and copies dynamic files
- Uses Jinja2 Engine