Unable to create instance on gcp using ansible - google-cloud-platform

Trying to create a gcp instance with ansible following the steps from this github link.
playbook:
---
- hosts: localhost
connection: local
tasks:
- name: creating instance
gcp_compute_instance:
name: ansible instance
machine_type: e2-micro
disks:
- auto_delete: true
boot: true
initialize_params:
source_image: projects/debian-cloud/global/images/family/debian-11
network_interfaces:
- access_configs:
- name: External NAT
type: ONE_TO_ONE_NAT
metadata:
startup-script: |
#!/bin/bash
sudo yum install httpd -y
sudo echo "HELLO WORLD !!" > /var/www/html/index.html
sudo systemctl restart httpd
zone: us-central1-a
project: capstone-project-347806
auth_kind: serviceaccount
service_account_file: service.json
state: present
output:
PLAY [localhost] ****************************************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************************************
ok: [localhost]
TASK [creating instance] ********************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "msg": "GCP returned error: {u'error': {u'message': u"Invalid value for field 'instance': 'ansible instance'. Must be a match of regex 'a-z?|[1-9][0-9]{0,19}'", u'code': 400, u'errors': [{u'reason': u'invalid', u'message': u"Invalid value for field 'instance': 'ansible instance'. Must be a match of regex 'a-z?|[1-9][0-9]{0,19}'", u'domain': u'global'}]}}"}
PLAY RECAP **********************************************************************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0

following the documentation for attribut name:
the name must be 1-63 characters long and match the regular expression
[a-z]([-a-z0-9]*[a-z0-9])? which means the first character must be a
lowercase letter, and all following characters must be a dash,
lowercase letter, or digit, except the last character, which cannot be
a dash.
so you cant have a space in the name, so the error says the same thing...

Related

ansible regex_replace in command

I'am using ansible to deploy filestore on GCP, I need to get IP from instance and use it to create mount point.
gcloud working fine but it's return bracket and simple quote with ip.
Someone can help me to remove these caractere please ? My regex command doesn't work and i'am newbie with regex.
Error tasks mount cannot resolv come from '' in ip.stdout
ansible code:
- name: get info
shell: gcloud filestore instances describe "{{nfs_id}}" --project=xxxx-xxxx --zone=xxxxx-b --format='get(networks.ipAddresses)'
register: ip
- name: master_setup.yml --> DEBUG REGEX
debug:
var: "{{ 'ip.stdout' | regex_replace('([^\\.]*)\\.(.+)$', '\\1') }}"
- name: print mount point test
debug:
msg: "{{ip.stdout}}:/{{nfs_name }}"
- name: Mount an NFS volume
mount:
fstype: nfs
state: mounted
opts: rw,sync,hard,intr
src: "{{ip.stdout}:/{{nfs_name }}"
path: /mnt/nexus-storage
result of ansible playbook execution
TASK [install_nexus : master_setup.yml --> DEBUG REGEX] ********************************************************
ok: [nexus-xxxx.xxx.xxxxx] => {
"ip": {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"cmd": "ggcloud filestore instances describe "{{nfs_id}}" --project=xxxx-xxxx --zone=xxxxx-b --format='get(networks.ipAddresses)'",
"delta": "0:00:01.013823",
"end": "2021-03-14 21:23:32.398266",
"failed": false,
"rc": 0,
"start": "2021-03-14 21:23:31.384443",
"stderr": "",
"stderr_lines": [],
"stdout": "['1xx.xxx.xx.2']",
"stdout_lines": [
"['1xx.xxx.xx.2']"
]
}
}
TASK [install_nexus : print mount point test] ******************************************************************
ok: [nexus-xxxx.xxx.xxxxx] => {
"msg": "['1xx.xxx.xx.2']:/nfsnexusnew"
}
TASK [install_nexus : Mount an NFS volume] *********************************************************************
[WARNING]: sftp transfer mechanism failed on [nexus-ppd.preprod.d-aim.com]. Use ANSIBLE_DEBUG=1 to see detailed
information
fatal: [nexus-xxxx.xxx.xxxxx]: FAILED! => {"changed": false, "msg": "Error mounting /mnt/nexus-storage: mount.nfs: Failed to resolve server '1xx.xxx.xx.2': Name or service not known\n"}
Thx
Resolved doing this, it's not very sexy but it's working. If someone find a solution please forward me it.
I have used 2 regex because I don't how to remove simple quote and bracket in one line:
- name: get info
shell: gcloud filestore instances describe "{{nfs_id}}" --project=xxxx-xxxx --zone=xxxxx-b --format='get(networks.ipAddresses)' > /tmp/nfs-ip.txt
- name: sed regex to delete []
shell: sed -i 's/[][]//g' /tmp/nfs-ip.txt
- name: sed regex to delete ''
shell: sed -i 's|["'\'']||g' /tmp/nfs-ip.txt
- name: register result in var ip
shell: cat /tmp/nfs-ip.txt
register: ip
- name: Mount an NFS volume
mount:
fstype: nfs
state: mounted
opts: rw,sync,hard,intr
src: "{{ip.stdout}}:/{{nfs_name }}"
path: /mnt/nexus-storage
Q: "Cannot resolve ip.stdout"
A: The value stored in ip.stdout is a string
"ip": {
...
"stdout": "['1xx.xxx.xx.2']",
...
}
Use the filters from_yaml and first to get the first item of the list, e.g.
src: "{{ ip.stdout|from_yaml|first }}:/{{ nfs_name }}"

Using Ansible to enable object lifecycle managent on Google Cloud Storage buckets

I am trying to enable versioning and Object lifecycle management on a GCP storage bucket using Ansible. For this I created the following playbook
- name: create a bucket
hosts: localhost
tasks:
- name: Storage bucket
google.cloud.gcp_storage_bucket:
name: ansible-storage-module
project: adminproject-272208
auth_kind: serviceaccount
service_account_file: "/home/project/ansible/project.json"
state: present
versioning:
enabled: true
lifecycle:
rule:
action:
type: Delete
condition:
ageDays: 3
isLive: false
But it is failing with below error
PLAY [create a bucket]
************************************************************************************************
TASK [Gathering Facts]
************************************************************************************************
ok: [localhost]
TASK [Storage bucket]
*************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "msg": "argument rule is of type <type 'dict'> found in 'lifecycle'. and we were unable to convert to list: <type 'dict'> cannot be converted to a list"}
PLAY RECAP
********************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
Any suggestion to resolve this ?
UPDATES
Modified as below the lifecycle part and it seems to PASS with no error
lifecycle:
rule:
- action:
type: Delete
condition:
is_live: no
age_days: 3
- action:
type: Delete
condition:
is_live: no
num_newer_versions: 3
PLAY RECAP *******************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
However on checking cannot see neither versioning nor lifecycle rules are implemented
$ gsutil versioning get gs://ansible-storage-module/
gs://ansible-storage-module: Suspended
$ gsutil lifecycle get gs://ansible-storage-module/
gs://ansible-storage-module/ has no lifecycle configuration.

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”

how use aws profile when using ansible ec2.py module

I wrote a quick ansible playbook to launch a simple ec2 instance but I think I have an issue on how I want to authenticate.
What I don't want to do is set my aws access/secret keys as env variables since they expire each hour and I need to regenerate the ~/.aws/credentials file via a script.
Right now, my ansible playbook looks like this:
--- # Launch ec2
- name: Create ec2 instance
hosts: local
connection: local
gather_facts: false
vars:
profile: profile_xxxx
key_pair: usrxxx
region: us-east-1
subnet: subnet-38xxxxx
security_groups: ['sg-e54xxxx', 'sg-bfcxxxx', 'sg-a9dxxx']
image: ami-031xxx
instance_type: t2.small
num_instances: 1
tag_name: ansibletest
hdd_volumes:
- device_name: /dev/sdf
volume_size: 50
delete_on_termination: true
- device_name: /dev/sdh
volume_size: 50
delete_on_termination: true
tasks:
- name: launch ec2
ec2:
count: 1
key_name: "{{ key_pair }}"
profile: "{{ profile }}"
group_id: "{{ security_groups }}"
instance_type: "{{ instance_type }}"
image: "{{ image }}"
region: "{{ region }}"
vpc_subnet_id: "{{ subnet }}"
assign_public_ip: false
volumes: "{{ hdd_volumes }}"
instance_tags:
Name: "{{ tag_name }}"
ASV: "{{ tag_asv }}"
CMDBEnvironment: "{{ tag_cmdbEnv }}"
EID: "{{ tag_eid }}"
OwnerContact: "{{ tag_eid }}"
register: ec2
- name: print ec2 vars
debug: var=ec
my hosts file is this:
[local]
localhost ansible_python_interpreter=/usr/local/bin/python2.7
I run my playbook like this:
ansible-playbook -i hosts launchec2.yml -vvv
and then get this back:
PLAYBOOK: launchec2.yml ********************************************************
1 plays in launchec2.yml
PLAY [Create ec2 instance] *****************************************************
TASK [launch ec2] **************************************************************
task path: /Users/usrxxx/Desktop/cloud-jumper/Ansible/launchec2.yml:27
Using module file /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/ansible/modules/core/cloud/amazon/ec2.py
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: usrxxx
<localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo ~/.ansible/tmp/ansible-tmp-1485527483.82-106272618422730 `" && echo ansible-tmp-1485527483.82-106272618422730="` echo ~/.ansible/tmp/ansible-tmp-1485527483.82-106272618422730 `" ) && sleep 0'
<localhost> PUT /var/folders/cx/_fdv7nkn6dz21798p_bn9dp9ln9sqc/T/tmpnk2rh5 TO /Users/usrxxx/.ansible/tmp/ansible-tmp-1485527483.82-106272618422730/ec2.py
<localhost> PUT /var/folders/cx/_fdv7nkn6dz21798p_bn9dp9ln9sqc/T/tmpEpwenH TO /Users/usrxxx/.ansible/tmp/ansible-tmp-1485527483.82-106272618422730/args
<localhost> EXEC /bin/sh -c 'chmod u+x /Users/usrxxx/.ansible/tmp/ansible-tmp-1485527483.82-106272618422730/ /Users/usrxxx/.ansible/tmp/ansible-tmp-1485527483.82-106272618422730/ec2.py /Users/usrxxx/.ansible/tmp/ansible-tmp-1485527483.82-106272618422730/args && sleep 0'
<localhost> EXEC /bin/sh -c '/usr/bin/env python /Users/usrxxx/.ansible/tmp/ansible-tmp-1485527483.82-106272618422730/ec2.py /Users/usrxxx/.ansible/tmp/ansible-tmp-1485527483.82-106272618422730/args; rm -rf "/Users/usrxxx/.ansible/tmp/ansible-tmp-1485527483.82-106272618422730/" > /dev/null 2>&1 && sleep 0'
fatal: [localhost]: FAILED! => {
"changed": false,
"failed": true,
"invocation": {
"module_name": "ec2"
},
"module_stderr": "usage: ec2.py [-h] [--list] [--host HOST] [--refresh-cache]\n [--profile BOTO_PROFILE]\nec2.py: error: unrecognized arguments: /Users/usrxxx/.ansible/tmp/ansible-tmp-1485527483.82-106272618422730/args\n",
"module_stdout": "",
"msg": "MODULE FAILURE"
}
to retry, use: --limit #/Users/usrxxx/Desktop/cloud-jumper/Ansible/launchec2.retry
PLAY RECAP *********************************************************************
localhost : ok=0 changed=0 unreachable=0 failed=1
I noticed in the ec2.py file it says this:
NOTE: This script assumes Ansible is being executed where the environment
variables needed for Boto have already been set:
export AWS_ACCESS_KEY_ID='AK123'
export AWS_SECRET_ACCESS_KEY='abc123'
This script also assumes there is an ec2.ini file alongside it. To specify a
different path to ec2.ini, define the EC2_INI_PATH environment variable:
export EC2_INI_PATH=/path/to/my_ec2.ini
If you're using eucalyptus you need to set the above variables and
you need to define:
export EC2_URL=http://hostname_of_your_cc:port/services/Eucalyptus
If you're using boto profiles (requires boto>=2.24.0) you can choose a profile
using the --boto-profile command line argument (e.g. ec2.py --boto-profile prod) or using
the AWS_PROFILE variable:
AWS_PROFILE=prod ansible-playbook -i ec2.py myplaybook.yml
so I ran it like this:
AWS_PROFILE=profile_xxxx ansible-playbook -i hosts launchec2.yml -vvv
but still got the same results...
----EDIT-----
I also ran it like this:
export ANSIBLE_HOST_KEY_CHECKING=false
export AWS_ACCESS_KEY=<your aws access key here>
export AWS_SECRET_KEY=<your aws secret key here>
ansible-playbook -i hosts launchec2.yml
but still got this back...still seems to be a credentials issue?
usrxxx$ ansible-playbook -i hosts launchec2.yml
PLAY [Create ec2 instance] *****************************************************
TASK [launch ec2] **************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "module_stderr": "usage: ec2.py [-h] [--list] [--host HOST] [--refresh-cache]\n [--profile BOTO_PROFILE]\nec2.py: error: unrecognized arguments: /Users/usrxxx/.ansible/tmp/ansible-tmp-1485531356.01-33528208838066/args\n", "module_stdout": "", "msg": "MODULE FAILURE"}
to retry, use: --limit #/Users/usrxxx/Desktop/cloud-jumper/Ansible/launchec2.retry
PLAY RECAP *********************************************************************
localhost : ok=0 changed=0 unreachable=0 failed=1
---EDIT 2------
Completely removed ansible and then installed with homebrew but got the same error....so I think went to the directory that its looking for ec2.py (Using module file /usr/local/Cellar/ansible/2.2.1.0/libexec/lib/python2.7/site-packages/ansible/modules/core/cloud/amazon/ec2.py) and replaced that ec2.py with this one...https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/ec2.py....but now get this error:
Using /Users/usrxxx/ansible/ansible.cfg as config file
PLAYBOOK: launchec2.yml ********************************************************
1 plays in launchec2.yml
PLAY [Create ec2 instance] *****************************************************
TASK [aws : launch ec2] ********************************************************
task path: /Users/usrxxx/Desktop/cloud-jumper/Ansible/roles/aws/tasks/main.yml:1
Using module file /usr/local/Cellar/ansible/2.2.1.0/libexec/lib/python2.7/site-packages/ansible/modules/core/cloud/amazon/ec2.py
fatal: [localhost]: FAILED! => {
"failed": true,
"msg": "module (ec2) is missing interpreter line"
}
Seems you have placed ec2.py inventory script into your /path/to/playbook/library/ folder.
You should not put dynamic inventory scripts there – this way Ansible runs inventory script instead of ec2 module.
Remove ec2.py from your project's library folder (or Ansible global library defined in ansible.cfg) and try again.

Run ansible task on EC2 instanse

I want connect to EC2 instance and run the task in playbook
playbook.yml:
---
- hosts: 'all'
remote_user: 'ubuntu'
vars:
ruby_version: '2.2.4'
user: 'deploy'
home: '/home/{{ user }}'
rbenv_root: '{{ home }}/.rbenv'
name: 'app'
application: '{{ home }}/applications/{{ name }}'
tasks:
- name: 'apt | install dependencies'
action: 'apt pkg={{ item }}'
with_items:
- 'build-essential'
- 'libssl-dev'
- 'libyaml-dev'
- 'libreadline6-dev'
- 'zlib1g-dev'
- 'libcurl4-openssl-dev'
- 'git'
- 'nginx'
- 'postgresql'
- 'postgresql-contrib'
- 'libpq-dev'
- 'imagemagick'
- 'libmagickwand-dev'
- 'htop'
run the command:
ansible-playbook -i11.111.1.111, playbook.yml
and got this:
PLAY [all] ********************************************************************
GATHERING FACTS ***************************************************************
ok: [11.111.1.111]
TASK: [apt | install dependencies] ********************************************
failed: [11.111.1.111] => (item=build-essential,libssl-dev,libyaml-dev,libreadline6-dev,zlib1g-dev,libcurl4-openssl-dev,git,nginx,postgresql,postgresql-contrib,libpq-dev,imagemagick,libmagickwand-dev,htop) => {"failed": true, "item": "build-essential,libssl-dev,libyaml-dev,libreadline6-dev,zlib1g-dev,libcurl4-openssl-dev,git,nginx,postgresql,postgresql-contrib,libpq-dev,imagemagick,libmagickwand-dev,htop"}
stderr: E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)
E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?
I think I need to specify AWS_ACCESS_KEY and AWS_SECRET_KEY, but I can't find how to do it
With the error message E: Unable to lock the administration directory (/var/lib/dpkg/), are you root? it means you need run the ansible playbook with root.
could you try with sudo to yes?
- hosts: 'all'
remote_user: 'ubuntu'
sudo: yes
You can use this, because sudo: yes will deprecate soon.
---
- hosts: 'all'
remote_user: 'ubuntu'
become: yes