Running Ansible Task & Adhoc Commands
- Using “Ping” Module
- Login to your ansible host and try to run:
ansible -m ping all
- This will return failed status code since our ansible host is able to login into our host machine, for letting ansible host to successfully we will need to provide .pem file to ansible host and configure ansible.cfg to read that .pem file
- Copy your EC2 instance private key .pem file to ansible host in following directory /home/ec2-user/ansible
- Now modify your ansible.cfg as follows
# ansible.cfg [defaults] inventory = ./hosts-dev remote_user = <SSH_USERNAME> private_key_file = /path_to/<SSH_KEY>.pem host_key_checking = False
- Now try to run:
ansible -m ping all
- Login to your ansible host and try to run:
- Using “Shell” Module
- Lets try to which linux distro we are using:
ansible -m shell -a "uname"
- This will return distro name
- Lets try to which linux distro we are using:
- Using “Command” Module
- Lets try to run some false command in bin
ansible -m command -a "/bin/false" \!local
- This will return non-zero return code
- Lets try to run some false command in bin