ansible create local directory (master) - ansible

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/

Related

How to run a command on localhost to define variable for an Ansible playbook?

I'm very new to Ansible and trying to figure things out. I have a simple playbook to run on a remote host. To simplify drastically:
- hosts: all
name: build render VM
tasks:
- copy:
src: ./project_{{ project_id }}.yaml
dest: /app/project.yaml
owner: root
I would like to have project_id set to the output of this command, run on localhost: gcloud config get-value project. Ideally I'd like that to be stored into a variable or fact that can be used throughout the playbook. I know I can pass project_id=$(...) on the ansible cmd line, but I'd rather have it set up automatically in the playbook.
Taking for granted the given command only returns the id and nothing else.
With a task delegated to localhost:
- hosts: all
name: build render VM
tasks:
- name: get project id
command: gcloud config get-value project
register: gcloud_cmd
run_once: true
delegate_to: localhost
- name: set project id
set_fact:
project_id: "{{ gcloud_cmd.stdout }}"
- copy:
src: ./project_{{ project_id }}.yaml
dest: /app/project.yaml
owner: root
With a pipe lookup:
- hosts: all
name: build render VM
tasks:
- name: set project id from localhost command
set_fact:
project_id: "{{ lookup('pipe', 'gcloud config get-value project') }}"
run_once: true
- copy:
src: ./project_{{ project_id }}.yaml
dest: /app/project.yaml
owner: root

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

I am having an issue with ansible where I am trying to sync /etc/yum.repos.d with other hosts

This is the ansible script. Its not copying all the files /etc/yum.respos.d to the other hosts.
- name: Sync System Files
hosts: all
tasks:
- name: Sync System Files
tags: sync-push
synchronize:
src: "{{ item }}"
dest: "/"
mode: push
recursive: yes
register: syncfile
with_items:
- "/etc/yum.repos.d"

Ansible get_url Fails If Destination File Does Not Exist

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

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

Resources