Ansible get_url Fails If Destination File Does Not Exist - ansible

When using get_url, it fails if the destination file does not exist locally. If I us vi and create the file with nothing in it, then the call works and the file is replaced. I added force: yes to encourage it to write out the file, but every attempt failed until I created a dummy file.
Any help is appreciated.
Thank you!
---
- name: Download HashiCorp Installation Archive File
hosts: 127.0.0.1
vars:
vault:
distro: 'linux'
version: '1.5.4'
user:
group: 'rcouch'
name: 'rcouch'
tasks:
# https://releases.hashicorp.com/vault/1.5.4/vault_1.5.4_linux_amd64.zip
- name: Download Binary
get_url:
url: "https://releases.hashicorp.com/vault/{{ vault.version }}/vault_{{ vault.version }}_{{ vault.distro }}_amd64.zip"
checksum: "sha256:https://releases.hashicorp.com/vault/{{ vault.version }}/vault_{{ vault.version }}_SHA256SUMS"
force: yes
dest: "vault_{{ vault.version }}_{{ vault.distro }}_amd64.zip"
owner: "{{ user.group }}"
group: "{{ user.name }}"
mode: 0755
register: vault_download
- name: Display Vault Download
debug:
msg:
- "vault_download: {{ vault_download }}"

The get_url module takes an absolute path in dest. A small excerpt from the module's page. Try an absolute path like /tmp or ./.
| dest | Absolute path of where to download the file to. |
tasks:
- get_url:
url: https://releases.hashicorp.com/vault/1.5.4/vault_1.5.4_linux_amd64.zip
dest: ./vault_1.5.4_linux_amd64.zip

Related

Ansible - Copy multiple files on remote host

I would like to copy few files on remote host , I used copy module as following but it copy all files under 'app' variable (dir) to 'backup_conf' variable (dir).
please advice here.
- name: backup configuration files
copy:
src: '{{ app }}'
dest: '{{ backup_conf }}'
remote_src: true
with_items:
- /bin/setenv.sh
- /conf/server.xml
Thank you, I fixed my issue:
- name: backup configuration files
copy:
src: "{{ item }}"
dest: "{{ backup_conf }}"
remote_src: true
with_items:
- /bin/setenv.sh
- /conf/server.xml

How to override some specific variables values in Ansible playbook

I have some multiple war files which I am downloading from nexus and creating the respective app folder on my remote host and copying the files.
Now there are 2 files which have a different name as war file but need to create a directory with different name. What I am doing right now is this -
- name: Ensuring the web deployment folder is created on tomcat nodes for each web application
file:
path: "{{tomcat_server_release_location}}/Release{{my_release_version}}/{{item}}"
state: directory
mode: 0777
with_items:
- "{{ apps }}"
- name: Copying the release from Admin server to tomcat nodes
copy: src={{admin_server_release_location}}/{{my_release_version}}/{{item}}-{{my_release_version}}.war dest={{tomcat_server_release_location}}/Release{{my_release_version}}/{{item}}
with_items:
- "{{ apps }}"
apps variable is defined like this -
webapps: ['test1','test2','test3','test4'].
Now test2 has a different name on the nexus it says test2-web.war but on the remote node I have to create a folder called just test2 and copy the war file in there.
Is there a way to override some variables at run time of playbook by using some condition or anything
If you want to override at runtime use extra vars.
From the docs --extra-vars "version=1.23.45"
Don't use conditions if you know a specific host should always be overwritten. I think placing it as a host var is best.
hosts:
myhost:
apps:
- test1
- trst2
See https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable for all of the various locations you can put variables and how to determine which will be used
It is possible to override variables by doing something like this:
- name: copy war
copy:
src: "{{ src_war | default('/path/to/my/war') }}"
dest: "{{ dest_war | default(src_war) }}"
Then at runtime you can set the variables src_war and dest_war either with extra vars, host/group_vars or any other way documented here: https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html
In your case I think it would be helpful to set a src & dest mapping:
- set_fact:
apps:
- {src: "test1"}
- {src: "test2", dest: "test2-web"}
- {src: "test3"}
- {src: "test4"}
Then you can use this mapping to achieve your goal:
- name: Ensuring the web deployment...
file:
path: {{ tomcat_server_release_location }}/Release{{ my_release_version }}/{{ item.src }}
state: directory
mode: 0777
with_items: "{{ apps }}"
- name: Copying the release...
copy:
src: "{{ admin_server_release_location }}/{{ my_release_version }}/{{ item.src }}-{{ my_release_version }}.war"
dest: "{{ tomcat_server_release_location }}/Release{{ my_release_version }}/{{ item.dest | default(item.src) }}"
with_items: "{{ apps }}"

ansible create local directory (master)

I forgot how to create on localhost ( ansible svr ) directory.
I'am using my ansible server as cache to download file and to copy them after that to the remote hosts.
Here example of task and playbook
tasks
- name: Create temp folder
file:
path: "{{ item }}"
state: directory
mode: 0755
with_items:
- /tmp/foo/
playbook
- hosts: foo
roles:
- foo
Tryed with this but doesn't work:
- name: Create temp folder
file:
path: "{{ item }}"
state: directory
mode: 0755
remote_src: no
with_items:
- /tmp/foo/
Thanks
I have found the solution delegate_to: localhost
- name: Create temp folder
file:
path: "{{ item }}"
state: directory
mode: 0755
delegate_to: localhost
with_items:
- /tmp/foo/

How to download and extract apache tomcat using ansible and rename unzaip file

I’m setting up a new setup for tomcat.
I wanna download and extract apache tomcat using ansible to several nodes of linux, but the thing is unzip file should be seen or rename as apache only. It doesn't matter what would be a version of tomcat?
- name: variable check
hosts: all
become: yes
vars:
tomcat_url: 'http://mirrors.estointernet.in/apache/tomcat/tomcat-8/v8.5.45/bin/apache-tomcat-8.5.45.tar.gz'
tasks:
- name:
get_url:
url: "{{tomcat_url}}"
dest: /opt
Here's complete working example:
---
- hosts: all
become: yes
vars:
tomcat_url: 'http://mirrors.estointernet.in/apache/tomcat/tomcat-8/v8.5.45/bin/apache-tomcat-8.5.45.tar.gz'
tasks:
- name: Download and Extract apache tomcat
unarchive:
src: "{{ tomcat_url }}"
dest: "/opt/"
remote_src: yes
- name: Synchronize the "{{ tomcat_url.split('/')[-1].rstrip('.tar.gz') }}" directory to apache on one remote host
synchronize:
src: "/opt/{{ tomcat_url.split('/')[-1].rstrip('.tar.gz') }}"
dest: "/opt/apache"
delegate_to: "{{ inventory_hostname }}"
- name: Remove the {{ tomcat_url.split('/')[-1].rstrip('.tar.gz') }} directory
file:
path: "/opt/{{ tomcat_url.split('/')[-1].rstrip('.tar.gz') }}"
state: absent

what is the best way to use ansible for reading hosts ip address and use it later?

i have 3 hosts, first one is the controller and the other two are used as lamp. i have to write a playbook in ansible which finds the ip address of each host and use it later in the same ansible playbook for settings.
basically i want to save it in a variable and use it for later.
Here is how i read it
---
- hosts: localhost
connection: local
tasks:
- debug: var=ansible_all_ipv4_addresses
- debug: var=ansible_default_ipv4.address
This is where i want to use it:
---
- hosts: laborator
become: yes
become_user: root
tasks:
- name: Create directory for php page
file:
path: /var/www/html/virtual1
state: directory
owner: apache
group: apache
mode: 0775
recurse: yes
- name: ensure file exists
copy:
content: ""
dest: /var/www/html/virtual1/info.php
owner: apache
group: apache
force: no
mode: 0555
- name: Add a string to the new file
lineinfile: dest=/var/www/html/virtual1/info.php
regexp='^'
line='<?php phpinfo(); ?>'
state=present
- name: Change file permissions
file:
path: /var/www/virtual1/info.php
owner: apache
group: apache
mode: 0644
- name: Set some kernel parameters
lineinfile:
path: /etc/hosts
regexp: '^'
line: '192.168.115.198 laborator1' <<<-here*****
You can see the line where i need the ip. I'm new to ansible this is my second day, please point me into the right direction.
thank you.
If you use the gather_fact: yes in your play (it's the default value unless you disabled in on your gathering config), you will be able to access the ansible_default_ipv4.address value for the current host.
If you want to access for another host (on which there was a gather_fact), you can use hostvars[INVENTORY_HOSTNAME].ansible_default_ipv4.address.
There is also ansible_ethX.ipv4.address for each interface.
You can see all the available variables by using the setup module
ansible all -m setup
Variables doc: http://docs.ansible.com/ansible/latest/playbooks_variables.html#magic-variables-and-how-to-access-information-about-other-hosts
In your case, it could be:
- name: Put IP of each laborator hosts in /etc/hosts
lineinfile:
path: /etc/hosts
regexp: '^'
line: '{{ hostvars[item].ansible_default_ipv4.address }} {{ hostvars[item].ansible_hostname }}'
with_items: '{{ groups.laborator }}'

Resources