Posts

Showing posts with the label Ansible Playbook

Ansible - Print Message - Debug: Msg="line1 \n {{ Var2 }} \n Line3 With Var3 = {{ Var3 }}"

Answer : debug module support array, so you can do like this: debug: msg: - "First line" - "Second line" The output: ok: [node1] => { "msg": [ "First line", "Second line" ] } Or you can use the method from this answer: In YAML, how do I break a string over multiple lines? The most convenient way I found to print multi-line text with debug is: - name: Print several lines of text vars: msg: | This is the first line. This is the second line with a variable like {{ inventory_hostname }}. And here could be more... debug: msg: "{{ msg.split('\n') }}" It splits the message up into an array and debug prints each line as a string. The output is: ok: [example.com] => { "msg": [ "This is the first line.", "This is the second line with a variable like example.com", "And...

Ansible And Git Permission Denied (publickey) At Git Clone

Answer : By reading the documentation for ssh forwarding in ansible. I was able to figure out the solution. The problem was that my ssh keys were not being forwarded because Ansible does not by default forward your keys, even if you have set up the key forwarding on ~/.ssh/conf (I updated my question with the ansible.cfg that I had before fixing the issue). The solution is was to add transport = ssh to ansible.cfg under [defaults] plus running ansible-playbook from the location where ansible.cfg is located and make sure thet the following setting exists in the /etc/ssh/sshd_config of the target box: AllowAgentForwarding yes My ansible.cfg now looks like this: [defaults] transport = ssh [ssh_connection] ssh_args = -o ForwardAgent=yes To clone the private github repo over the remote server, I am doing this: First add the ssh key to your ssh-agent: eval `ssh-agent -s` ssh-add ~/.ssh/my-private-key.pem After that I have modified the ansible.cfg : [defaults] ...

Check If Service Exists With Ansible

Answer : See the service_facts module, new in Ansible 2.5. - name: Populate service facts service_facts: - debug: msg: Docker installed! when: "'docker' in services" Of course I could also just check if the wrapper script exists in /etc/init.d. So this is what I ended up with: - name: Check if Service Exists stat: path=/etc/init.d/{{service_name}} register: service_status - name: Stop Service service: name={{service_name}} state=stopped when: service_status.stat.exists register: service_stopped It would be nice if the "service" module could handle "unrecognized service" errors. This is my approach, using the service command instead of checking for an init script: - name: check for apache shell: "service apache2 status" register: _svc_apache failed_when: > _svc_apache.rc != 0 and ("unrecognized service" not in _svc_apache.stderr) - name: disable apache service: name=...

Ansible. Fast Way To Check Syntax?

Answer : This is expected behaviour according to the documentation: When ansible-playbook is executed with --check it will not make any changes on remote systems. Instead, any module instrumented to support ‘check mode’ (which contains most of the primary core modules, but it is not required that all modules do this) will report what changes they would have made rather than making them. Other modules that do not support check mode will also take no action, but just will not report what changes they might have made. http://docs.ansible.com/ansible/playbooks_checkmode.html If you would like to check the YAML syntax you can use syntax-check. ansible-playbook rds_prod.yml --syntax-check playbook: rds_prod.yml I was looking for the same, but was not satisfied by the --syntax-check option, since it does not work its way down to the roles. A more complete check can be performed with ansible-lint which also includes style-checks. But if you turn off all style-chec...

Ansible Command Module Says That '|' Is Illegal Character

Answer : From the doc: command - Executes a command on a remote node The command module takes the command name followed by a list of space-delimited arguments. The given command will be executed on all selected nodes. It will not be processed through the shell, so variables like $HOME and operations like "<", ">", "|", and "&" will not work (use the shell module if you need these features). shell - Executes a commands in nodes The shell module takes the command name followed by a list of space-delimited arguments. It is almost exactly like the command module but runs the command through a shell (/bin/sh) on the remote node. Therefore you have to use shell: dpkg -l | grep python-apt . read about the command module in the Ansible documentation: It will not be processed through the shell, so .. operations like "<", ">", "|", and "&" will not work As it recommends, use t...

Ansible Synchronize With Wildcard

Answer : This can be done with ansible's with_lines: - name: Install services jar synchronize: src="{{item}}" dest=/opt/company/ with_lines: "find {{ core_repo }}/service-packaging/target/ -name all-services*.jar | grep -v original" Ansible module synchronize uses rsync and supports custom options for rsync through parameter rsync_opts (since 1.6) which could be used to filter file. Example: - name: sync source code synchronize: src: "/path/to/local/src" dest: "{{lookup('env','HOME')}}/remote/src" rsync_opts: - "--include=*.py" - "--exclude=*.pyc" - "--delete"