a newbee trying to use ansible and a weird error - ansible

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: ...

Related

Cannot log Ansible output if log_path doesn’t exist

I have an ansible.cfg file that has
log_path=logdir/install.log
Where, the logdir is a new directory which doesn’t exist.
I created this logdir in the Ansible playbook as below:
- name: installation starts
hosts: localhost
gather_facts: yes
tasks:
- name: log directory creation if doesn’t exist
file:
path: logdir
state: directory
mode: 0755
- name: other tasks
………………………………………
………………………………………
Here, when this playbook is run, logdir is created but the ansible-playbook output isn’t recorded inside the install.log during the first run.
But at the second run, the logs are recorded in the logdir/install.log file.
What could be the reason and how can I get the first logs in the install.log file?

Ansible copy file using command syntax

The assignment is as follows:
Lets create a file touch afile.txt, prior to creating tasks
Create a playbook test.yml to
copy afile.txt from your control machine to host machine at /home/ubuntu/ location as afile_copy.txt and
debug the above task to display the returned value
Execute your playbook (test.yml) and observe the output
I did following
Created the afile_copy.txt using touch
created the playbook as follows:
- name: copy files
hosts: all
tasks:
- name: copy file
command: cp afile.txt /home/ubuntu/afile_copy.txt
register:output
- debug: var=output
When I run the playbook using the command
ansible-playbook -i myhosts test.yml
it fails with the error message
stderr: cp: cannot stat 'afile.txt' : no such file or directory
The afile.txt is present in directory /home/scrapbook/tutorial
You should use copy module instead of command module. command module executes on the remote node.
1)first execute the ad-hoc command for copy:
ansible all -i myhosts -m copy -a "src=afile.txt dest=/home/ubuntu/"
2) After execute the above command,execute this playbpook:
hosts: all
tasks:
stat: path=/home/ubuntu/afile_copy.txt
register: st
name: rename
command: mv afile.txt /home/ubuntu/afile_copy.txt
when: not st.stat.exists
register: output
debug: var=output
Copy module should be used instead of command module
- name: copy files
hosts: all
tasks:
- name: copy file
copy: src=afile.txt dest=/home/ubuntu/afile_copy.txt
register:output
- debug: var=output
---
- name: copy files
hosts: all
tasks:
- name: copy file
copy:
src: afile.txt
dest: /home/ubuntu/afile_copy.txt
register: output
- debug: var=output

Ansible: Limit a task in a playbook to localhost

I want to restrict an Ansible play to a specific host
Here's a cut down version of what I want:
- hosts some_host_group
tasks:
- name: Remove existing server files
hosts: 127.0.0.1
file:
dest: /tmp/test_file
state: present
- name: DO some other stuff
file:
...
I want to (as an early task), remove a local directory (I've created a file in the example as it's a more easily observed test). I was under the impression that I could limit a play to a set of hosts with the "hosts" parameter to the task -
but I get this error:
ERROR! 'hosts' is not a valid attribute for a Task
$ansible --version
ansible 2.3.1.0
Thanks.
PS I could wrap the ansible in a shell fragment, but that's ugly.
You should use delegate_to or local_action and tell Ansible to run the task only once (otherwise it will try to delete the directory as many times as target hosts in your play, although it won't be a problem).
You should also use absent not present if you want to remove directory, as you stated.
- name: Remove existing server files
delegate_to: 127.0.0.1
run_once: true
file:
dest: /tmp/test_file
state: absent
There are syntax errors in your playbook, have a look at Ansible Intro, Local Playbooks and Delegation.
- hosts: some_host_group
tasks:
- name: Remove existing server files
- hosts: localhost
tasks:
- file:
dest: /tmp/test_file
state: present
- name: DO some other stuff
file:

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 playbook error in Roles vars section while copying the main.yaml file of vars from a remote machine

Error MsgI am trying to execute a ansible playbook using roles. I have some variables, which I defined in main.yaml of vars section. I am copying these variables (main.yaml) from another remote machine.
My question is, my playbook throws an error for the first time even though it copies the main.yaml file in my vars section. When I run for second time, it executes playbook well. I am understanding, for the first time though the file is copied it doesn't read it as it was not present before the playbook was running. Is there a way I can run it successfully without an error for the first time.
Image roles will give clear idea about roles and sub files. Roles
site.yaml
---
- name: Testing the Mini project
hosts: all
roles:
- test
tasks/main.yaml
---
- name: Converting Mysql to CSV file
command: mysqldump -u root -padmin -T/tmp charan test --fields-terminated-by=,
when: inventory_hostname == "ravi"
- name: Converting CSV file to yaml format
shell: python /tmp/test.py > /tmp/main.yaml
when: inventory_hostname == "ravi"
- name:Copying yaml file from remote node to vars
shell: sshpass -p admin scp -r root#192.168.56.101:/tmp/main.yaml /etc/ansible/Test/roles/vars/main.yaml
when: inventory_hostname == "charan"
- name:Install Application as per the table
apt: name={{ item.Application }} state=present
when: inventory_hostname == {{ item.Username }}
with_items: user_app
/vars/main.yaml This will be copied from remote machine.
---
user_app:
- {Username: '"ravi"' , Application: curl}
- {Username: '"charan"' , Application: git}
Take a look at the include_vars task. It may do what you need. It looks like you need to be explicitly including /vars/main.yaml in a task before your apt task where you reference the variables.

Resources