how to solve ansible unknown url? - amazon-web-services

roles/tasks/main.yml code
- name: Download and unpack node exporter binary to /usr/local/bin
unarchive:
src: https://github.com/prometheus/node_exporter/releases/download/v1.2.2/node_exporter-1.2.2.linux-amd64.tar.gz
dest: /usr/local/bin/
remote_src: yes
extra_opts: [--strip-components=1]
owner: "ec2-user"
group: "ec2-user"
node-exporter.yml code
---
- hosts: node-exporter
become: false
gather_facts: false
roles:
- roles
error message
fatal: [ip]: FAILED! => {"changed": false, "msg": "Failure downloading https://github.com/prometheus/node_exporter/releases/download/v1.2.2/node_exporter-1.2.2.linux-amd64.tar.gz, Request failed: <urlopen error [Errno -2] Name or service not known>"}
if I run "ansible -m ping node-exporter", I receive pong. and "ping www.google.com" working well
but, this code not working
Help me how to solve this problem or recommend me any code ....
(I use amazon linux)

Something weird is happening there. At your role code you have https://github.com/ and in the error ssh://github.com, are you sure that you are using the last version of your code or something like that?

Related

Error in Ansible Playbook where Cloudwatch Agent Status is being checked

can you help me? This is my ansible script:
---
- hosts: "{{host_list}}"
remote_user: root
gather_facts: true
tasks:
- name: Check if Cloudwatch Agent is Installed Already
command: service status amazon-cloudwatch-agent
register: init_status_result
ignore_errors: yes
- debug:
var: init_status_result.stderr
verbosity: 4
- name: Create Directory for Downloading Cloudwatch Agent zip
file:
path: /opt/aws/amazon-cloudwatch-zip
state: directory
owner: root
group: root
mode: '0755'
recurse: no
when: init_status_result.stderr is search ("For other actions, please try to use systemctl")
I have this error when attempting to run my playbook (I just want a way really to run through the playbook if the status check of the cloudwatch agent service is not found.):
user1#ansible01-infra-mgnt:~/.ansible/playbooks/cw_agent$ ansible-playbook -K -i /home/user1/.ansible/etc/hosts --extra-vars="host_list=11.22.33.44" install_cw_agent.yml
SUDO password:
PLAY [11.22.33.44] **************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************************************************************************************
ok: [11.22.33.44]
TASK [Check if Cloudwatch Agent is Installed Already] ***************************************************************************************************************************************************************************************
[WARNING]: Consider using the service module rather than running service. If you need to use command because service is insufficient you can add warn=False to this command task or set command_warnings=False in ansible.cfg to get rid
of this message.
fatal: [11.22.33.44]: FAILED! => {"changed": false, "cmd": "service status amazon-cloudwatch-agent", "msg": "[Errno 2] No such file or directory", "rc": 2}
...ignoring
TASK [debug] ********************************************************************************************************************************************************************************************************************************
skipping: [11.22.33.44]
TASK [Create Directory for Downloading Cloudwatch Agent zip] ********************************************************************************************************************************************************************************
fatal: [11.22.33.44]: FAILED! => {"msg": "The conditional check 'init_status_result.stderr is search (\"For other actions, please try to use systemctl\")' failed. The error was: Unexpected templating type error occurred on ({% if init_status_result.stderr is search (\"For other actions, please try to use systemctl\") %} True {% else %} False {% endif %}): expected string or buffer\n\nThe error appears to have been in '/home/user1/.ansible/playbooks/cw_agent/install_cw_agent.yml': line 15, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Create Directory for Downloading Cloudwatch Agent zip\n ^ here\n"}
to retry, use: --limit #/home/user1/.ansible/playbooks/cw_agent/install_cw_agent.retry
PLAY RECAP **********************************************************************************************************************************************************************************************************************************
11.22.33.44 : ok=2 changed=0 unreachable=0 failed=1 ```
Try shell module to check the service status instead of command module .
shell: “service status service_name”

Ansible add EC2 with add_host but connection gives missing Python error

I have used Ansible to create 1 AWS EC2 instance using the examples in the Ansible ec2 documentation. I can successfully create the instance with a tag. Then I temporarily add it to my local inventory group using add_host.
After doing this, I am having trouble when I try to configure the newly created instance. In my Ansible play, I would like to specify the instance by its tag name. eg. hosts: <tag_name_here>, but I am getting an error.
Here is what I have done so far:
My directory layout is
inventory/
staging/
hosts
group_vars/
all/
all.yml
site.yml
My inventory/staging/hosts file is
[local]
localhost ansible_connection=local ansible_python_interpreter=/home/localuser/ansible_ec2/.venv/bin/python
My inventory/staging/group_vars/all/all.yml file is
---
ami_image: xxxxx
subnet_id: xxxx
region: xxxxx
launched_tag: tag_Name_NginxDemo
Here is my Ansible playbook site.yml
- name: Launch instance
hosts: localhost
gather_facts: no
tasks:
- ec2:
key_name: key-nginx
group: web_sg
instance_type: t2.micro
image: "{{ ami_image }}"
wait: true
region: "{{ region }}"
vpc_subnet_id: "{{ subnet_id }}"
assign_public_ip: yes
instance_tags:
Name: NginxDemo
exact_count: 1
count_tag:
Name: NginxDemo
exact_count: 1
register: ec2
- name: Add EC2 instance to inventory group
add_host:
hostname: "{{ item.public_ip }}"
groupname: tag_Name_NginxDemo
ansible_user: centos_user
ansible_become: yes
with_items: "{{ ec2.instances }}"
- name: Configure EC2 instance in launched group
hosts: tag_Name_NginxDemo
become: True
gather_facts: no
tasks:
- ping:
I run this playbook with
$ cd /home/localuser/ansible_ec2
$ source .venv/bin/activate
$ ansible-playbook -i inventory/staging site.yml -vvv`
and this creates the EC2 instance - the 1st play works correctly. However, the 2nd play gives the following error
TASK [.....] ******************************************************************
The authenticity of host 'xx.xxx.xxx.xx (xx.xxx.xxx.xx)' can't be established.
ECDSA key fingerprint is XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.
Are you sure you want to continue connecting (yes/no)? yes
fatal: [xx.xxx.xxx.xx]: FAILED! => {"changed": false, "module_stderr":
"Shared connection to xx.xxx.xxx.xx closed.\r\n", "module_stdout": "/bin/sh:
1: /usr/bin/python: not found\r\n", "msg": "MODULE FAILURE", "rc": 127}
I followed the instructions from
this SO question to create the task with add_hosts
here to set gather_facts: False, but this still does not allow the play to run correctly.
How can I target the EC2 host using the tag name?
EDIT:
Additional info
This is the only playbook I have run to this point. I see this message requires Python but I cannot install Python on the instance as I cannot connect to it in my play Configure EC2 instance in launched group...if I could make that connection, then I could install Python (if this is the problem). Though, I'm not sure how to connect to the instance.
EDIT 2:
Here is my Python info on the localhost where I am running Ansible
I am running Ansible inside a Python venv.
Here is my python inside the venv
$ python --version
Python 2.7.15rc1
$ which python
~/ansible_ec2/.venv/bin/python
Here are my details about Ansible that I installed inside the Python venv
ansible 2.6.2
config file = /home/localuser/ansible_ec2/ansible.cfg
configured module search path = [u'/home/localuser/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /home/localuser/ansible_ec2/.venv/local/lib/python2.7/site-packages/ansible
executable location = /home/localuser/ansible_ec2/.venv/bin/ansible
python version = 2.7.15rc1 (default, xxxx, xxxx) [GCC 7.3.0]
Ok, so after a lot of searching, I found 1 possible workaround here. Basically, this workaround uses the lineinfile module and adds the new EC2 instance details to the hosts file permanently....not just for the in-memory plays following the add_host task. I followed this suggestion very closely and this approach worked for me. I did not need to use the add_host module.
EDIT:
The line I added in the lineinfile module was
- name: Add EC2 instance to inventory group
- lineinfile: line="{{ item.public_ip }} ansible_python_interpreter=/usr/bin/python3" insertafter=EOF dest=./inventory/staging/hosts
with_items: "{{ ec2.instances }}"

Ansible - EC2 create new and remove tag

I've more than 100 running instances
Example I have 10 running instances with tag name dev-redis-slave. And now I want to create new tag -> tag ServiceName: redis-slave and tag ServiceGroup: redis
First of all, I try following this guide: https://aws.amazon.com/blogs/apn/getting-started-with-ansible-and-dynamic-amazon-ec2-inventory-management/
Then I try to excecute ec2.py --list | grep redis. Then the output is: tag_Name_dev_redis_slave. Also I try to ping: ansible -m ping tag_Name_dev_redis_slave and success.
Next I want to create new tag for dev-redis-slave using ansible.,
I create yaml file like this (playbook.yaml).
- hosts: localhost
gather_facts: yes
tasks:
- name: Adding tags
ec2_tag:
resource: tag_Name_dev_redis_slave
region: xxx
state: present
tags:
ServiceGroup: redis
ServiceName: redis-slave
I run ansible-playbook playbook.yaml But It give an error.
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: <Response><Errors><Error><Code>InvalidID</Code><Message>The ID 'tag_Name_dev_redis_slave' is not valid</Message></Error></Errors><RequestID>de51df48-df26-4312-8d03-4c8ca2b993bf</RequestID></Response>
fatal: [localhost]: FAILED! => {"changed": false, "module_stderr": "Traceback (most recent call last):\n File \"/var/folders/5b/hhh0h2fx2cxf7_24dmn05ht00000gq/T/ansible_460t6taz/ansible_module_ec2_tag.py\", line 183, in <module>\n main()\n File \"/var/folders/5b/hhh0h2fx2cxf7_24dmn05ht00000gq/T/ansible_460t6taz/ansible_module_ec2_tag.py\", line 160, in main\n ec2.create_tags(resource, dictadd)\n File \"/Users/fourirakbar/Documents/ansible/venv/lib/python3.6/site-packages/boto/ec2/connection.py\", line 4219, in create_tags\n return self.get_status('CreateTags', params, verb='POST')\n File \"/Users/fourirakbar/Documents/ansible/venv/lib/python3.6/site-packages/boto/connection.py\", line 1227, in get_status\n raise self.ResponseError(response.status, response.reason, body)\nboto.exception.EC2ResponseError: EC2ResponseError: 400 Bad Request\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Response><Errors><Error><Code>InvalidID</Code><Message>The ID 'tag_Name_dev_redis_slave' is not valid</Message></Error></Errors><RequestID>de51df48-df26-4312-8d03-4c8ca2b993bf</RequestID></Response>\n", "module_stdout": "", "msg": "MODULE FAILURE", "rc": 1}
I also try to follow this: http://ansible-manual.readthedocs.io/en/latest/ec2_tag_module.html#examples . But also error.
how to fix it?
sorry, I was wrong to define resource. I think resource=tag, but the correct one is resource=instance id. Thankyou for helping
so I change my playbook.yaml like this
- hosts: localhost
gather_facts: yes
vars:
development:
- YOUR INSTANCE ID
- YOUR INSTANCE ID
tasks:
- name: Adding tags
ec2_tag:
resource: "{{ item }}"
region: YOUR INSTANCE REGION
args:
tags:
ServiceGroup: redis
ServiceName: redis-slave
with_items: "{{ development }}"
but I was wondering, maybe next I don't need to describe instance id one by one (imagine if we've so many instance with different group tag).
guys if you have an answer, please let me know
thank you very much

Ansible error - No Authentication Handler found:

i am trying to download a file from s3 bucket , i did aws configure and also exported my access key and secret key but i am still getting the same error. Please suggest me
Code:
- name: Download xx tarball
s3:
bucket: xxx
object: folder/xx-commandline-4.0.3.tar.gz
dest: '/tmp/{{ xx_tarball }}'
mode: get
when: 'st.stat.exists == false'
Error:
fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "msg": "No Authentication Handler found: No handler was ready to authenticate. 1 handlers were checked. ['HmacAuthV1Handler'] Check your credentials "}
ansible --version
ansible 2.0.0.2
uname -a
Linux ip-xx-xxx-xx-x 4.4.0-1026-aws #35-Ubuntu SMP Thu Jul 20 21:59:09 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
You need to check couple of things:
I- boto is installed on the target host, where you need to download the file from s3:
sudo -H pip install boto
II- If this is remote host then use this format:
- name: Download xx tarball
s3:
aws_access_key: "{{ AWS_S3_ACCESS_KEY }}"
aws_secret_key: "{{ AWS_S3_SECRET_KEY }}"
bucket: xxx
object: folder/xx-commandline-4.0.3.tar.gz
dest: '/tmp/{{ xx_tarball }}'
mode: get
when: st.stat.exists == false
Note: when you export the AWS credentials, it work locally but not for the remote host, so you need to pass the credentials to the module so it will work for remote host.
Hope it might help you

ansible local_action returns error "invalid output was: [sudo via ansible, key=xxx] password:"

I've being trying to run this ansible playbook to get a AWS resource tags:
- name: list resource tags
local_action: ec2_tag resource=i-abcdefg region=us-east-1 state=list
register: result
And this error is returned:
failed: [ec2-11-222-333-444.compute-1.amazonaws.com] => {"failed":
true, "parsed": false} invalid output was: [sudo via ansible,
key=heoqwlqnhxlxyzwnxmtbvmdtvmvjbsux] password:
FATAL: all hosts have already failed -- aborting
How can I fix that
You cannot run this local_action as root. Change your task to be:
- name: list resource tags
sudo: false
local_action: ec2_tag resource=i-abcdefg region=us-east-1 state=list
register: result