Head first program - ansible - ansible

I have installed ansible using following commands.
sudo apt-add-repository ppa:ansible/ansible
sudo apt update
sudo apt install ansible
After i followed this Link. By using that i created a yml file called test.yml ( the code is shown below)
- name: test my new module
hosts: 127.0.1
tasks:
- name: run the new module
my_test:
name: 'hello'
new: true
register: testout
- name: dump test output
debug:
msg: '{{ testout }}'
Then i run ansible-playbook ./test.yml . I get following error.
I have no idea where i missed. Any idea is appreciated. Thank you so much.
sato:~/play_around_with_ansible$ ansible-playbook ./test.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note
that the implicit localhost does not match 'all'
ERROR! no action detected in task. This often indicates a misspelled module name, or incorrect module path.
The error appears to be in '/home/ven/play_around_with_ansible/test.yml': line 4, column 5, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
tasks:
- name: run the new module
^ here

You are missing the inventory file. Create inventory file in current folder or in the directory of your choice and provide the inventory file path to the ansible-playbook command as below or you can mention the default location of your host file in ansible.cfg
inventory file contents (sample)
[127.0.1]
localhost ansible_connection=local
command
ansible-playbook -i inventory ./test.yml
In the above command inventory is the file name with path.

seems your 'yaml' file was not able to recognize the path for custom module. Even I am new to custom module development. As per the standards you should keep the .py file in "library" directory and create the .ymal file outside of library directory.
This will help your yaml file to look into ./library directory for modules.

Related

<class 'ansible.parsing.yaml.objects.AnsibleSequence'> ERROR IN PLAYBOOK.WHAT IS WRONG WITH MAIN.YML FILE

Create a file named simplefile.txt - write a task in the main.yml file in present in fresco_when\tasks folder.- the task is to move the created simplefile.txt file to created directory i.e move the create file to /home/user/test folder.- move only if the file doesn't exist. using when in the playbook
I created a text file and then wrote this main.yml file:
hosts: localhost
tasks:
name: copy a file, but do not copy if the file already exists
command: cp challenge/fresco_when/defaults/simplefile.txt /home/usr/test/ creates=simplefile.txt
I got this error :
ERROR! unexpected parameter type in action: <class 'ansible.parsing.yaml.objects.AnsibleSequence'>
The error appears to be in '/projects/challenge/fresco_when/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: localhost
^ here
SCORE:0%
Please let me know what is wrong with the code
Based solely on the path in your question, it appears you have used the playbook structure for a tasks file within that playbook
For clarity, playbooks need to specify the hosts to which they will apply, but tasks within a playbook are going to apply to all the hosts in that play (err, more or less)
The contents of tasks/*.yml inside a playbook directory should be a yaml list consisting only of tasks (- command:, - debug:, that kind of thing), and not - hosts:)
Separately, while this isn't what you asked, you are re-implementing - copy: as ansible is likely going to warn you about when you run that task
The ansible-y way of doing that is:
- name: copy a file, but do not copy if the file already exists
copy:
src: challenge/fresco_when/defaults/simplefile.txt
dest: /home/usr/test/simplefile.txt

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.

copy and running configfiles programatically in ansible

There is a ansible code i am writing. It does two tasks, first is to copy a configuration file to target instance in groups. Second is running that config file to install the application.
I am creating configuration file and inventory programtically so that same suffix is added to both configfile name & groupname in inventory:
Configfile name example : Equivalent group names:
myappconf1 [myapp1]
hostname
myappconf2 [myapp2]
hostname
This is my code for copying files
hosts: all
tasks:
name: Copy file.role1 to host1
copy: src=/tmp/myconf1 dest=/tmp
when:
- "'myapp1' in group_names"
name: Copy Config File two to to Ldap2
copy: src=/tmp/myconf2 dest=/tmp
when:
- "'myapp2' in group_names"
This is my code for running conf file
hosts: myapp1
tasks:
- command: "/tmp/mainapp/update.sh -f myappconf1"
hosts: myapp2
tasks:
- command: "/tmp/mainapp/update.sh -f myappconf1"
But depending on user input uncertain number of conf files and groups can be created so I would like to do task more programatically. desired code may look like:
[task for copying file]
hosts: ~(myapp)
tasks:
- copy: copy the appropriate file to the host
example: copy myappconf4 to myapp4
- command: run the commmand with appropirate file
example: for myapp3, command: /tmp/mainapp/update.sh -f myappconf3
Can someone please suggest me what i can use to make my code more generic and efficient ?
You can use group variables like this,
#File : "myapp1/group_vars/all/vars_file.yml"
# Application settings
app_name: "myapp1"
copy_file: "myapp1conf1"
.
.
# other variables used for myapp1
Then use 2nd folder for 2nd app
#File : "myapp2/group_vars/all/vars_file.yml"
# Application settings
copy_file: "myapp2conf2"
.
.
# other variables used for myapp2
And In your code modify as below,
hosts: all
-tasks
-name: Copy file.role1 to host1
copy: src=/tmp/{{ copy_file }} dest=/tmp
-name : execute script
command: "/tmp/mainapp/update.sh -f {{ app_name }}"
Now depending upon the environment use the user input during the play time as below,
ansible-play -i myapp1 main.yml
or
ansible-play -i myapp2 main.yml
I populated variables with my python code in below format to resolve the issue:
[myapp]
host1 conf_file="myappconf1"
host2 conf_file="myappconf2"
then in my code i used the variable for both copy and running configfile tasks.

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.

a newbee trying to use ansible and a weird error

I have this playbook:
- name: main playbook
hosts: 127.0.0.1
connection: local
sudo: Yes
gather_facts: True
vars_files:
- /home/core/REPO/alonso/core2door-integration/workflows/core2door.ansible/vars-feed.yml
tasks:
- include: tasks/feed-adapter.yml
tasks/feed-adapter.yml
# This playbook deploys feed-adapter application in this host. This app needs a jar that basically writes to HDFS and write a log in a system,
# actually Impala. Variables are described within vars-feed.yml file.
- name: feed-adapter playbook
hosts: "{{host_feed}}"
remote_user: "{{remote-user}}"
sudo: Yes
- name: Creates feed_adapter_outputpath directory
file: path=/var/app/feed-adapter/ state=directory
- name: Creates check-feed-adapter-folders_outputpath directory
file: path=/var/app/check-feed-adapter-folders/ state=directory
- name: Copy supervisor conf files to /etc/supervisor.d folder
copy: src=/home/core/REPO/alonso/core2door-integration/feed-adapter/feed_adapter_Sip_Pub.conf dest=/etc/supervisor.d/feed_adapter_Sip_Pub.conf owner=root group=root mode=0644
- name: Copy generated jar to destination folder
copy: src=/home/core/REPO/alonso/core2door-integration/feed-adapter/target/feed-adapter-0.0.1-SNAPSHOT.jar dest=/var/app/check-feed-adapter-folders/feed-adapter.jar mode=0644
- name: Copy necessary .properties files to destination folder
copy: src=/home/core/REPO/alonso/core2door-integration/feed-adapter/feed-adapter-SIP-Pub.properties dest=/var/app/check-feed-adapter-folders/feed-adapter-SIP-Pub.properties mode=0644
- name: Copy check-feed-adapter-folders jar to destination folder
copy: src=/home/core/REPO/alonso/core2door-integration/CheckFeedAdapterFolders/target/CheckFeedAdapterFolders-0.0.1-SNAPSHOT.jar dest=/var/app/check-feed-adapter-folders/CheckFeedAdapterFolders.jar mode=0644
- name: Copy check.sh script to destination folder
copy: src=/home/core/REPO/alonso/core2door-integration/CheckFeedAdapterFolders/check.sh dest=/var/app/check-feed-adapter-folders/check.sh mode=0644
and when I try to run the playbook, it returns me this output:
$ ansible-playbook playbook.yml --syntax-check --i hosts.txt
/usr/lib/python2.6/site-packages/setuptools-12.0.5-py2.6.egg/pkg_resources/__init__.py:1222: UserWarning:
/home/core/.python-eggs is writable by group/others and vulnerable to
attack when used with get_resource_filename. Consider a more secure
location (set with .set_extraction_path or the PYTHON_EGG_CACHE
environment variable).
playbook: playbook.yml
ERROR: copy is not a legal parameter at this level in an Ansible Playbook
Please help, i am newbee with ansible.
UPDATE
after Daniel's advice, I have indented carefully the playbook and I am getting this error:
$ vi tasks/feed-adapter.yml
$ ansible-playbook playbook.yml --syntax-check --i hosts.txt
/usr/lib/python2.6/site-packages/setuptools-12.0.5-py2.6.egg/pkg_resources/__init__.py:1222: UserWarning:
/home/core/.python-eggs is writable by group/others and vulnerable to
attack when used with get_resource_filename. Consider a more secure
location (set with .set_extraction_path or the PYTHON_EGG_CACHE
environment variable).
playbook: playbook.yml
ERROR: file is not a legal parameter at this level in an Ansible Playbook
UPDATE 2:
The execution of ansible-playbook with -vvvv shows the same output:
ERROR: file is not a legal parameter at this level in an Ansible Playbook
[core#dub-vcd-vms171 core2door.ansible]$ ansible-playbook --i hosts.txt -vvvv playbook.yml
/usr/lib/python2.6/site-packages/setuptools-12.0.5-py2.6.egg/pkg_resources/__init__.py:1222: UserWarning: /home/core/.python-eggs is writable by group/others and vulnerable to attack when used with get_resource_filename. Consider a more secure location (set with .set_extraction_path or the PYTHON_EGG_CACHE environment variable).
ERROR: file is not a legal parameter at this level in an Ansible Playbook
I guess that the error comes with this line:
- name: Creates feed_adapter_outputpath directory
file: path=/var/app/feed-adapter/ state=directory
You have to be very careful with line indention when working with yaml files. The error here is probably caused by the copy element in line 28:
- name: ...
copy: ...
Add two whitespaces in front of the copy, so it looks like this:
- name: ...
copy: ...

Resources