#1283
[Ansible] How-To
Copy template file with filtering (template)
- name: Copy template.properties template: src=tempplate.properties.j2 dest={{ target_folder }}/tempplate.properties
Copy file (no filtering) (copy)
- name: Copy file copy: src=a.txt dest='{{ target_folder }}/a.txt'
Copy folder (no filtering)
- name: Copy file copy: src=folderA dest='{{ target_folder }}/'
Don’t forget the ‘/’ at the end
Execute shell script (shell)
- name: Execute script shell: chdir={{ script_folder }} ./script.sh
or (command)
- name: Execute script command: chdir={{ script_folder }} ./script.sh
Wait for a “web service” to be up (waif_for)
- name: Wait for server be started wait_for: host=localhostport="8080" delay=2 timeout=10 state=drained
or
- name: Wait for server be started wait_for: host=localhostport="8080" delay=2 timeout=10 state=started
Include file only if exists (don’t failed if missing)
tasks: - include: "{{ item }}" with_first_found: - files: - /home/user/optional/file.yml skip: true
Test if changes have been performed or not
- name: "Update file content" lineinfile: dest='aa.txt' regexp='^APP.*' line="APP=TOTO" register: updateFile - name: "Do somehting ony if update" when: updateFile.changed
Include file only if exists (don’t failed if missing)
tasks: - include: "{{ item }}" with_first_found: - files: - /home/user/optional/file.yml skip: true
set_fact :
Create a map :
- set_fact: map_of_values: "{{ map_of_values|default({}) | combine( {THIS_IS_MY_KEY: THIS_IS_MY_VALUE} ) }}" with_items: "{{SOMETHING_TO_LOOP_OVER}}"
Compute a counter :
- set_fact: nb_modules: "{{nb_modules | int +1 }}"
Unix – Awk : retrieve IP address Installation d’un serveur OpenVPN rapidement
Pass args/vars to template :
Include or template with extra variables :
access one of “hostvar” from group name :
will return the value of “ansible_ssh_host” for the first “server” from group “group1”
IP Address (even if notdefined in host file) :
With setFact :
Cheat Sheet : https://gist.github.com/andreicristianpetcu/b892338de279af9dac067891579cad7d
Ansible toolbox : https://github.com/larsks/ansible-toolbox
Ansible execute local playbook :
with local.host :
Using an SSH bastion with Ansible.
Update inventory and add :