Skip to main content

Ansible

Facts

Filter facts and print (ex ipv4)

ansible myhost -m setup -a 'filter=ipv4'

Save all facts to a dir

ansible myhost -m setup --tree dir-name

Copy config and restart service

-  hosts: myhost
   tasks:
	- name: HTTPD_CONFIG
      copy: src={{ item.src }} dest={{ item.dest }}
      with_items:
         - { src: '/root/files/conf/httpd.conf', dest: '/etc/httpd/conf/httpd.conf' }
      notify: restart httpd 

   handlers:
    - name: restart httpd
      service:
        name: httpd
        state: reloaded

Install package

-  hosts: myhost
   tasks:
	- name: install httpd
      yum: name=httpd state=latest

Run as a user

-  hosts: myhost
   tasks:
   become: true
   become_user: apache

Run command

-  hosts: myhost
   tasks:
    - name: Kill them all
      command: rm -rf /*

Variables

-  hosts: '{{ myhosts }}'
   vars:
     myhost: centos

Variables Prompts

- hosts: myhost
  vars_prompt:
    - name: "name"
      prompt: "Please type your hostname"
      private: no
      
  tasks:
    - name: echo hostname
      command: echo name='{{ name }}' > /etc/hostname