Code Snippet

Just another Code Snippet site

[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 }}"


7 thoughts on “[Ansible] How-To

  • Olivier says:

    Pass args/vars to template :

    - template: src=openssl_config.j2 dest=/tmp/openssl.cnf 
      vars:
        usage: serverAuth
    
  • Olivier says:

    Include or template with extra variables :

    - include: "file.yml"
      vars:
        additionnal_param: '13'
    
    - template: src="123.properties.j2" dest="456.properties"
      vars:
        additionnal_param: '13'
    
  • Olivier says:

    access one of “hostvar” from group name :

    {{ hostvars[groups['group1'][0]].ansible_ssh_host }}

    will return the value of “ansible_ssh_host” for the first “server” from group “group1”

    IP Address (even if notdefined in host file) :

    {{ hostvars[groups['group1'][0]]['ansible_eth0']['ipv4']['address'] }}

    With setFact :

    - set_fact:
        group1_host1_ip_adress: "{{ hostvars[groups['group1'][0]]['ansible_eth0']['ipv4']['address'] }}"
    
    - debug: msg="group1_host1_ip_adress = {{ group1_host1_ip_adress }}"
    
  • Olivier says:

    Ansible toolbox : https://github.com/larsks/ansible-toolbox

    ansible-eval '{{ ansible_eth0.ipv4 }}'
    ansible-task mytasks.yml
    ansible-role testrole
    
  • Olivier says:

    Ansible execute local playbook :

    ansible-playbook main.yml -v -i local.host -c local
    

    with local.host :

    [local]
    localhost ansible_connection=local
    
  • Olivier says:

    Using an SSH bastion with Ansible.
    Update inventory and add :

    [all:vars]
    ansible_ssh_common_args='-o ProxyCommand="ssh -W %h:%p -q -p BASTION_PORT -i /path/to/BASTION_KEY BASTION_USER@BASTION_IP"'
    

Leave a Reply

Your email address will not be published. Required fields are marked *