How can I manage groups in inventory? - ansible

I have a common playbook (task) but in my last task I want to do an action on a group of hosts and when it is finished, do the same action on another group (for my test I simply empty a folder):
- name: test clean folder on hosts
ansible.builtin.file:
path: /testJO
when: inventory_hostname in groups['c_hosts']
- name: test clean folder on master
ansible.builtin.file:
path: /testJO
when: inventory_hostname in groups['master’]
I have issue with my inventory file :
/root/ansible_Folder/inventories/inventories.yml :
all:
hosts:
children:
master:
hosts:
dem-master:
c_hosts:
hosts:
dem-host:
Who knows how I can manage my inventories file please?
EDIT :
I tried your solution which seems to me coherent but i have this error message :
ERROR! conflicting action statements: hosts, tasks
The error appears to be in '/root/ansible-cortex-lab/playbooks/roles/cortex/tasks/main.yml': line 1, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- hosts: c_hosts
^ here
I just copy past both config file you gave to me.
Do you know why I have this behaviour please ?

Separate the plays, e.g.
- hosts: c_hosts
tasks:
- name: test remove folder
ansible.builtin.file:
path: /testJO
state: absent
- name: test create folder
ansible.builtin.file:
path: /testJO
state: directory
- hosts: master
tasks:
- name: test remove folder
ansible.builtin.file:
path: /testJO
state: absent
- name: test create folder
ansible.builtin.file:
path: /testJO
state: directory
The module file can't simply clean a folder. Instead, remove a folder and create it again.
To fix the inventory, remove the 2nd line hosts:. See Inventory basics: formats, hosts, and groups
all:
children:
master:
hosts:
dem-master:
c_hosts:
hosts:
dem-host:

Related

Each Ansible task in playbook takes 10s to even start

I run the script from AnsibleTower and use WinRM to execute it on a Windows machine.
This script just creates a directory. It takes 8-10s to run it.
In some completely different environments, it takes < 1s.
What could be wrong in the environment where it takes 8-10s to run each of these tasks?
- hosts: the_test_host
tasks:
- name: Create 1st file
win_file:
path: D:\test_1.txt
state: touch
- name: Create 2nd file
win_file:
path: D:\test_2.txt
state: touch
- name: Create 3rd file
win_file:
path: D:\test_3.txt
state: touch
- name: Create 4th file
win_file:
path: D:\test_4.txt
state: touch
- name: Create 5th file
win_file:
path: D:\test_5.txt
state: touch
The main source of playbook start slowdown is fact gathering. Turn it off:
- hosts: the_test_host
gather_facts: false
tasks:
...
The other issues may be with network (latency), gss pam module (for centos; disable it if you can).
If you are desperate for speed, you can try to use connection multiplexing and mitogen, but both would require a decent amount of attention and will yield few unpleasant surprises.

Ansible win_file module register results

I am using the win_file Ansible module to create directories and files on Windows 2012 R2 servers. I want to register the results of the win_file task but I can't get it to work.
For example this playbook...
---
- hosts: windows_server
gather_facts: no
- name: create directory on remote windows server
win_file:
path: 'c:\temp}'
state: directory
register: task_results
debug: var=task_results
...does not print the results of the task.
Any help would be greatly appreciated.
Your playbook syntax is flawed... Try this:
---
- hosts: windows_server
gather_facts: no
tasks:
- name: create directory on remote windows server
win_file:
path: 'c:\temp'
state: directory
register: task_results
- debug: var=task_results
register is a part of task not module's parameter, so it should be with the same indentation.
debug is a new task, so it should be another item in a list.
Also there is not tasks keyword in your example.

How to get facts at each command in Ansible

Ansible get facts only at start. But i need check facts at each commands.
For example:
I need create a directory, after that i need put file to this directory. But ansible get fact 'dir doesn't exist' at start, create dir and at next step fact still FALSE and ansible skip this step =( And do this step only after second run.
I'll try setup after all steps to gathering facts again but it doesn't work.
I do it like this:
- stat: path=/etc/zabbix/scripts/rabbitmq
register: rmqscriptdir
- name: Create scripts dir if not exist
when: rmqscriptdir.stat.exists == False
shell: mkdir /etc/zabbix/scripts/rabbitmq
- name: Gathering facts again
setup:
- name: Set owner and permissions to rabbitmq directory
when: rmqscriptdir.stat.exists == True
file: path=/etc/zabbix/scripts/rabbitmq owner=zabbix group=root mode=0750
- stat: path=/etc/zabbix/scripts/rabbitmq/api.py
register: rmqscript_api
- name: Create api.py if not exist
when: rmqscript_api.stat.exists == False and rmqscriptdir.stat.exists == True
shell: cd /etc/zabbix/scripts/rabbitmq; wget https://raw.githubusercontent.com/jasonmcintosh/rabbitmq-zabbix/master/scripts/rabbitmq/api.py
- name: Gathering facts again
setup:
- name: Set owner and permissions to api.py
when: rmqscript_api.stat.exists == True
file: path=/etc/zabbix/scripts/rabbitmq/api.py owner=zabbix group=root mode=0755
I think you misunderstand what the setup module does. By registering a value it does not become a fact that will be reloaded by the setup module when run again. Your registered value stays the same. If you want to check again if a path exists you do not need to re-run the setup module, but the stats module and again register its output.
But anyway, the idea of Ansible is actually to not manually check if every task should be executed or not. That is something Ansible takes care for you, Ansible in general is indepotent, meaning it will have the same result no matter how many times you run the play.
Here is a cleaned up version, which creates a folder and downloads the file. If the folder already exists, the 1st task will do nothing. If the file api.py already exists, the 2nd task will do nothing.
- name: Create scripts dir if not exist
file:
path: /etc/zabbix/scripts/rabbitmq
state: directory
owner: zabbix
group: root
mode: 0750
- name: Create api.py if not exist
get_url:
url: https://raw.githubusercontent.com/jasonmcintosh/rabbitmq-zabbix/master/scripts/rabbitmq/api.py
dest: /etc/zabbix/scripts/rabbitmq/api.py
owner: zabbix
group: root
mode: 0755
PS: If you want to see which values are reloaded by the setup module, you can register its output and show it in a debug task, like so:
- setup:
register: all_server_facts
- debug:
var: all_server_facts
This only contains server facts, info about cpu, hard drives, network etc. Also see this answer for an example output.

ansible synchronize with excludes file role

Im creating an ansible role for deploying laravel5 project,
now I do that with a "synchronize" (rsync)
I have my excludes file for the rsync, and the files structured like this:
the: "deploy-laravel5" role:
files
excludes
tasks
main.yml
now here is the tasks in main.yml:
- name: deploy laravel projects
synchronize:
src: "{{item.src}}"
dest: "{{item.dest}}"
rsync_opts:
- "--exclude-from=excludes"
with_items: "{{projects}}"
some playbook:
---
- hosts: php
gather_facts: no
vars:
projects:
- {src: "../../twitter/", dest: "/web/boom/", envFile: "twitter.env"}
roles:
- deploy-laravel5
now when I run that, ansible says it can't find the "excludes" file
msg: rsync: failed to open exclude file excludes: No such file or directory (2)
I tried many different paths but nothing, any ideas how to point to the excludes file?
After I browsed the web and the docs quite extensively, I found that you can't define othe files in the "role" only files for templating or copying.
But you can define the file inside a playbook, so now the file structure is like this:
roles/
deploy/ (NOT "deploy-laravel5" as before)
playbooks/
deploy-laravel5/
excludes-file
deploy-playbook.yml
and the playbook looks quite the same:
---
- hosts: php
gather_facts: no
vars:
projects:
- {src: "../../twitter/",
dest: "/web/boom/",
excludes-file: "path/to/excludes/file",
envFile: "twitter.env"}
roles:
- deploy

error conflicting actions statements

My playbook appears as follows:
hosts: localhost
tasks:
name: Get the build synchronize:
mode=pull src=jenkins_server_ip:/home/capsilon/Jenkins/trunk/builds/{{item}}/ dest=/home/builds/{{item}}
with_items:
['as2-client', 'amc-gateway', 'router']
hosts: localhost
tasks:
name: Zip and send
command: /bin/sh "/home/zipfile.sh"
hosts: windows
tasks:
name: Deployment
win_get_url:
url: 'http://server_ip/builds/build.zip'
dest: 'D:\build.zip'
win_unzip:
src: D:\build.zip
dest: D:\
Get the following error:
ERROR! conflicting action statements
The error appears to have been in '/etc/ansible/playbooks/new_logic_zip.yaml': line 16, column 7, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
tasks:
- name: Deployment
^ here
What is the error here??
Using latest git developer code. Any help would be really appreciated.
It needs to have quotes around the src and dest on the win_unzip task which also need to be defined separate to win_get_url:
- name: Deployment
win_get_url:
url: 'http//server_ip/builds/build.zip'
dest: 'D:\build.zip'
- win_unzip:
src: 'D:\build.zip'
dest: 'D:\'

Resources