Unable to locate ansible.cfg file - ansible

I'm trying to create a local config file in my ansible but when I run the list-hosts command its unable to locate it.

-i switch is for inventory, why do you feed config file into it?
Either use ansible -i dev or don't specify -i at all to use configured value from ansible.cfg.

Related

Ansible Run Command

Fairly new to Ansible.
I have a text file which contains a list of ec2 instance ip addresses. Secondly, I have a .yml file which applies tags to an array of ec2 ips.
Does anyone what run command I would use to pass the list of ips stored as a text file? I did it once a while back before. I forgot the run command, and cannot find it in my history.
I have a text file which contains a list of ec2 instance ip addresses: you probably mean an inventory file.
to include it in your ansible command execution, add -i <file name>
example:
ansible-playbook -i <inventory file> <yml file>
You could create an inventory with your IP's, for example, something like /tmp/my-ec2-vms:
[my-ec2-vms]
10.1.2.10
10.1.2.11
10.1.2.12
10.1.2.13
...
[my-ec2-vms:vars]
ansible_python_interpreter=/usr/local/bin/python
Then for testing, you could use the ping module, for example:
ansible -i /tmp/my-ec2-vms -m ping all
If that works later you could just run your playbooks:
ansible-playbook -i /tmp/my-ec2-vms my-playbook.yml

ansible command to list all known hosts

Ansible is already installed in a seperated ec2 instance.
I need to install apache on an ec2 instance.
Trying to find a list of known hosts
I run this command
ansible -i hosts all --list-hosts
and get this message
[WARNING]: Host file not found: hosts
[WARNING]: provided hosts list is empty, only localhost is available
[WARNING]: No hosts matched, nothing to do
--list-hosts lists hosts that match a --limit. The input is the -i, inventory. Your inventory is a file named hosts, which doesn't exist.
You need to create or generate an inventory file from somewhere. Ansible can't intuit what your inventory is.
If you installed Ansible by Pip, you need to create a directory with ansible.cfg and hosts file. For it, use:
sudo mkdir /etc/ansible/
sudo touch /etc/ansible/hosts
So you will be able to use the command below:
cat /etc/ansible/hosts
Got permission to ssh to the target server. Now i can install on this target server.
If I can login as an ec2-user by being part of a management domain then I can access any server

Writing a string to file using Ad-Hoc Commands in Ansible

I'm a beginner with Ansible and trying to write a string to a file with an Ad-Hoc command I'm trying to play around with the replace module. The file I'm trying to write to is /etc/motd/.
ansible replace --sudo /etc/motd "This server is managed by Ansible"
Any help would be appreciated thanks!
Have a look at the lineinfile module usage and a general syntax for Ad hoc commands.
What you are looking for is:
ansible target_node -b -m lineinfile -a 'dest=/etc/motd line="This server is managed by Ansible"'
in extended form:
ansible target_node --become --module-name=lineinfile --args='dest=/etc/motd line="This server is managed by Ansible"'
Explanation:
target_node is the hostname or group name as defined in the Ansible inventory file
--become (-b) instructs Ansible to use sudo
-module-name (-m) specifies the module to run (lineinfile here)
--args (-a) passes arguments to the module (these change depending on a module)
dest points to the destination file
line instructs Ansible to ensure a particular line is in the file
If you would like to replace the whole contents of the /etc/motd you should use copy module.
ansible target_node -b -m copy -a 'dest=/etc/motd content="This server is managed by Ansible"'
Notice one of the arguments is changed accordingly.

Creating Multiple host file in Ansible

Is it possible to create multiple host file in Ansible. These files are getting created dynamically.
New host file name is A,B and C
I want to do ssh into a machine which is present is B. How my ansible will understand the host file B for logging into the system.
Maybe you are looking for the -i switch to specify hosts file?
ansible host_name_in_B -i hosts_file_B -m setup

Ansible dynamic inventory on GCE: success command not found

I am trying to configure my Ansible for dynamic inventory. According to the ansible documentation I am typing on my OS X laptop command line:
GCE_INI_PATH=~/.gce.ini ansible all -i gce.py -m setup hostname | success >> {"ansible_facts": {"ansible_all_ipv4_addresses": ["x.x.x.x"],
and I am getting:
-bash: success: command not found
close failed in file object destructor:
sys.excepthook is missing
lost sys.stderr
What should I type instead?
Try just running the command
GCE_INI_PATH=~/.gce.ini ansible all -i gce.py -m setup
if this works you will get a stream of JSON output (from the Ansible setup module) showing the system information about any GCE hosts you have set up.

Resources