Ansible: Run modules on a target host in a virtual enviroment - ansible

I want to run the os_server module on a target host which has a virtual enviroment where openstacksdk is installed. If I try to run the script as shown below it results in a error "conflicting action statements: virtualenv, os_server". How can I specify a virtual enviroment correctly so that ansible uses it? I don't want to install openstacksdk globally and just use the venv.
- name: Create Server
virtualenv: "/home/user/otc2/bin/activate"
os_server:
state: present
auto_ip: false
...

You would set the ansible_python_interpreter hostvar for that host to point to the python binary inside the virtualenv on the remote machine, which appears to be /home/user/otc2/bin/python based on your posted snippet

Related

Ansible version difference in AWX

I have same ansible version, in a virtual machine and Ansible AWX virtual env which is Ansible 2.11. But when I run the playbook on Ansible AWX, I get errors tried to replicate in virtual machine what might be source of it, and found it's same error as If I have Ansible 2.9 in AWX.
How could this possible? is there a way to verify Ansible AWX version running beyond using debug mode on AWX?
I verified virtual env in AWX and it's setup with Ansible 2.11 and same modules used in virtual machine. So no clear idea why I get Ansible 2.9 errors in 2.11 environment. Can the ansible runner be a cause of this?
is there a way to verify Ansible AWX version running beyond using debug mode on AWX?
You could run the following playbook in AWX:
- shell: ansible -—version
register: ansible_v
delegate_to: localhost
- debug:
msg: “{{ ansible_v }}”

AWX fetch module directory missing

I'm working on this environment
ansible version = 2.5.1
python version = 2.7.17
awx version = 8.0.0
I'm trying to change my ansible project to AWX ( I installed AWX via docker)
I had two hosts called ansible-server and k8s-master
I originally fetched k8s-master's config.yaml to ansibler-server with this playbook
my fetch module worked on ansible playbook on cli (it worked on host : ansible-server)
- name: Fetch config.yaml from K8S-Master
hosts: K8S-Master
become: yes
fetch:
src: /home/vagrant/config.yaml
dest: /home/vagrant/config.yaml
flat: true
but when I play same playbook on AWX command, it shows me playbook has successed but no file exists on ansible-server
this is my inventory on AWX
Inventory : Demo-Inventory
Groups : Ansible-Server, K8S-Master
HOSTS(one each) : 192.168.32.145, 192.167.32.150
and I added my host ip on each group
I've fixed settings -> jobs -> job execution path and "extra environment variables" path to
/home/vagrant/
and
{
"HOME":"/home/vagrant/"
}
I've also checked those directory
/var/lib/awx/projects
/home/awx
I'm not sure how can I find AWX container's file system
I'll add more informations if I need to.
I found out that the file goes to container's directory
/var/lib/docker/overlay2/RANDOMCODE/merged/home/config.yaml
how can I get those file (as shell script or as another playbook)

Running Ansible setup module without an ansible.cfg

I want to run the ansible setup module, by declaring a specific host.
I am using Windows 10 machine , with windows subsystem for linux where ansible is installed.
I can run the setup module when using localhost, ie:
ansible localhost -m setup
I can run it when using the ansible.cfg file I use from one virtual environment, against a virtual machine ie:
ansible TestVm -m setup
In another virtual environment i have installed ansible without setting up an ansible.cfg file, and my hosts.yml file is in the windows (not the WSL filesystem).
No matter which directory i switch, I cannot run the setup module using for instance:
ansible -i inventory -m setup
.
Is there a way to run setup without a configuration file and if yes what part of the call am i missing?
Try this:
ansible all -i inventory -m setup
Where the inventory is a directory with at least the hosts.yml file.
And this an example of hosts.yml
all:
hosts:
192.168.2.9:
192.168.2.3:
192.168.2.4:
Also before any of these you must have added pywinrm:
pip install pywinrm

Is it possible to call ansible or ansible-playbook directly on a target host using a script or ansible itself?

I need to know if it's possible to call / execute ansible playbooks from the target machine. I think i saw a vendor do it or at least something similar. they downloaded a script and it did ran the playbook.
if this is possible how would it be done?
my goal is to run ansible as a centralized server in aws to perform tasks in mulitple environments. most are behind firewalls, any reccomendations/thoughts would be appreciated.
Sure. If your host will install Ansible on target and feed it with all the playbooks the you can run it as any other executable. Should you do that is another story but technically there's no obstacle.
You can run ansible and ansible playbook as you would any other binary on the target's $PATH, so any tool that facilitates running remote commands would work.
Because you are in AWS, one way might be to use AWS System's Manager.
If you wanted to use Ansible itself to do this you could use the shell or command modules:
- hosts: target
become: false
gather_facts: false
tasks:
- name: ansible in ansible
command: ansible --version
- name: ansible-playbook in ansible
command: ansible-playbook --version
Though, as with any situation where you reach for the shell or command modules, you have to be vigilant to maintain playbook idempotency yourself.
If you're requirement is just the ability to execute Ansible commands remotely, you might look into AWX which is the upstream project for Red Hat's Ansible Tower. It wraps ansible in a nice user interface to allow you to trigger Ansible playbooks remotely and with nice-to-haves like RBAC.
If you're ok with executing tasks remotely over ssh take a look at Sparrowdo it has out of the box facilities to run bash scripts ( read ansible executable ) remotely from one master host to another. Or you can even use it to install all the ansible dependencies or whatever you need to do for your scope.

How to get the host name of the current machine as defined in the Ansible hosts file?

I'm setting up an Ansible playbook to set up a couple servers. There are a couple of tasks that I only want to run if the current host is my local dev host, named "local" in my hosts file. How can I do this? I can't find it anywhere in the documentation.
I've tried this when statement, but it fails because ansible_hostname resolves to the host name generated when the machine is created, not the one you define in your hosts file.
- name: Install this only for local dev machine
pip:
name: pyramid
when: ansible_hostname == "local"
The necessary variable is inventory_hostname.
- name: Install this only for local dev machine
pip:
name: pyramid
when: inventory_hostname == "local"
It is somewhat hidden in the documentation at the bottom of this section.
You can limit the scope of a playbook by changing the hosts header in its plays without relying on your special host label ‘local’ in your inventory. Localhost does not need a special line in inventories.
- name: run on all except local
hosts: all:!local
This is an alternative:
- name: Install this only for local dev machine
pip: name=pyramid
delegate_to: localhost

Resources