Showing posts with label ansible. Show all posts
Showing posts with label ansible. Show all posts

Wednesday, December 26, 2018

Tech Notes:- log file at /var/log/ansible.log is not writeable and we cannot create it, aborting

This is one of the errors that we encounter after installing ansible and trying to run our first playbook as a non-root user. To fix this all that we need to do is to create a group called ansible , add the user who is executing the playbook to the ansible group, then touch the file /var/log/asnible.log with root as the owner and ansible as the group.Permission should be 775 which is read write and execute for  owner and group and read and execute for others.

# sudo touch /var/log/ansible.log

#sudo groupadd ansible

# sudo chown root:ansible /var/log/ansible.log

# sudo chmod 775 /var/log/ansible.log

# sudo usermod sudeep -aG ansible

You need to signout and sign in before executing the playbook.


Sunday, November 25, 2018

Tech Notes :- Ansible Ad hoc Command Examples



Ansible ad-hoc commands are useful when you want to run something quickly on multiple machines. Here I am providing some examples of different situations that might help. Let's assume that our inventory name is 'servers'  and  we the names of all servers in this file



Checking the status of all servers in our inventory file 'servers'
# ansible  'servers' -m ping

For checking the uptime of all servers
# ansible 'servers'  -a 'uptime'

Create a user sudeep and set the password on all servers in the inventory file servers
# ansible 'servers' -m user -a "name=sudeep password=redhat"

Updating  Linux server with latest updates
# ansible 'servers' -m package -a 'name=* state=latest'

Installing a specific package in all servers in the inventory
# ansible 'servers'  -m package -a "name=httpd state=present"

Reboot the servers and wait for it to come back
# ansible 'servers' -b -B 1 -P 0 -m shell -a "sleep 5 && reboot"

Remove a text from a file
# ansible 'servers'  -m lineinfile -a "dest=/etc/somefile.txt regexp='texttoberemoved' state=absent"

Checking entries in a file
# ansible 'servers' -m command -a 'grep listen  /etc/nginx/conf.d/nginx.conf'

Replacing strings in a file on multiple servers
# ansible 'servers' -m replace -a 'path=/etc/nginx.conf.d/nginx.conf regexp="192.168.1.20" replace=192.168.1.100'

Commenting a line in a file based on finding a pattern in that line.For example comment the line containing the word backup in a cron job
ansible 'servers' -m replace -a 'path=/etc/cron.d/backup_schedule regexp="(.*backup.*)" replace="#\1"'

Check the hypervisor on remote machines
# ansible 'servers' -m setup -a "filter=ansible_virtualization_*"

List remote files
# ansible 'servers' -m command -a 'ls -l  /var/www/html'

Copy files
# ansible 'servers' -m copy -a 'src=/home/sudeep/test.php dest=/var/www/html/test.php owner=sudeep group=sudeep'

List all variables a partiucular host can see
# ansible -m debug -a 'var=hostvars[inventory_hostname]' foo.com

Schedule a reboot
# ansible 'foo.com  -a  '/sbin/shutdown -r +5'

Checking the remote git version on multiple servers
# ansible 'servers' -m command -a "git status  args: chdir=/var/www/html/sudeep"

List all tasks in a playbook
# ansible-playbook --list-tasks playbooks/app-server.yml

Get an excerpt from a file
# ansible 'servers' -a 'grep -A 2 port /etc/supervisord.conf'

Remove a port from firewall where the firewall is configured using firewalld
# ansible 'server' -m command -a "firewall-cmd --remove-port='8080/tcp' --zone=internal --permanent "

Reload firewalld
#ansible 'server' -m command -a  'systemctl reload firewalld'

List all opened ports in public zone
#ansible 'server' -m command -a 'firewall-cmd --list-ports  --zone=public'

List all services in public zone
#ansible 'server' -m command -a 'firewall-cmd --list-services  --zone=public'

List all sources in public zone
#ansible 'server' -m command -a 'firewall-cmd --list-sources  --zone=public'

Adding a server to proxysql with a default_hostgroup of 80, writing the changes to disk and loading it to runtime.
ansible 'myserver.example.com' -m proxysql_backend_servers -a 'hostgroup_id=80 hostname=172.16.1.55 load_to_runtime=True login_host=proxysql.example.com login_password=Password123 login_port=6032 login_user=proxysql-admin save_to_disk=True state=present status=ONLINE comment="My new mysql database server"'


We can send the output of the ansible command(or any command) both to a file as well as stdout.See the example below
ansible 'servers' -m command -a "git status  args: chdir=/var/www/html/sudeep" 2>&1 | tee ~/outfile

Related Posts Plugin for WordPress, Blogger...