I wish to write a playbook in ansible which will first transfer my package to remote hosts and then run a script. In detail, let's say I have apache package in local machine and need to scp/rsync it to remote nodes A & B. Then I have my script to install the package on A & B both, check whether it was installed properly followed by scrutinizing the config file etc. This script should run only if the transfer is successful.
Have written the below playbook which should meet above requirement. Please confirm if it needs further improvement. Thanks in advance !
Playbook:
---
- hosts: droplets
remote_user: root
tasks:
- name: Copy package to target machines
synchronize: src=/home/luckee/apache.rpm dest=/var/tmp/
- name: Run installation and verification script
script: /home/luckee/apache_install.sh
register: result
- name: Show result
debug: msg="{{ result.stdout }}"
...
This way the installation script will only run if the copy tasks changed (was execuded in the process) and exited successfully:
---
- hosts: droplets
remote_user: root
tasks:
- name: Copy package to target machines
synchronize: src=/home/luckee/apache.rpm dest=/var/tmp/
register: result_copy
- name: Run installation and verification script
script: /home/luckee/apache_install.sh
register: result_run
when: result_copy.changed
- name: Show result
debug: msg="{{ result_run.stdout }}"
...
Related
Is there a way to wait for ssh to become available on a host before installing a role? There's wait_for_connection but I only figured out how to use it with tasks.
This particular playbook spin up servers on a cloud provider before attempting to install roles. But fails since the ssh service on the hosts isn't available yet.
How should I fix this?
---
- hosts: localhost
connection: local
tasks:
- name: Deploy vultr servers
include_tasks: create_vultr_server.yml
loop: "{{ groups['vultr_servers'] }}"
- hosts: all
gather_facts: no
become: true
tasks:
- name: wait_for_connection # This one works
wait_for_connection:
delay: 5
timeout: 600
- name: Gather facts for first time
setup:
- name: Install curl
package:
name: "curl"
state: present
roles: # How to NOT install roles UNLESS the current host is available ?
- role: apache2
vars:
doc_root: /var/www/example
message: 'Hello world!'
- common-tools
Ansible play actions start with pre_tasks, then roles, followed by tasks and finally post_tasks. Move your wait_for_connection task as the first pre_tasks and it will block everything until connection is available:
- hosts: all
gather_facts: no
become: true
pre_tasks:
- name: wait_for_connection # This one works
wait_for_connection:
delay: 5
timeout: 600
roles: ...
tasks: ...
For more info on execution order, see this title in role's documentation (paragraph just above the notes).
Note: you probably want to move all your current example tasks in that section too so that facts are gathered and curl installed prior to do anything else.
I have an ansible playbook which will copy a file into a location on a remote server. It works fine. In this case, the file is an rpm. This is the way it works:
---
- hosts: my_host
tasks:
- name: mkdir /tmp/RPMS
file: path=/tmp/RPMS state=directory
- name: copy RPMs to /tmp/RPMS
copy:
src: "{{ item }}"
dest: /tmp/RPMS
mode: 0755
with_items:
[any_rpm-x86_64.rpm]
register: rpms_copied
Now, with the file successfully on the remote server, I need to start some new logic that will install the rpm that sits in /tmp/RPMS. I have run many different versions of the below (So this code is added onto the above block):
- name: install rpm from file
yum:
name: /tmp/RPMS/any_rpm-x86_64.rpm
state: present
become: true
I don't know if the formatting is incorrect, or if this is not the way. Can anyone advise as to how I can get the rpm in the directory /tmp/RPMS installed using a new few lines in the existing playbook?
Thanks.
I did not find this anywhere else, and it genuinely took me all of my working day to get to this point. For anyone else struggling:
- name: Install my package from a file on server
shell: rpm -ivh /tmp/RPMS/*.rpm
async: 1800
poll: 0
become_method: sudo
become: yes
become_user: root
I'm started to try Ansible, and using example code from Ansible Documentation. After I try several examples, I get error at the beginning of the code. It says
- name: Change the hostname to Windows_Ansible
^ here(Point at name)"
Any advice would be appreciate.
I tried this one
https://docs.ansible.com/ansible/latest/modules/win_hostname_module.html#win-hostname-module
---
- name: Change the hostname to Windows_Ansible
win_hostname:
name: "Windows_Ansible"
register: res
- name: Reboot
win_reboot:
when: res.reboot_required
The below task will change the hostname of the server. Make sure you run on a test server so that it wont create issues. If you just wanted to test some playbook, use the second playbook with win_command
---
- hosts: <remote server name which needs to be added in the inventory>
tasks:
- name: Change the hostname to Windows_Ansible
win_hostname:
name: "Windows_Ansible"
register: res
- name: Reboot
win_reboot:
when: res.reboot_required
---
- hosts: <remote server name which needs to be added in the inventory>
tasks:
- name: Test
win_command: whoami
register: res
I have ansible playbook which look similar to the code below :
---
- hosts: localhost
connection: local
tasks:
- name: "Create custom fact directory
file:
path: "/etc/ansible/facts.d"
state: "directory"
- name: "Insert custom fact file"
copy:
src: custom_fact.fact
dest: /etc/ansible/facts.d/custom_fact.fact
mode: 0755
roles:
- role1
- role2
once i am running the playbook with ansible-playbook command
only the roles is running ,but the tasks is not getting ran
if i am remarking the roles from the playbook,the task gets ran
how can i make the task to run before the roles ?
Put the tasks in a section pre_tasks which are run before roles.
You may also find post_tasks useful which run tasks after roles.
Correct the indentation
- hosts: localhost
connection: local
tasks:
- name: "Create custom fact directory
file:
path: ...
I am using the win_file Ansible module to create directories and files on Windows 2012 R2 servers. I want to register the results of the win_file task but I can't get it to work.
For example this playbook...
---
- hosts: windows_server
gather_facts: no
- name: create directory on remote windows server
win_file:
path: 'c:\temp}'
state: directory
register: task_results
debug: var=task_results
...does not print the results of the task.
Any help would be greatly appreciated.
Your playbook syntax is flawed... Try this:
---
- hosts: windows_server
gather_facts: no
tasks:
- name: create directory on remote windows server
win_file:
path: 'c:\temp'
state: directory
register: task_results
- debug: var=task_results
register is a part of task not module's parameter, so it should be with the same indentation.
debug is a new task, so it should be another item in a list.
Also there is not tasks keyword in your example.