# tasks file for testing
- name: check if the container is running
shell: docker ps | grep "my_mysql"
failed_when: "result.rc == 2"
- name: check value
shell: echo "Container is not Running"
when: result.stdout == ""
The ansible playbook above is what I normally do when it comes to debugging or running the playbook based on some condition match.
Lets write a playbook to check if the container is running or not and we will run the job only when it is running.
- In line number 5 we are simply running bash script to check if the container with that name is running or not.
- In line number 6 we register the variable. This is how we store the result of the command in ansible.
- The purpose of line number 7 8 is to ignore the error. Suppose if such container is not found in line number 5. Though we expect it to register blank value in our variable. It does not do so and terminate so we are forcing it to ignore the error. On success it returns 0 and 1 on error. So we are making it compare with 2 that will not ever happen and making the playbook not fail.
- Use debug to check what exactly is there in our registered value. Normally it will showcase everything, here we just want to check output so specifying its key called stdout.
- Use when to work as a conditional statement in ansible. It is like if condition in any programming language. We want to run the shell command above it only when the value registered output is empty.
After installing the ansible. Normally these was the two basic setup that made me stuck my ansible playbook. Here, I have tried to sum up, what are the issues that appears and how can we resolve it.
The ansible stuck in gathering facts:
when we first try to login to a instance for the first time we get a message like
Are you sure you want to continue connecting (yes/no)?
One way to get rid of it is manually login to the instance and go through this process manually. But we are here for automations. So to get rid of it, we simply need to make one setting in ansible config files.
Ansible config file is available in this location /etc/ansible/ansible.cfg Here just need to uncomment this default setting, that is commented by default.
# uncomment this to disable SSH key host checking
host_key_checking = False
Ansible playbook can ssh
There might be multiple reason for it but lets see what else it can be?
- Not added ssh public key:
Make sure you have added the ansible public key to the authorized_keys in the instance. With this we make the ansible server authorized to the instance. We can even automate this process of adding the public key. But I would not like to cover it here. If you are interested you will find a roles built for the purpose.
- If we are not able to ssh even after clearing the above issue. The problem can be we have not set the instance ip in the right location. If you have created the inventory.ini and you are trying to run playbook to the instance set in inventory.ini but not able to. We need to change the default ansible inventory location that is set to /etc/ansible/hosts
Either change the host location in ansible.cfg or set the ip address to the default host location.
#inventory = /etc/ansible/hosts
Change this to your newly created inventory.ini.
These are few of the issues that I used to face during my initial setups.