How to use "echo" in ansible? - ansible

I am getting the below error when I try to run the following code:
ERROR! Syntax Error while loading YAML.
The error appears to have been in '/home/shanthi/ansible-5g/roles/ymlRoles/tasks/main.yml': line 5, column 7, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
-name: adding yml files
shell: echo '
^ here
Here is the code:
- name: adding yml files
shell: echo '
ops-center:
product:
autoDeploy: true
helm:
api:
release: cnee-ops-center
namespace: cnee
repository:
url: http://engci-maven-master.cisco.com/artifactory/mobile-cnat-charts-release/builds/2019.01-5/amf.2019.01.01-5/
name: amf
' > amf.yml

You should avoid using the shell module. Instead, use the copy module to copy a file to remote hosts.
Contents of /path/to/local/amf.yml
ops-center:
product:
autoDeploy: true
helm:
api:
release: cnee-ops-center
namespace: cnee
repository:
url: http://engci-maven-master.cisco.com/artifactory/mobile-cnat-charts-release/builds/2019.01-5/amf.2019.01.01-5/
name: amf
Playbook task
- name: Copy amf.yml to host
copy:
src: /path/to/local/amf.yml
dest: /path/to/remote/amf.yml

Related

How can I create YML script that copy txt file from one place to another?

I start to learn Ansible and due to a tutorial exercise I needed to copy and rename afile.txt.
Can someone who is more expert help me understand what I am doing wrong?
Here is the yml:
-name: display return
hosts: all
tasks:
-name: copy and rename
command mc src=/home/scrapbook/tutorial/afile.txt dest=/home/ubuntu/afile_copy.txt
register: output
-debug: var=output
To execute it I use:
ansible-playbook -i myhosts test.yml
And the error message I get is:
ERROR: Syntax Error while loading YAML script, test.yml
Note: The error may actually appear before this position: line 3, column 1
- name: display return
hosts: all
^
please make your yaml to be code visible (like you see my yaml code below) So that it's easy to understand.
coming to your YAML, i am sure you missed copy module and some indentation errors also found.
use this sample yaml to copy a file//
- name: copy module sample
hosts: all
tasks:
- name: Copy file task
copy:
src: /srv/myfiles/foo.conf
dest: /etc/foo.conf
register: result
- debug:
var: result
by the way, you can refer ansible module samples in this document.

Ansible, save variable content to new file

I'm trying to create an Ansible task to save the content of a variable to a new file.
Using Ansible 2.5.13 and Python 2.7.5.
I've tried already to copy the content of the variable to a destination path where the file should be created...
- name: Save alert rule example file to Prometheus
copy:
content: "{{ alert_rule_config }}"
dest: /opt/compose/prom/alert_rule.yml
Tried also to create the file before copying the content of the variable
- name: Create alert rule file
file:
path: /opt/compose/prom/alert_rule.yml
state: touch
- name: Save alert rule example file to Prometheus
copy:
content: "{{ alert_rule_config }}"
dest: /opt/compose/prom/alert_rule.yml
Tried to wrap the destination path in quotes...
But no matter what a directory /opt/compose/prom/alert_rule.yml/ is created!
The content of the variable is something like
alert_rule_config:
groups:
- name: debug-metrics
rules:
- alert: DebugAlert
expr: test_expression
I expect the file to be created (because it does not exist) and the content of the variable to be saved to the newly created file but the task fails with
FAILED! => {"changed": false, "msg": "can not use content with a dir as dest"}
I want to avoid issuing a command and would prefer to use an Ansible module.
You need to create the target directory instead of the target file, or you will get Destination directory /opt/compose/prom does not exist in the first option or Error, could not touch target: [Errno 2] No such file or directory: '/opt/compose/prom/alert_rule.yml' in the second option.
- name: Create alert rule containing directory
file:
path: /opt/compose/prom/
state: directory
- name: Save alert rule example file to Prometheus
copy:
content: "{{ alert_rule_config }}"
dest: /opt/compose/prom/alert_rule.yml
But as #Calum Halpin says, if during your tests you did an error and created a directory /opt/compose/prom/alert_rule.yml/, you need to remove it before.
This will happen if a directory /opt/compose/prom/alert_rule.yml already exists when your tasks are run.
To remove it as part of your tasks add
- file:
path: /opt/compose/prom/alert_rule.yml
state: absent
ahead of your other tasks.

How to fix Ansible error: " conflicting action statements: include, file"

I get some error on the include statement. I have two simple playbooks, but cannot find the problem.
files.yml:
---
- name: create leading path
file:
path: "{{ path }}"
state: directory
- name: touch the file
file:
path: "{{ path + '/' + file }}"
state: touch
include.yml:
---
- name: touch files
hosts: localhost
gather_facts: false
tasks:
- include: files.yaml
path: /tmp/foo
file: herp
If I run include.yml I get this error:
ERROR! conflicting action statements: include, file
The error appears to be in '/home/ansible/mastering_ansible/touch.yml': line 7, column 13, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
tasks:
- include: files.yml
^ here
The module include is "free-form" only i.e. it has no attributes. The error is the result of the wrong syntax
tasks:
- include: files.yaml
path: /tmp/foo
file: herp
Correct
tasks:
- include: files.yaml
If you have trouble with paths see Search paths in Ansible and optionally use Special variables. For example
- include: "{{ playbook_dir }}/tasks/files.yaml"
Notes
files.yml is not a playbook.
The error message is "/home/ansible/mastering_ansible/touch.yml': line 7, column 13 ...", but there is no touch.yml among the posted code.
If the module include had attributes path and file the correct indentation would have been
- include: files.yaml
path: /tmp/foo
file: herp
See Including and Importing for details.

Ansible fails on presumably valid syntax

I work on a playbook. The valid syntax makes ansible complaining
Here is my playbook:
- hosts: tcagents
tasks:
- name: Create temporary dir C:\Tmp\windows-sdk-8.0
win_file: path=C:\TMP\windows-sdk-8.0 state=directory
- name: Copy windows-sdk-8.0/Windows-SDK-8.0.zip file to temporary dir on a node
win_copy:
src: Windows-SDK-8.0.zip
dest: C:\Tmp\windows-sdk-8.0\Windows-SDK-8.0.zip
- name: Unzip C:\TMP\windows-sdk-8.0\Windows-SDK-8.0.zip to C:\TMP\windows-sdk-8.0
win_unzip:
src: C:\TMP\windows-sdk-8.0\Windows-SDK-8.0.zip
dest: C:\TMP\windows-sdk-8.0
- win_command: C:\TMP\windows-sdk-8.0\setup.exe /Quiet /NoRestart
args:
chdir: C:\TMP\windows-sdk-8.0
It works without last three lines, but fails with them:
$ ansible-playbook -l windows windows-sdk-8.0/windows-sdk-8.0.yml
ERROR! no action detected in task. This often indicates a misspelled module name, or incorrect module path.
The error appears to have been in '/home/qaexpert/ansible-lab/windows-sdk-8.0/windows-sdk-8.0.yml': line 20, column 7, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
dest: C:\TMP\windows-sdk-8.0
- win_command: C:\TMP\windows-sdk-8.0\setup.exe /Quiet /NoRestart
^ here
Here is ansible version:
$ ansible --version
ansible 2.1.2.0
config file = /etc/ansible/ansible.cfg
configured module search path = Default w/o overrides
Please help!
Ansible win_command
is new in version 2.2 and you are running 2.1.2

ansible rsync or copy a randomly named file to a remote machine

I am using ansible 2.1 to either rsync or copy a file from the host machine to a remote one. The file is in a directory but has a random string as part of its name. I have tried using ls -d to get the name via the shell command and tried to register this value but apparently, the syntax I am using is causing the role to fail. Any thoughts on what I might be doing wrong?
---
- name: copying file to server
- local_action: shell cd /tmp/directory/my-server/target/
- local_action: shell ls -d myfile*.jar
register: test_build
- debug: msg={{ test_build.stdout }}
- copy: src=/tmp/directory/my-server/target/{{ test_build.stdout }} dest=/home/ubuntu/ owner=ubuntu group=ubuntu mode=644 backup=yes
become: true
become_user: ubuntu
become_method: sudo
exception
fatal: [testserver]: FAILED! => {"failed": true, "reason": "no action detected in task. This often indicates a misspelled module name, or incorrect module path.\n\nThe error appears to have been in '/home/user/test/roles/test-server/tasks/move.yml': line 2, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n---\n- name: transferring file to server\n ^ here\n\n\nThe error appears to have been in '/home/user/test/roles/test-server/tasks/synchronize.yml': line 2, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n---\n- name: transferring artifact to server\n ^ here\n"}
You should avoid shell commands if possible, it is Ansible anti-pattern.
In your case you can use fileglob lookup like follows:
- name: Copy file
copy:
src: "{{ lookup('fileglob','/tmp/directory/my-server/target/myfile*.jar', wantlist=true) | first }}"
dest: /home/ubuntu/
owner: ubuntu
group: ubuntu
mode: 644
backup: yes
If you 100% sure that there is only one such file, you can omit wantlist=true and | first – I used it as a safe net to filter only first entry, if there are many.
You need to simplify your commands. You also do not want to put a hyphen before a module name. This will cause a syntax error as it won't be able to identify that module as an action. You can only invoke one module per task. For example, this will not work;
- name: task one
copy: src=somefile dest=somefolder/
copy: src=somefile2 dest=somefolder2/
The two would need to be split into two separate tasks. The same for your playbook. Do the following:
- name: copying file to server
local_action: "shell ls -d /tmp/directory/my-server/target/myfile*.jar"
register: test_build
- debug: msg={{ test_build.stdout }}
- name: copy the file
copy: src={{ test_build.stdout }} dest=/home/ubuntu/ owner=ubuntu group=ubuntu mode=644 backup=yes
If possible, insert "become" in your playbook not in your tasks/main.yml file, unless you only want to use become for these two tasks and will be adding more tasks to the same playbook later on.
Note: The debug msg line is completely optional. It doesn't affect the results of the playbook in any way, all it will do is show you the folder/file name that was found as a result of the shell "ls" command.

Resources