A few ansible tips.
Some distro's by default put the ansible.cfg in /etc/ansible
That is where ansible expects it to be by default.
You can also create a file called "hosts" and put it in /etc/ansible
Then you don't have to include the inventory file every time you type the command.
vvv is a little too verbose for me, I usually use a single v, unless I'm really having problems.
If you install a package called "sshpass" ( available for most distros ) and copy the ssh key over
then you don't have to enter a password everytime. A simple way to do this is..
ssh-keygen -t ecdsa -b 384
Just press enter several times to go with the defaults, I recommend not using a passphrase ( leave it null )
After the key is created, run the following command as the user you run the playbook as.
ssh-copy-id [email protected]
You will have to type in the password once, but after that you shouldn't need to.
Finally, if I can ( this isn't always possible ) I test it locally. In the example above where you are
simply grepping for the release name in the os-release file, you could probably do that on the localhost.
The advantage of doing this, is that you know the script works and it isn't a ssh or permission problems
before you run it remotely.
When possible use the built-in ansible modules, instead of writing your own. For example...
Code:
- name: Gather Server Information
hosts: localhost
gather_facts: yes
tasks:
- name: Get Uptime
command: "uptime -p"
register: uptime_result
- name: Get Video Card
shell: "lspci | grep VGA | cut -f3 -d:"
register: shell_output
- name: Replace null video if needed
set_fact:
video: "{{ shell_output.stdout | default('unknown') }}"
- name: Check for root filesystem device
shell: "mount | grep ' / ' | awk '{ print $1 }'"
register: root_fs_device
changed_when: false
failed_when: root_fs_device.rc != 0
- name: Create Text Output
set_fact:
server_info_text: |
Hostname: {{ ansible_nodename }}
CPU Model: {{ ansible_processor[2] }}
CPU Cores: {{ ansible_processor_vcpus }}
Date and Time: {{ ansible_date_time.date }} {{ ansible_date_time.time }}
IPv4 Address: {{ ansible_default_ipv4.address }}
Interface: {{ ansible_default_ipv4.interface }}
IPv4 Netmask: {{ ansible_default_ipv4.netmask }}
IPv4 Gateway: {{ ansible_default_ipv4.gateway }}
MAC Address: {{ ansible_default_ipv4.macaddress }}
IPv6 Address: {{ ansible_default_ipv6.address }}
IPv6 Gateway: {{ ansible_default_ipv6.gateway }}
IPv6 Prefix: {{ ansible_default_ipv6.prefix }}
Distribution: {{ ansible_distribution }} {{ ansible_distribution_version }}
Kernel: {{ ansible_kernel }}
SELinux Status: {{ ansible_selinux.status }}
Uptime: {{ uptime_result.stdout }}
Memory: {{ ansible_memtotal_mb }} MB
Video Card: {{ video }}
Python Version: {{ ansible_python_version }}
- name: Print Text Output
debug:
msg: "{{ server_info_text }}"
There is a line in this playbook, that has ansible_distribution and ansible_distribution_version.
There is another line with ansible_kernel
These are built in modules that will give you the same info as grepping the os-release file.
To see a list of all the standard macros, run this command.
ansible -m setup localhost
There are a lot more, but that's good start.