ansible-playbook

Ansible lineinfile insertafter injects line at end of file

情到浓时终转凉″ 提交于 2019-12-04 06:19:53
I'm using lineinfile as follows: lineinfile dest=./hosts_exp insertafter='\[hosts1\]' line="xxxxxxxxx" state=present My hosts_exp is as follows: [local] localhost [hosts1] [hosts2] [hosts3] lineinfile inserts the text after [hosts3] instead of inserting it after [hosts1]. use: lineinfile: dest: "./hosts_exp" line: "xxxxxxxxx" insertafter: '^\[hosts1\]' state: present example: - name: "blah" lineinfile: dest: "/test.sh" insertafter: 'test text' line: "text add" state: present tedder42 It appears redundant, but you need to specify the regex too: lineinfile: dest: ./hosts_exp insertafter: '\

Playbooks in a subdirectory, not next to group_vars

喜你入骨 提交于 2019-12-04 03:14:01
Does anyone know how to put playbooks into folders, but share the same roles, group_vars, and other stuff typically located at the root dir? Here's what I'd like to have: root_dir: - group_vars - roles - inventory - playbooks - my_playbook.yml - site.yml - deploy.yml Our root dir is getting pretty big now and I'd like to split out some playbooks into their own folder (shown as playbooks/ above). An identical tiny playbook fails to run when inside a directory (say, playbooks/) vs at the root dir, because it doesn't grab stuff from group_vars. I can partially work around this, and run a playbook

How to delete *.web files only if they exist

旧街凉风 提交于 2019-12-04 03:10:50
I need to create an Ansible playbook to delete the *.web files in a specific directory only if the files exists. OS : cent OS, Redhat 5x, 6x. I have tried the following with no success: - stat: path=/opt/app/jboss/configuration/*.web register: web - shell: rm -rf /opt/app/jboss/configuration/*.web when: web.stat.exists Josh Smift @bruce-p's answer gives a deprecation warning with Ansible 2.0+, but the new with_fileglob gives another option: - file: path={{ item }} state=absent with_fileglob: /opt/app/jboss/configuration/*.web (Similar question remove all files containing a certain name within

Ansible + 10.11.6

不羁岁月 提交于 2019-12-04 02:13:30
问题 I'm having a weird issue with Ansible on a (very) clean install of 10.11.6. I've installed brew, zsh, oh-my-zsh, Lil' snitch and 1password (and literally nothing else). I installed ansible with... brew install ansible ... which was successful. I then went to a preexisting (and crazy simple) Ansible project and did an... ansible -m ping all It then asked me to enter my SSH passphrase. I've reinstated the keys from my previous install but I hadn't previously ssh'd into the server. I entered the

Commenting out a line with Ansible lineinfile module

China☆狼群 提交于 2019-12-04 00:05:14
I find it hard to believe there isn't anything that covers this use case but my search has proved fruitless. I have a line in /etc/fstab to mount a drive that's no longer available: //archive/Pipeline /pipeline/Archives cifs ro,credentials=/home/username/.config/cifs 0 0 What I want is to change it to #//archive/Pipeline /pipeline/Archives cifs ro,credentials=/home/username/.config/cifs 0 0 I was using this --- - hosts: slurm remote_user: root tasks: - name: Comment out pipeline archive in fstab lineinfile: dest: /etc/fstab regexp: '^//archive/pipeline' line: '#//archive/pipeline' state:

how to run a particular task on specific host in ansible

痴心易碎 提交于 2019-12-03 23:31:03
问题 my inventory file's contents - [webservers] x.x.x.x ansible_ssh_user=ubuntu [dbservers] x.x.x.x ansible_ssh_user=ubuntu in my tasks file which is in common role i.e. it will run on both hosts but I want to run a following task on host webservers not in dbservers which is defined in inventory file - name: Install required packages apt: name={{ item }} state=present with_items: - '{{ programs }}' become: yes tags: programs is when module helpful or there is any other way? How could I do this ?

Ansible: Get number of hosts in group

。_饼干妹妹 提交于 2019-12-03 22:46:24
I'm trying to get the number of hosts of a certain group. Imagine an inventory file like this: [maingroup] server-[01:05] Now in my playbook I would like to get the number of hosts that are part of maingroup which would be 5 in this case and store that in a variable which is supposed to be used in a template in one of the playbook's tasks. At the moment I'm setting the variable manually which is far from ideal.. vars: HOST_COUNT: 5 vars: HOST_COUNT: "{{ groups['maingroup'] | length }}" Cigizmoond Vyhuholev Also without explicit group name: vars: HOST_COUNT: "{{ ansible_play_hosts | length }}"

running an ansible local task in a remote playbook

戏子无情 提交于 2019-12-03 22:32:20
I'm trying to get this task to run locally (on the machine that is running the playbook) : - name: get the local repo's branch name local_action: git branch | awk '/^\*/{print $2}' register: branchName I tried plenty of variations with no success all other tasks are meant to run on the target host, which is why running the whole playbook local is not an option TASK: [get the local repo's branch name] ************************************** <127.0.0.1> REMOTE_MODULE git branch | awk '/^\*/{print $2}' <127.0.0.1> EXEC ['/bin/sh', '-c', 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1407258765.57

Running an Ansible Playbook using Python API 2.0.0.1

ⅰ亾dé卋堺 提交于 2019-12-03 16:38:30
Ansible version : 2.0.0.1 I've been looking around quite a bit now, and most documentation I find is either incomplete or deprecated ( this post is for version 1.8.4, ie ) I'm trying to launch an Ansible playbook through the Python API. Ansible's documentation seem to be showing how to generate and play tasks, but not how to load and run a playbook yml file. I've been digging into the code to try to understand how to launch it, and I think I've done some progress, but I'm really hitting a wall. Here's what I have so far : def createcluster(region, environment, cluster): Options = namedtuple(

How to traverse a nested dict structure with Ansible?

房东的猫 提交于 2019-12-03 14:31:10
I have the following dict structure variable in an ansible playbook: apache_vhosts: - name: foo server_name: foo.com server_aliases: - a.foo.com - b.foo.com - c.foo.com - name: bar server_name: bar.com server_aliases: - d.bar.com - e.bar.com - f.bar.com I need to create a symlink for each of the server_name and server_aliases domains, e.g.: /tmp/foo.com -> /var/www/foo /tmp/a.foo.com -> /var/www/foo /tmp/b.foo.com -> /var/www/foo /tmp/c.foo.com -> /var/www/foo /tmp/bar.com -> /var/www/bar /tmp/d.bar.com -> /var/www/bar /tmp/e.bar.com -> /var/www/bar /tmp/f.bar.com -> /var/www/bar I have the