how to include extra package in ansible? - ansible

for example, want to use gluster_volume module.
with ansible 2.1 I get
'gluster_volume' is not a valid attribute for a Play.
How do I "include" it in my ansible?
---
- name: create gluster volume
hosts: all
gluster_volume: state=present name=test1 bricks=/bricks/brick1/g1 rebalance=yes
run_once: true

Can you try this:
---
- name: create gluster volume
hosts: all
tasks:
- gluster_volume: state=present name=test1 bricks=/bricks/brick1/g1 rebalance=yes
run_once: true

Related

How to change the host dynamically in ansible playbook

I need to change the host dynamically in ansible playbook
Below is my sample playbook
---
- name: Deployment Playbook
hosts: “{{Servers}}”
tasks:
- name: deployment
shell: "deploy.sh {{DEPLOY_NAME}}"
In above play I need to change the server with respect of DEPLOY_NAME
Example
If {{DEPLOY_NAME}}=APP
THEN {{Servers}} = 172.17.65.17
If {{DEPLOY_NAME}}=SCRIPT
THEN {{Servers}} = 172.17.65.66
Previously we passed this as inventory from AWX. But now we need to handle this on playbook.
So please help me on this issue
---
- name: Deployment Playbook targetting Servers_1, will be skipped if DEPLOY_NAME is not APP
hosts: “{{Servers_1}}”
tasks:
- name: deployment
shell: "deploy.sh {{DEPLOY_NAME}}"
when: DEPLOY_NAME == 'APP'
- name: Deployment Playbook targetting Servers_1, will be skipped if DEPLOY_NAME is not SCRIPT
hosts: “{{Servers_2}}”
tasks:
- name: deployment
shell: "deploy.sh {{DEPLOY_NAME}}"
when: DEPLOY_NAME == 'SCRIPT'
I don't think you can do that. What I think it may work for you, is to do this instead:
---
- name: Deployment Playbook
hosts: localhost
tasks:
- name: deployment
shell: ssh root#{{ item.server }} deploy.sh {{ item.app }}
loop:
- { server: 'server1', app: 'app_1' }
- { server: 'server1', app: 'app_1' }
You could even improve this, by using that "inventory from awx", loading it as a "vars_files" which contains this list. So your final loop will just iterate over that list. Like this:
---
- name: Deployment Playbook
hosts: localhost
vars_files:
- your_list_file.yml
tasks:
- name: deployment
shell: ssh root#{{ item.server }} deploy.sh {{ item.app }}
loop: "{{ your_list_variable }}"

How to add host to group in Ansible Tower inventory?

How can I add a host to a group using tower_group or tower_host modules?
The following code creates a host and a group, but they are unrelated to each other:
---
- hosts: localhost
connection: local
gather_facts: false
tasks:
- tower_inventory:
name: My Inventory
organization: Default
state: present
tower_config_file: "~/tower_cli.cfg"
- tower_host:
name: myhost
inventory: My Inventory
state: present
tower_config_file: "~/tower_cli.cfg"
- tower_group:
name: mygroup
inventory: My Inventory
state: present
tower_config_file: "~/tower_cli.cfg"
Docs mention instance_filters parameter ("Comma-separated list of filter expressions for matching hosts."), however do not provide any usage example.
Adding instance_filters: myhost to the tower_group task has no effect.
I solved it using Ansible shell module and tower-cli. I Know that create a ansible module is better than it, but to a fast solution...
- hosts: awx
vars:
tasks:
- name: Create Inventory
tower_inventory:
name: "Foo Inventory"
description: "Our Foo Cloud Servers"
organization: "Default"
state: present
- name: Create Group
tower_group:
inventory: "Foo Inventory"
name: Testes
register: fs_group
- name: Create Host
tower_host:
inventory: "Foo Inventory"
name: "host"
register: fs_host
- name: Associate host group
shell: tower-cli host associate --host "{{fs_host.id}}" --group "> {{fs_group.id}}"
This isn't natively available in the modules included with Tower, which are older and use the deprecated tower-cli package.
But it is available in the newer AWX collection, which uses the awx CLI, as long as you have a recent enough Ansible (2.9 should be fine).
In essence, install the awx collection through a requirements file, or directly like
ansible-galaxy collection install awx.awx -p ./collections
Add the awx.awx collection to your playbook
collections:
- awx.awx
and then use the hosts: option to tower_group:.
- tower_group:
name: mygroup
inventory: My Inventory
hosts:
- myhost
state: present
You can see a demo playbook here.
Be aware though that you may need preserve_existing_hosts: True if your group already contains other hosts. Unfortunately there does not seem to be an easy way to remove a single host from a group.
In terms of your example this would probably work:
---
- hosts: localhost
connection: local
gather_facts: false
collections:
- awx.awx
tasks:
- tower_inventory:
name: My Inventory
organization: Default
state: present
tower_config_file: "~/tower_cli.cfg"
- tower_host:
name: myhost
inventory: My Inventory
state: present
tower_config_file: "~/tower_cli.cfg"
- tower_group:
name: mygroup
inventory: My Inventory
state: present
tower_config_file: "~/tower_cli.cfg"
hosts:
- myhost

ansible with_items pass variables from outside file

I just want to pass list of rpm packages in a yml file and call it in with_items inside my tasks.
Which format the yml file should be. Please help me. I googled a lot, still being confused. I need to achieve so that I could change only the package names in outside file, without changing the main file.
Ex: files.yaml
---
- vars:
modules:
- firmware-system-p89-2.56_2018_01_22-1.1.i386.rpm
- firmware-smartarray-ea3138d8e8-6.30-1.1.x86_64.rpm
=> passing to with_items in another file
---
- name: List required packages
include_vars:
-files.yml
set_fact: pkglist="{{ item}}"
with_items:
- "{{ modules }}"
register: pkglist_result
Comment:
Thanks a lot. that helps.
I did just this to finally accomplish. I wasn't trying right. But I mentioned just the name of the packages in files.yml and placed the real packages in files directory where tasks directory resides.
- name: List required packages
include_vars: files.yml
register: pkglist_result
- name: make a list
set_fact: pkg_list="{{ pkglist_result.ansible_facts.modules}}"
- debug: var=pkg_list
files.yml:
---
modules:
- firmware-system-p89-2.56_2018_01_22-1.1.i386.rpm
- firmware-smartarray-ea3138d8e8-6.30-1.1.x86_64.rpm
example playbook:
---
- hosts: my_hosts
vars_files:
- files.yml
tasks:
- name: print module name one by one
debug:
msg: "{{ item }}"
with_items: "{{ modules }}"

Ansible Dynamic Inventory

I'm running a playbook which houses multiple roles targets multiple hosts
The goal is to deploy a VM and use it's IP to deploy an app.
My playbook, has two roles, using "build_vm" role I'm able to display IP address via debug, yet when passing ipaddr variable to second role, Ansible complains that the variable is not defined
- hosts: linux
become: true
roles:
- build_vm
- tasks:
- debug: msg="{{ ipaddr }}"
- hosts: "{{ ipaddr }}"
roles:
- deploy_app
I have used set_fact with and ran into same issue, I wonder what I should be using here? dynamic inventory? I have searched sparse docs online and I'm unable to find an intuitive example to follow.
There are many ways to using add_host. In this example, I am adding the new host to a group and using it in a later play.
- hosts: linux
become: true
roles:
- build_vm
- tasks:
- debug: msg="{{ ipaddr }}"
- name: Add ipaddr to host inventory
add_host: name="{{ ipaddr }}" group=NewHostGroup
- hosts: NewHostGroup
roles:
- deploy_app

how to run a particular task on specific host in ansible

my inventory file's contents -
[webservers]
x.x.x.x ansible_ssh_user=ubuntu
[dbservers]
x.x.x.x ansible_ssh_user=ubuntu
in my tasks file which is in common role i.e. it will run on both hosts but I want to run a following task on host webservers not in dbservers which is defined in inventory file
- name: Install required packages
apt: name={{ item }} state=present
with_items:
- '{{ programs }}'
become: yes
tags: programs
is when module helpful or there is any other way? How could I do this ?
If you want to run your role on all hosts but only a single task limited to the webservers group, then - like you already suggested - when is your friend.
You could define a condition like:
when: inventory_hostname in groups['webservers']
Thank you, this helps me too.
hosts file:
[production]
host1.dns.name
[internal]
host2.dns.name
requirements.yml file:
- name: install the sphinx-search rpm from a remote repo on x86_64 - internal host
when: inventory_hostname in groups['internal']
yum:
name: http://sphinxsearch.com/files/sphinx-2.2.11-1.rhel7.x86_64.rpm
state: present
- name: install the sphinx-search rpm from a remote repo on i386 - Production
when: inventory_hostname in groups['production']
yum:
name: http://sphinxsearch.com/files/sphinx-2.2.11-2.rhel6.i386.rpm
state: present
An alternative to consider in some scenarios is -
delegate_to: hostname
There is also this example form the ansible docs, to loop over a group. https://docs.ansible.com/ansible/latest/user_guide/playbooks_delegation.html -
- hosts: app_servers
tasks:
- name: gather facts from db servers
setup:
delegate_to: "{{item}}"
delegate_facts: True
loop: "{{groups['dbservers']}}"

Resources