How do i print statements using debug in Ansible - ansible

The file main.yaml inside a role is below where I want to print some message like "Directory created/existed". When I use something like below I get the error -
ERROR: multiple actions specified in task: 'file' and 'Ensuring that directory exists to download the artifacts'
---
- name: Ensuring that directory exists to download the artifacts
debug:
msg: "Directory Created/Existed"
file:
path: "{{ local_server_release_location }}"
state: directory

It's not possible to use both debug and file modules in one task.
Instead, given the directory shall be present at localhost, create the directory and test its existence. For example
- name: Create directory
file:
path: "{{ local_server_release_location }}"
state: directory
delegate_to: localhost
- name: Ensuring that directory exists to download the artifacts
debug:
msg: "Directory exists"
when: local_server_release_location is directory

Related

Ansible copies a dedicate file to a namesake directory first as opposed to copying in an existing directory

Problem
Ansible Version: 2.12.10~ppa:focal
I am using packer to provision a qemu image using packer and its ansible-local provisioner (I already have an base image in which ansible is installed).
I am trying to copy a file to a destination directory (already exists) but ansible keeps creating a directory of the filename and then copies is the file to the image
Current result
/home/admin/stack/app/libsomething.so.0.5.0/libsomething.so.0.5.0
Desired result
/home/admin/stack/app/libsomething.so.0.5.0
Structure
My directory structure is as follows:
stack/
├── appstack # here is the role to be called
├── common-docs
└── common-software # here is the file to be copied
|--- bootstap.yml
the libsomething.so.0.5.0 exists in common-software/license/ directory
Role
tasks/main.yml
---
- name: create app directory and copy some previous files
copy:
src: "{{ item }}"
dest: "{{ core_directory }}/"
loop: "{{ core_services }}"
- name: app add Licensing related files to app Directory
copy:
src: "{{ playbook_dir }}/common-software/license/libsomething.so.0.5.0"
dest: "{{ core_directory }}/app/"
when: ansible_facts['architecture'] == "x86_64"
defaults/main.yml
core_directory: /home/admin/stack
core_services:
- app
there are certain files already existing under files/app which will be copied to image first and then I use another task to move the libsomething.so.0.5.0
boostrap.yml
---
- hosts: 127.0.0.1
connection: local
roles:
- appstack
Query
where am I going wrong here with a simple task of just copying the file to the destination in an already existing directory beforehand. I went through the documentation and if I remove the / from the dest parameter I get the following error:
[Errno 21] Is a directory

Ansible win_file can not create a directory on a partitioned drive?

Ansible 2.11.0
I created anI: disk partition on my Windows 2019 host under jenkins user, who is an admin. Logged in as jenkins, I can create a directory on the partition.
I have this Ansible task that simply tries to create the same directory structure, like this
- name: "Create data directory {{ pg_data_dir }}"
win_file:
path: "{{ pg_data_dir }}" # I:\pgdata\13
status: directory
I get ...
TASK [postgresql : Create data directory I:\pgdata\13] ********************************
task path: /path/ansible/exa-playbooks/roles/postgresql/tasks/install_postgresql.yml:21
redirecting (type: modules) ansible.builtin.win_file to ansible.windows.win_file
Using module file /usr/local/Cellar/ansible/3.3.0/libexec/lib/python3.9/site-packages/ansible_collections/ansible/windows/plugins/modules/win_file.ps1
Pipelining is enabled.
<10.227.xx.xx> ESTABLISH WINRM CONNECTION FOR USER: jenkins on PORT 5986 TO 10.227.xx.xx
EXEC (via pipeline wrapper)
fatal: [10.227.xx.xx]: FAILED! => {
"changed": false,
"msg": "path I:\\pgdata\\13 will not be created"
}
Any clues?
UPDATE
For now, I just changed my task to this, but the above issue persists.
- name: "Create data directory {{ pg_data_dir }}"
win_shell:
mkdir "{{ pg_data_dir }}" # I:\pgdata\13
Duh on me. It should be state, not status, like this
- name: "Create data directory {{ pg_data_dir }}"
win_file:
path: "{{ pg_data_dir }}" # I:\pgdata\13
state: directory

Read a file locally and use the vars remote in Ansible

I read a YAML file locally with the following playbook:
- name: Ensure the deploy_manifest var is defined and read deploy manifest
hosts: localhost
connection: local
gather_facts: False
tasks:
- assert:
that: deploy_manifest is defined
msg: |
Error: Must provide providers config path. Fix: Add '-e deploy_manifest=/path/to/manifest' to the ansible-playbook command
- name: Read deploy manifest
include_vars:
file: "{{ deploy_manifest }}"
name: manifest
register: manifest
- debug:
msg: "[{{ manifest.key }}]: {{ manifest.value }}"
with_dict: "{{ manifest.ansible_facts }}"
and then in the same playbook YAML file I run:
- name: Deploy Backend services
hosts: backend
remote_user: ubuntu
gather_facts: False
vars:
env: "{{ env }}"
services: "{{ manifest.ansible_facts }}"
tasks:
- include_role:
name: services_backend
when: backend | default(true) | bool
However it doesn't work because debug fails. It says that manifest is empty.
Which is the best way to read a YAML file or generally a configuration in a playbook and then have the variables passed in another playbook?
Your debug module doesn't say "that manifest is empty", it says the key manifest.key does not exist because it does not.
You registered a fact named manifest with:
register: manifest
You try to refer to a key of the above manifest named key and another key (!) named value:
msg: "[{{ manifest.key }}]: {{ manifest.value }}"
Please read Looping over Hashes chapter and acknowledge that (without using loop control) you refer to the iterated variable using item.
Please note that with name: manifest and register: manifest you read your vars file into manifest.ansible_facts.manifest.

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.

Resources