legacy-wiki
Ansible
Recovered from the older tannerjc.net wiki snapshot dated January 23, 2016.
Is there a way to run ansible-playbook in debug mode to save the scripts it writes?
set ANSIBLE_KEEP_REMOTE_FILES=1 in your ansible.cfg
How do I debug a remote hung module?
-
ANSIBLE_KEEP_REMOTE_FILES=1 ansible-playbook -vvvv
-
get the path for the remote file from the debug output
-
python -m trace –trace module-file
-
https://groups.google.com/forum/#!msg/ansible-project/JdZWzLeZMmk/93W-42MP9U0J
How do I make something idempotent?
- first check if XYZ exists, register result as variable
- next task, create XYZ if variable returns false
How do I utilize variables registered from the command and shell modules in templates?
- The command and shell modules produce registered variables with a stdout key, and can be referenced with foo[‘stdout’] in a jinja2 template
---
- hosts: ubuntu
gather_facts: yes
vars:
- tvar: hello world
tasks:
- name: get uname output
command: uname -a
register: uname_command
- name: get uname output
shell: uname -a
register: uname_shell
- name: save both sets of facts
template: src=files/data.j2 dest=/tmp/facts.txt
-------------------
jtanner@u1304:~/playbooks$ cat files/data.j2
##################
{{ uname_command }}
##################
{{ uname_shell }}
##################
-------------------
##################
{u'changed': True, u'end': u'2013-09-25 14:05:53.124404', u'stdout': u'Linux u1304 3.8.0-19-generic #29-Ubuntu SMP Wed Apr 17 18:16:28 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux', u'cmd': [u'uname', u'-a'], u'start': u'2013-09-25 14:05:53.122165', u'delta': u'0:00:00.002239', u'stderr': u'', u'rc': 0, 'invocation': {'module_name': 'command', 'module_args': 'uname -a'}, 'stdout_lines': [u'Linux u1304 3.8.0-19-generic #29-Ubuntu SMP Wed Apr 17 18:16:28 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux']}
##################
{u'changed': True, u'end': u'2013-09-25 14:05:53.212422', u'stdout': u'Linux u1304 3.8.0-19-generic #29-Ubuntu SMP Wed Apr 17 18:16:28 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux', u'cmd': u'uname -a ', u'start': u'2013-09-25 14:05:53.209926', u'delta': u'0:00:00.002496', u'stderr': u'', u'rc': 0, 'invocation': {'module_name': 'shell', 'module_args': 'uname -a'}, 'stdout_lines': [u'Linux u1304 3.8.0-19-generic #29-Ubuntu SMP Wed Apr 17 18:16:28 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux']}
##################
How do I register and use the output from the facter and ohai modules in a template?
- Modules such as facter and ohai produce register variables that do not have a stdout key, but will actually be a nested dictionary which can be addressed like foo[‘key1’][‘key2’][‘key3’]
---
- hosts: ubuntu
gather_facts: yes
vars:
- tvar: hello world
tasks:
- name: get facter output
facter:
register: factervars
- name: get ohai output
ohai:
register: ohaivars
- name: save both sets of facts
template: src=files/facter-ohai.j2 dest=/tmp/facts.txt
-------------------
jtanner@u1304:~/playbooks$ cat files/facter-ohai.j2
##################
{{ factervars['kernel'] }}
##################
{{ ohaivars['kernel']['name'] }}
##################
-------------------
jtanner@u1304:/tmp$ sudo cat facts.txt
##################
Linux
##################
Linux
##################
How do I run the ansible unit tests?
- sudo apt-get install python-nose pyflakes pep8
- cd ansible
- make pyflakes
- make pep8
- make tests
How do I install the latest devel with pip?
pip install -I git+https://github.com/ansible/ansible.git@devel#egg=ansible
Why can’t I install packages with apt in EC2?
- https://github.com/ansible/ansible/issues/4915
- http://stackoverflow.com/questions/12438946/e-unable-to-locate-package-git-ubuntu-on-ec2
- shell: for i in 1 2 3; do apt-get update -yq --fix-missing ; apt-get install -yq git ; done;
Why is there no generic package module?
- not wanting to limit a module to the lowest common denominator of features
- different package names for different package managers
- multiple package managers installed, and you don’t want to guess which to use
How can I put my initial ssh keys on all my hosts?
# set the root password if not set
ansible all -s -K -k -u jtanner -m shell -a 'echo redhat | passwd --stdin root' #RHEL
ansible all -s -K -k -u jtanner -m shell -a 'echo root:redhat | chpasswd' #UBUNTU
# set the authorized key
ansible all -k -u root -m authorized_key -a state=present user=root key='ssh-rsa KEYSTRING jtanner@corsair'
# test
ansible all -u root -m ping
Why are my tasks taking a long time to complete?
blackdog hey all. am having an odd problem - running date on all my servers with ansible has suddenly slowed down a heap. on one group of 50, it takes 46s, for instance. -f is set to 80, and a for loop with ssh finishes in about three seconds, so I don't think it could be my ssh config. Any ideas?
drybjed /quit
jtanner blackdog, elaborate suddenly
jtanner what has changed
blackdog jtanner: I don't know - nothing springs to mind.
jtanner did you update ansible?
blackdog i did, but only because it was running so slowly.
blackdog now it's at 1.4.4
jtanner is memory or cpu completely consumed on your control host?
blackdog no.
jtanner i would reduce your forks down to 1, to get a realistic estimate of round trip time for one host
jtanner then scale up to determine if ansible is really at fault
blackdog righto. 1m48s for 1 fork
blackdog hm, i think i have it now. i had StrictKeyChecking no on in the ssh config, and UserKnownHostsFile set to /dev/null - works fine for ssh conns, but seems to slow ansible down a heap.
blackdog anyway, seems to be working now. thanks for your help.