Ansible command to trigger registration on another server - ansible

I can't find any documentation on how to include a secondary server in a playbook.
If for instance, I want to install sssd on SERVERA and register with a FreeIPA server.
On the FreeIPA server (only), I need to:
get a Kerberos ticket (via kinit)
check if SERVERA is already in IPA instance
delete SERVERA from IPA if true
Since this is an installation playbook run against SERVERA, it doesn't seem right to include the IPA server in the hostlist...but nor can I see any "third party servers" module?

I presume you are searching for the delegate_to option, which allows you to delegate a task to a host that is not in the hostlist.
Often used to run things on the localhost (host running ansible), it can also be used to push a task to a host not in hostlist. The host has to be in the inventory file though.
Example:
- name: Ping the other host
ping:
delegate_to: otherhost.com # This is where you set it
More info: http://docs.ansible.com/ansible/latest/user_guide/playbooks_delegation.html#delegation

Related

ansible-pull on remote hosts

I want run the playbook on remote host, which the playbook is in github. So, I followed this blog and forked the repo https://github.com/vincesesto/ansible-pull-example
In side the repo, I have modified hosts file to my server IP. When run ansible-pull
veeru#carb0n:~/ansible-example$ ansible-pull -U https://github.com/veerendra2/ansible-pull-example -i hosts
Starting Ansible Pull at 2019-06-26 16:26:30
/usr/local/bin/ansible-pull -U https://github.com/veerendra2/ansible-pull-example -i hosts
[WARNING]: Could not match supplied host pattern, ignoring: carb0n
ERROR! Specified hosts and/or --limit does not match any hosts
Not sure why it is picking current server name carb0n even I specified -i hosts argument.
here is my hosts file
[hydrogen]
10.250.30.11
local.yml
---
- hosts: all
tasks:
- name: install example application
copy:
src: ansible_test_app
dest: /tmp/
owner: root
group: root
I had changed local.yml to hydrogen.yml, but still getting same error.
Not sure why it is picking current server name carb0n even I specified -i hosts argument.
Sure, because ansible pull is designed to run against the current host, always. If you want to run against a remote server then you are supposed to use ansible or ansible-playbook and then your specification of a host list and the connection mechanism would start to make sense again.
Using ansible-pull is designed for the cases where it is either impossible, or highly undesirable for something to connect to the managed host. That can be due to firewall, security policies, or any number of reasons. But policies are usually less strict about what a managed host can, itself, connect to, and that's why pulling configuration onto the host can be easier.

Is there a way to set up an SSH connection (pass-wordless login) between host A and host B while running playbook from hostC using ansible only?

I am trying to set up a passwordless login (copy id_rsa.pub from server A to server B) from server A to server B while running a playbook from controller machine C. The playbook:
cannot have an inventory file. The host IP will be passed from the command line to the playbook as:
ansible-playbook -i , test.yml
Server A DNS name or IP address will be hardcoded in my playbook.
I have tried:
Using fetch module, I tried fetching ssh key(id_rsa_serverA.pub) from server A to controller C and then using copy module to copy the ssh_key(id_rsa_ServerA) to Server B. While it did the work, it does not adhere to the project guidelines I am working on.
Tried 'synchronize' module with ansible 2.5. Fails.
I did a similar thing,
i use user module on serverA with option generate_ssh_key: yes and user register: user_pubkey
then i use authorized_key module with delegate_to serverB, setting the key to "{{ user_pubkey.stdout }}" for the neededuser:`
you can pass #IP of serverB as extra_vers at launch time : ansible-playbook ... ... ... -e serverB=serverB_#IP
hope this helps
cheers

Ansible playbook : How to get server up or down status

I'm designing a dashboard for displaying all servers status(up/down),free RAM,Kernel version,Processor type etc using ansible playbook. Using gather facts,I can get all server parameters easily,but not sure how to get server up/down using playbook. If server is down,ansible cannot connect to server hence 'ping' module cannot used. Please help me. IS there any way to catch the return value if playbook fails for any server ?
The order how ansible connects is 1. Make ssh connection 2.if ssh succeeds, gather facts 3. Execute playbook taks one by one. If ping is a task inside playbook, control wont even come to that place and ansible would have thrown connection error in step 1 itself. Please refer below https://docs.ansible.com/ansible/2.4/ping_module.html
Also,please note My inventory contains many servers and I need to get server status of each of them.
You could just use the result of the ping as per comment, or use the wait_for if you want to check a different port:
- hosts: all
tasks:
- wait_for: host=192.168.87.100 port=80 timeout=1
- debug: msg=ok

How to loop through Ansible inventory in Ansible Playbook but run against localhost

I have a inventory within the hosts file called [WEB], it consists of the servers below.
[WEB]
WEB01
WEB02
WEB03
When I declare hosts: WEB, it will iterate through each server and run locally on that respective server to do what it needs.
How can I take the same inventory but run what I want to do on the local Ansible server when running ansible-playbook FILE.YML? For example, I want to run a URI command that has the web server name as a parameter but as said, run it on the local Ansible server to POST to a external website. This doesn't need to run on the web servers themselves but I want to take the webserver names (WEB01, WEB02, WEB03) and run the URI module to post to a site.
Thanks!
You can loop through the host name vars using with items and delegate that to local host .
OR
If you don't want to perform any action on the hosts in the web group then you can define that in a variable in the inventory then apply with items. specify hosts as hosts: localhost

Ansible execute command locally and then on remote server

I am trying to start a server using ansible shell module with ipmitools and then do configuration change on that server once its up.
Server with ansible installed also has ipmitools.
On server with ansible i need to execute ipmitools to start target server and then execute playbooks on it.
Is there a way to execute local ipmi commands on server running ansible to start target server through ansible and then execute all playbooks over ssh on target server.
You can run any command locally by providing the delegate_to parameter.
- shell: ipmitools ...
delegate_to: localhost
If ansible complains about connecting to localhost via ssh, you need to add an entry in your inventory like this:
localhost ansible_connection=local
or in host_vars/localhost:
ansible_connection: local
See behavioral parameters.
Next, you're going to need to wait until the server is booted and accessible though ssh. Here is an article from Ansible covering this topic and this is the task they have listed:
- name: Wait for Server to Restart
local_action:
wait_for
host={{ inventory_hostname }}
port=22
delay=15
timeout=300
sudo: false
If that doesn't work (since it is an older article and I think I previously had issues with this solution) you can look into the answers of this SO question.

Resources